zoukankan      html  css  js  c++  java
  • 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;
                }
            }

  • 相关阅读:
    Python len() 方法
    Python join() 方法
    Python isupper() 方法
    使用quartz进行容器启动时登陆接口服务器和接口服务器进行心跳连接
    实现锁死的有滚动条的div的表格(datagird)
    使用spring的事务的三种方法
    webservice系统学习笔记7-使用handler实现过滤器/拦截器效果
    webservice系统学习笔记7-异常处理
    webservice系统学习笔记6-使用soap的header传递消息
    JSTL fmt:formatNumber 数字、货币格式化
  • 原文地址:https://www.cnblogs.com/xiao123/p/2554890.html
Copyright © 2011-2022 走看看