动态网络服务客户端

Dynamic Web Service Client

我写了一个动态网络服务客户端。这适用于 SOAP 1.1 但不适用于 SOAP 1.2

当我使用 ServiceDescriptionImporter.Import 时,我收到以下警告:

OptionalExtensionsIgnored

下面是准备网络服务的代码:

 using (var client = new WebClient())
        {
            //Trust all certificates
            System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
            client.Credentials = new NetworkCredential(@"domain\user","password");
            using (var stream = client.OpenRead(url))
            {
                // Get a WSDL file describing the service.
                ServiceDescription description = ServiceDescription.Read(stream);

                // Initialize a service description importer.
                ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
                importer.ProtocolName = webServiceProtocol.ToString();
                importer.Style = ServiceDescriptionImportStyle.Client;
                importer.AddServiceDescription(description, null, null);

                // Report on the service descriptions.
                Console.WriteLine("Importing {0} service descriptions with {1} associated schemas.",
                                  importer.ServiceDescriptions.Count, importer.Schemas.Count);

                // Add any imported files
                foreach (System.Xml.Schema.XmlSchema wsdlSchema in description.Types.Schemas)
                {
                    foreach (System.Xml.Schema.XmlSchemaObject externalSchema in wsdlSchema.Includes)
                    {
                        if (externalSchema is System.Xml.Schema.XmlSchemaImport)
                        {
                            Uri baseUri = new Uri(url);
                            Uri schemaUri = new Uri(baseUri, ((System.Xml.Schema.XmlSchemaExternal)externalSchema).SchemaLocation);
                            using (var schemaStream = client.OpenRead(schemaUri))
                            {
                                System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(schemaStream, null);
                                importer.Schemas.Add(schema);
                            }
                            Console.WriteLine(((System.Xml.Schema.XmlSchemaExternal)externalSchema).SchemaLocation);
                        }
                    }
                }

                // 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 nmspace = new CodeNamespace();
                CodeCompileUnit unit1 = new CodeCompileUnit();
                unit1.Namespaces.Add(nmspace);

                // Import the service into the Code-DOM tree. This creates proxy code
                // that uses the service.
                ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);
                Console.WriteLine("Warning: " + warning);

                if (warning == 0 || warning == ServiceDescriptionImportWarnings.OptionalExtensionsIgnored)
                {
                    // Generate and print the proxy code in C#.
                    CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");

                    // Compile the assembly with the appropriate references
                    string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
                    CompilerParameters parms = new CompilerParameters(assemblyReferences);
                    CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);
                    assembly = results.CompiledAssembly;

                    foreach (CompilerError oops in results.Errors)
                    {
                        Console.WriteLine("========Compiler error============");
                        Console.WriteLine(oops.ErrorText);
                    }

                }
                else
                {
                    // Print an error message.
                    Console.WriteLine("Warning: " + warning);
                }
            }
        }

如果我忽略警告并使用 CodeDomProvider 编译代码,它编译时没有错误。问题是当我从 Web 服务调用一个方法时,出现以下错误:

Exception has been thrown by the target of an invocation. SOAP header Action was not understood.

调用方法的代码如下:

 //Invoke the web service method
        object service = GetAssembly().CreateInstance("BizTalkServiceInstance");
        Type serviceType = service.GetType();
        PropertyInfo propInfo = serviceType.GetProperty("Credentials");
        propInfo.SetValue(service, new NetworkCredential("user", "pass", "domain"), null);

        object request = GetObjectFromString(requestName, requestValue);

        object response = serviceType.InvokeMember(methodName, System.Reflection.BindingFlags.InvokeMethod, null, service, new object[] { request });
        Console.WriteLine(GetValueFromObject(responseName,response));
        Console.ReadLine();
        return null;

我真的不知道我错过了什么。

看来我是用老方法做的。我已经切换到 ServiceContractGenerator。这现在创建了一个服务客户端对象,它提供了一个允许您设置绑定的构造函数。

它现在在我的一项服务上抛出编译错误,但至少我的 1.1 和 1.2 服务都在工作。

你可以在这里看到代码和我的新方法的问题: ServiceContractGenerator CodeDomProvider Compile Errors