• C#动态调用WebService


    大致思路:

    1,得到对应Webservice的WSDL文件说明;

    2,根据得到的WSDL动态编译成代理类;

    3,利用反射调用编译的代理类,并得到返回值。

    下面对应这三个部分分别贴出代码:

    private  CodeCompileUnit GetServiceCompileUnit(string webServiceUrl, string nameSpaceName)
            {
                try
                {
                    WebClient client = new WebClient();
                    Stream stream = client.OpenRead(webServiceUrl);

                    ServiceDescription description = ServiceDescription.Read(stream);

                    ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
                    importer.ProtocolName = "Soap";
                    importer.Style = ServiceDescriptionImportStyle.Client;
                    importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
                    importer.AddServiceDescription(description, null, null);

                    CodeNamespace nmspace = new CodeNamespace();
                    nmspace.Name = nameSpaceName;

                    CodeCompileUnit unit = new CodeCompileUnit();

                    unit.Namespaces.Add(nmspace);
                    ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);

                    return unit;
                }
                catch (Exception)
                {
                    return null;
                }
            }

     private  CompilerResults Compile(CodeCompileUnit unit)
            {
                try
                {
                    CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp");
                    CompilerParameters compilerParameters = new CompilerParameters();
                    compilerParameters.GenerateExecutable = false;
                    compilerParameters.GenerateInMemory = true;

                    CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, unit);
                    if (compilerResults.Errors.HasErrors)
                    {
                        return null;
                    }

                    return compilerResults;
                }
                catch (Exception)
                {
                    return null;
                }
            }

    public object InvokeWebService(string ServiceUrl,string namespace,string ClassName,string interfaceName,object[]

    parms)
            {
                try
                {
                    CodeCompileUnit unit = GetServiceCompileUnit(ServiceUrl, namespace);
                    if (unit == null)
                    {
                        return null;
                    }

                    // Compile
                    CompilerResults result = Compile(unit);
                    if (result == null)
                    {
                        return null;
                    }

                    // Call SP interface
                    Assembly asm = result.CompiledAssembly;
                    Type t = asm.GetType(NameSpace + "." + ClassName);
                    object o = Activator.CreateInstance(t);
                    MethodInfo method = t.GetMethod(interfaceName);
                    object item = method.Invoke(o, parms);
                    return item;
                }
                catch (Exception)
                {
                    return null;
                }
            }

  • 相关阅读:
    sublime去除空白行和重复行
    python list删除数据 和复制 列表
    微博实现简繁体转换
    2017.10.27日面试总结
    python 类和__class__理解
    python 单例模式应用
    pt-query-digest 慢日志监控
    在线安全清空慢查询日志slowlog
    Linux高级系统恢复技术
    灾备演练
  • 原文地址:https://www.cnblogs.com/xiao123/p/2554890.html
Copyright © 2020-2023  润新知