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;
            }
        }

原文地址:https://www.cnblogs.com/xiao123/p/2554890.html