Calling the Web Service dynamically (.NET 动态访问Web Service)

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!

posted @ 2017-06-13 17:33 kiddy-star 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值