使用MEX客户端消费WSDL时CodeDom编译遇CS0246错误求助
问题:使用MEX客户端消费WSDL时编译错误CS0246:找不到类型或命名空间'schema'
尝试使用MEX客户端消费WSDL,期望无需引用即可编译并使用,但遇到编译器错误:CS0246: 找不到类型或命名空间名称'schema'。
导入契约的代码
MetadataExchangeClient mexClient = new MetadataExchangeClient(binding); mexClient.ResolveMetadataReferences = true; mexClient.MaximumResolvedReferences = 1000; var metaSet = mexClient.GetMetadata(mexAddress, mexMode); WsdlImporter importer = new WsdlImporter(metaSet); result.ContractDescriptions = importer.ImportAllContracts(); result.AllEndpoints = importer.ImportAllEndpoints();
生成并编译代码的逻辑
CodeCompileUnit unit = (CodeCompileUnit)importer.State[typeof(CodeCompileUnit)]; ServiceContractGenerator generator = new ServiceContractGenerator(unit); foreach (ContractDescription contract in result.ContractDescriptions) { generator.GenerateServiceContractType(contract); } // Generate a code file for the contracts. CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions(); codeGeneratorOptions.BracingStyle = "C"; // Create Compiler instance of a specified language. CompilerResults results; using (CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp")) { CompilerParameters compilerParameters = new CompilerParameters(new string[] { "System.dll", "System.Data.dll", "System.ServiceModel.dll", "System.Runtime.Serialization.dll", "System.Xml.dll", "System.Xml.Linq.dll", "System.Xml.Serialization.dll" }); compilerParameters.GenerateInMemory = true; compilerParameters.GenerateExecutable = false; compilerParameters.WarningLevel = 1; compilerParameters.TempFiles = new TempFileCollection("Compile", true); results = codeDomProvider.CompileAssemblyFromDom(compilerParameters, generator.TargetCompileUnit); }
生成代码中的错误点
编译后查看生成的代码,发现TestDS类中出现了未定义的schema类型:
/// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("BfusTester", "1.0.0.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.logica.com/BusinessForUtilities/2012/LoggerService")] public partial class TestDS { private schema schemaField; private System.Xml.XmlElement anyField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace="http://www.w3.org/2001/XMLSchema", Order=0)] public schema schema { get { return this.schemaField; } set { this.schemaField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAnyElementAttribute(Order=1)] public System.Xml.XmlElement Any { get { return this.anyField; } set { this.anyField = value; } } }
服务端对应代码(无法修改)
该方法对应服务端的标准System.Data.DataSet接口与实现:
接口代码
[OperationContract] DataSet Test(DataSet ds);
实现代码
public DataSet Test(DataSet ds) { return new DataSet(); }
WSDL中的关键细节
引发问题的DataSet方法在WSDL中使用了"literal"类型:
<wsdl:operation name="Test2"> <soap12:operation soapAction="http://www.logica.com/BusinessForUtilities/2012/LoggerService/ILoggerService/Test2" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation>
需求推测与求助
推测需要指定WsdlImporter或ServiceContractGenerator查找DataSet类型的位置,但不知具体操作方式,恳请提供解决方案。
内容的提问来源于stack exchange,提问作者Pierre Lundgren
相关产品推荐
相关产品推荐

