整理,内容来源于网络及本人实践】
前面整理了一篇关于VS2008实现简单的WebService的文章,但里面的调用是静态的,当服务地址更改后,需要重新编译程序,对于很多应用来说是不现实的。本文介绍一下C#动态调用WebService的方法。
1.实现
写个叫WebServiceHelper的类
[csharp] view plaincopyprint?
1. namespace webhelp
2. {
3. using System;
4. using System.Net;
5. using System.IO;
6. using System.CodeDom;
7. using Microsoft.CSharp;
8. using System.CodeDom.Compiler;
9. using System.Web.Services.Description;
10. using System.Web.Services.Protocols;
11. public class WebServiceHelper
12. {
13. #region InvokeWebService
14.
15. public static object InvokeAndCallWebService(string url, string methodname, object[] args)
16. {
17. return WebServiceHelper.InvokeAndCallWebService(url, null, methodname, args);
18. }
调用webservice服务
19. public static object InvokeAndCallWebService(string url,/* string @namespace, */string classname, string methodname, object[] args)
20. {
21. string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
22. if ((classname == null) || (classname == ""))
23. {
24. classname = WebServiceHelper.GetWsClassName(url);
25. }
26.
27. try
28. {
29. //获取WSDL
30. WebClient webClient = new WebClient();
31. Stream stream = webClient.OpenRead(url + "?WSDL");
32. ServiceDescription description = ServiceDescription.Read(stream);
33. ServiceDescriptionImporter descriptionImporter = new ServiceDescriptionImporter();
34. descriptionImporter.AddServiceDescription(description, "", "");
35. CodeNamespace codeNamespace = new CodeNamespace(@namespace);
36.
37. //生成客户端代理类代码
38. CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
39. codeCompileUnit.Namespaces.Add(codeNamespace);
40. descriptionImporter.Import(codeNamespace, codeCompileUnit);
41. CSharpCodeProvider codeProvider = new CSharpCodeProvider();
42.
43. //设定编译参数
44. CompilerParameters compilerParameters = new CompilerParameters();
45. compilerParameters.GenerateExecutable = false;
46. compilerParameters.GenerateInMemory = true;
47. compilerParameters.ReferencedAssemblies.Add("System.dll");
48. compilerParameters.ReferencedAssemblies.Add("System.XML.dll");
49. compilerParameters.ReferencedAssemblies.Add("System.Web.Services.dll");
50. compilerParameters.ReferencedAssemblies.Add("System.Data.dll");
51.
52. //编译代理类
53. CompilerResults codeResult = codeProvider.CompileAssemblyFromDom(compilerParameters, codeCompileUnit);
54. if (true == codeResult.Errors.HasErrors)
55. {
56. System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
57. foreach (System.CodeDom.Compiler.CompilerError compileError in codeResult.Errors)
58. {
59. stringBuilder.Append(compileError.ToString());
60. stringBuilder.Append(System.Environment.NewLine);
61. }
62. throw new Exception(stringBuilder.ToString());
63. }
64.
65. //生成代理实例,并调用方法
66. System.Reflection.Assembly assembly = codeResult.CompiledAssembly;
67. Type @class = assembly.GetType(@namespace + "." + classname, true, true);
68. object instance = Activator.CreateInstance(@class);
69. System.Reflection.MethodInfo methorInfo = @class.GetMethod(methodname);
70.
71. return methorInfo.Invoke(instance, args);
72.
73. }
74. catch (Exception ex)
75. {
76. throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
77. }
78. }
79.
80. private static string GetWsClassName(string wsUrl)
81. {
82. string[] parts = wsUrl.Split('/');
83. string[] pps = parts[parts.Length - 1].Split('.');
84.
85. return pps[0];
86. }
87. #endregion
88. }
89. }
namespace webhelp
{
    using System;
    using System.Net;
    using System.IO;
    using System.CodeDom;
    using Microsoft.CSharp;
    using System.CodeDom.Compiler;
    using System.Web.Services.Description;
    using System.Web.Services.Protocols;
    public class WebServiceHelper
    {
        #region InvokeWebService
        public static object InvokeAndCallWebService(string url, string methodname, object[] args)
        {
            return WebServiceHelper.InvokeAndCallWebService(url, null, methodname, args);
        }
        public static object InvokeAndCallWebService(string url,/* string @namespace, */string classname, string methodname, object[] args)
        {
            string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
            if ((classname == null) || (classname == ""))
            {
                classname = WebServiceHelper.GetWsClassName(url);
            }
            try
            {
                //获取WSDL 
                WebClient webClient = new WebClient();
                Stream stream = webClient.OpenRead(url + "?WSDL");
                ServiceDescription description = ServiceDescription.Read(stream);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。