Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过传递方法名称和参数并在不添加web引用的情况下获得响应来使用wsdl url?_C#_Asp.net_Web Services_Wsdl - Fatal编程技术网

C# 通过传递方法名称和参数并在不添加web引用的情况下获得响应来使用wsdl url?

C# 通过传递方法名称和参数并在不添加web引用的情况下获得响应来使用wsdl url?,c#,asp.net,web-services,wsdl,C#,Asp.net,Web Services,Wsdl,导入时在上述代码中: using System; using System.Collections.Generic; using System.Linq; using System.Text; //Added for samplecode using System.CodeDom.Compiler; using System.Security.Permissions; using System.Web.Services.Description; using System.Reflection;

导入时在上述代码中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Added for samplecode
using System.CodeDom.Compiler;
using System.Security.Permissions;
using System.Web.Services.Description;
using System.Reflection;
using System.CodeDom;
using System.Diagnostics;

namespace DynamicSoap
{
    public static class DynamicWebService
    {
        public static Object CallWebService(string webServiceAsmxUrl, string serviceName, string methodName, object[] args)
        {
            try
            {
                System.Net.WebClient client = new System.Net.WebClient();

                //-Connect To the web service
                System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");

                //Read the WSDL file describing a service.
                ServiceDescription description = ServiceDescription.Read(stream);

                //Load the DOM

                //--Initialize a service description importer.
                ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
                importer.ProtocolName = "Soap12"; //Use SOAP 1.2.
                importer.AddServiceDescription(description, null, null);

                //--Generate a proxy client. 
                importer.Style = ServiceDescriptionImportStyle.Client;

                //--Generate properties to represent primitive values.
                importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

                //Initialize a Code-DOM tree into which we will import the service.
                CodeNamespace codenamespace = new CodeNamespace();
                CodeCompileUnit codeunit = new CodeCompileUnit();
                codeunit.Namespaces.Add(codenamespace);

                //Import the service into the Code-DOM tree. 
                //This creates proxy code that uses the service.

                ServiceDescriptionImportWarnings warning = importer.Import(codenamespace, codeunit);

                if (warning == 0)
                {
                    //--Generate the proxy code
                    CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

                    //--Compile the assembly proxy with the 
                    // appropriate references
                    string[] assemblyReferences = new string[] {
                        "System.dll", 
                        "System.Web.Services.dll", 
                        "System.Web.dll", 
                        "System.Xml.dll", 
                        "System.Data.dll"
                    };

                    //--Add parameters
                    CompilerParameters parms = new CompilerParameters(assemblyReferences);
                    parms.GenerateInMemory = true; //(Thanks for this line nikolas)
                    CompilerResults results = provider.CompileAssemblyFromDom(parms, codeunit);

                    //--Check For Errors
                    if (results.Errors.Count > 0)
                    {
                        foreach (CompilerError oops in results.Errors)
                        {
                            System.Diagnostics.Debug.WriteLine("========Compiler error============");
                            System.Diagnostics.Debug.WriteLine(oops.ErrorText);
                        }

                        throw new Exception("Compile Error Occured calling WebService.");
                    }

                    //--Finally, Invoke the web service method
                    Object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
                    MethodInfo mi = wsvcClass.GetType().GetMethod(methodName);
                    return mi.Invoke(wsvcClass, args);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
警告应该是0,然后只有它会进入if语句中。但我正在得到代码生成


我想不出是什么问题。有人能解释一下吗?

请帮助别人?有人能提出更好的方法吗??
ServiceDescriptionImportWarnings warning = importer.Import(codenamespace, codeunit);