ServiceDescriptionImporter返回NoCodeGenerated问题排查请求
ServiceDescriptionImportWarnings.NoCodeGenerated When Using ServiceDescriptionImporter Let’s break down the potential issues in your code and walk through how to diagnose why no proxy code is being generated:
1. Mismatched SOAP Protocol Version
You’ve set importer.ProtocolName = "Soap12";, but if your local WSDL only defines SOAP 1.1 bindings, the importer won’t find a matching protocol to generate code for.
Try switching to SOAP 1.1 by updating that line to:
importer.ProtocolName = "Soap"; // For SOAP 1.1 compatibility
If that doesn’t work, you can verify the SOAP version supported by your WSDL by inspecting the Binding object’s BindingElements — look for a SoapBinding entry and check its Namespace property (SOAP 1.1 uses http://schemas.xmlsoap.org/soap/, SOAP 1.2 uses http://www.w3.org/2003/05/soap/bindings/HTTP/).
2. Add Warning Logging to Get Detailed Errors
The NoCodeGenerated warning is vague, but you can hook into the importer’s Warning event to capture specific messages about why code isn’t being generated. Add this before calling importer.Import:
importer.Warning += (sender, args) => { Console.WriteLine($"Import Warning: {args.Message}"); // You could also write this to a log file if needed };
This will print exact issues like missing port types, mismatched bindings, or unsupported WSDL features that are blocking code generation.
3. Validate the Loaded ServiceDescription
Before proceeding with the import, double-check that your WSDL loaded correctly and contains valid content:
// Add these checks right after reading the WSDL if (desc.PortTypes.Count == 0) throw new InvalidOperationException("WSDL contains no port types."); if (desc.Bindings.Count == 0) throw new InvalidOperationException("WSDL contains no bindings."); // Ensure the first binding references an existing port type var targetPortType = desc.PortTypes[binding.Type.Name]; if (targetPortType == null) throw new InvalidOperationException($"Binding {binding.Name} references a non-existent port type.");
If any of these checks fail, it means your WSDL is incomplete or malformed, which explains why no code can be generated.
4. Expand Code Generation Options
Restricting yourself to only CodeGenerationOptions.GenerateNewAsync might be missing required flags for full proxy generation. Try adding common options:
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateNewAsync | CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateSerializable;
Modified Code with Diagnostics
Here’s your code updated with the above fixes and diagnostic tools:
public string[] GenerateProxyAssembly(string pathOrURL) { using Stream stream = File.OpenRead(pathOrURL); ServiceDescription desc = ServiceDescription.Read(stream); // Validate WSDL content if (desc.PortTypes.Count == 0) throw new InvalidOperationException("WSDL has no port types defined."); if (desc.Bindings.Count == 0) throw new InvalidOperationException("WSDL has no bindings defined."); // Extract operation names Binding binding = desc.Bindings[0]; OperationBindingCollection opColl = binding.Operations; string[] listOfOperations = new string[opColl.Count]; int i = 0; foreach (OperationBinding operation in opColl) { listOfOperations[i++] = operation.Name; } ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); // Try SOAP 1.1 first if SOAP 1.2 isn't working importer.ProtocolName = "Soap"; importer.Style = ServiceDescriptionImportStyle.Client; importer.AddServiceDescription(desc, null, null); importer.CodeGenerationOptions = CodeGenerationOptions.GenerateNewAsync | CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateSerializable; // Log import warnings to diagnose issues importer.Warning += (sender, args) => { Console.WriteLine($"Import Warning: {args.Message}"); }; CodeNamespace nm = new CodeNamespace("test"); CodeCompileUnit unit = new CodeCompileUnit(); unit.Namespaces.Add(nm); ServiceDescriptionImportWarnings warnings = importer.Import(nm, unit); Console.WriteLine($"Import Warnings: {warnings}"); // Log the warning flag if (warnings == 0) { using System.IO.StringWriter sw = new System.IO.StringWriter(); CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); provider.GenerateCodeFromCompileUnit(unit, sw, new CodeGeneratorOptions()); TempFileCollection coll = new TempFileCollection(@"C:\wmpub\tempFiles"); coll.KeepFiles = false; string[] refAssembly = { "System.dll", "System.Data.dll", "System.Web.Services.dll", "System.Xml.dll" }; CompilerParameters param = new CompilerParameters(refAssembly); param.GenerateInMemory = true; param.TreatWarningsAsErrors = false; param.OutputAssembly = "WebServiceReflector.dll"; param.TempFiles = coll; CompilerResults results = provider.CompileAssemblyFromSource(param, sw.ToString()); this.assem = results.CompiledAssembly; } return listOfOperations; }
Run this modified code and check the console output — the warning messages should directly point you to the issue preventing code generation.
内容的提问来源于stack exchange,提问作者CallMeBackdraft

