zoukankan      html  css  js  c++  java
  • Calling the Web Service dynamically (.NET 动态访问Web Service)

    针对.NET平台下的WebService访问,为达到不添加引用的情况下,动态调用外部服务。

    主体方法:

        public class WebServiceHelper
        {
            //Calling the WebService dynamically
            public static T CallWebServiceDynamic<T>(string address, string serviceName, string serviceMethod, object[] args) {
                WebClient client = new WebClient();
                System.IO.Stream stream = client.OpenRead(address);
                ServiceDescription description = ServiceDescription.Read(stream);
    
                ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
                importer.ProtocolName = "Soap12";
                importer.AddServiceDescription(description, null, null);
                importer.Style = ServiceDescriptionImportStyle.Client;
                importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
    
                CodeNamespace nmspace = new CodeNamespace();
                CodeCompileUnit compileUnit = new CodeCompileUnit();
                compileUnit.Namespaces.Add(nmspace);
                ServiceDescriptionImportWarnings warning = importer.Import(nmspace, compileUnit);
                if ( warning != 0 ) {
                    throw new Exception($"Warning:{warning}");
                }
    
                CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
                string[] assemblyReferences = new string[3] {
                    "System.dll",
                    "System.Xml.dll",
                    "System.Web.Services.dll"
                };
                CompilerParameters parms = new CompilerParameters(assemblyReferences);
                parms.GenerateExecutable = false;
                parms.GenerateInMemory = true;
    
                CompilerResults results = provider.CompileAssemblyFromDom(parms, compileUnit);
                object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
                MethodInfo method = wsvcClass.GetType().GetMethod(serviceMethod);
                var data = method.Invoke(wsvcClass, args);
                if ( data != null ) {
                    return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(data));
                }
                return default(T);
            }
        }
    View Code

    使用场景:

    string endPointAddress = ConfigurationManager.AppSettings["xxxxxx"];
    
            public WorkPackageServiceModel[] GetMinimalWorkPackage(Guid projectItemId, string projectNumber) {
                return WebServiceHelper.CallWebServiceDynamic<WorkPackageServiceModel[]>(endPointAddress, "DataCenter", "GetMinimalWorkPackage", new object[] { projectItemId, projectNumber });
            }
    View Code

    That's all!

  • 相关阅读:
    day26:面向对象进阶:set、get、del反射和内置
    day26、面向对象进阶:多态、封装、反射
    day25、 静态属性、类方法、静态方法、组合、继承、
    day24:面向对象设计与面向对象编程、类和对象
    day23:s
    day21、模块
    阿里云ECS服务器挂载磁盘
    Python爬虫总结——常见的报错、问题及解决方案
    Python爬虫实战——反爬机制的解决策略【阿里】
    Python爬虫实战——反爬策略之模拟登录【CSDN】
  • 原文地址:https://www.cnblogs.com/kongdf/p/7002496.html
Copyright © 2011-2022 走看看