我正在尝试使用ServiceCodeGenerator和CodeDomProvider动态创建服务引用。使用CodeDomProvider编译代码时,它将引发以下错误。
看起来它仅适用于特定的Web服务。我可以编译其他Web服务,但这会引发以下编译错误。
知道如何编辑源代码或忽略错误吗?
CS0579:重复的'System.CodeDom.Compiler.GeneratedCodeAttribute'
属性99 CS0579:重复
'System.Diagnostics.DebuggerStepThroughAttribute'属性101
CS0579:重复的'System.CodeDom.Compiler.GeneratedCodeAttribute'
属性191 CS0579:重复
'System.Diagnostics.DebuggerStepThroughAttribute'属性193
代码如下:
Uri address = new Uri(url + "?wsdl");
MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;
MetadataExchangeClient metadataExchangeClient = new MetadataExchangeClient(address, mexMode);
metadataExchangeClient.ResolveMetadataReferences = true;
//Trust all certificates
System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
ICredentials networkCredential = new NetworkCredential("username", "password", "domain");
metadataExchangeClient.HttpCredentials = networkCredential;
MetadataSet metadataSet = metadataExchangeClient.GetMetadata();
WsdlImporter wsdlImporter = new WsdlImporter(metadataSet);
Collection<ContractDescription> contracts = wsdlImporter.ImportAllContracts();
ServiceEndpointCollection allEndpoints = wsdlImporter.ImportAllEndpoints();
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator();
foreach (ContractDescription contract in contracts)
{
serviceContractGenerator.GenerateServiceContractType(contract);
}
// Generate a code file for the contracts.
CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions();
codeGeneratorOptions.BracingStyle = "C";
// Create Compiler instance of a specified language.
CompilerResults compilerResults;
using (CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp"))
{
// Adding WCF-related assemblies references as copiler parameters, so as to do the compilation of particular service contract.
CompilerParameters compilerParameters = new CompilerParameters(new string[] { "System.dll", "System.ServiceModel.dll", "System.Runtime.Serialization.dll" });
compilerParameters.GenerateInMemory = true;
compilerParameters.WarningLevel = 1;
compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, serviceContractGenerator.TargetCompileUnit);
}
if (compilerResults.Errors.Count <= 0)
{
assembly = compilerResults.CompiledAssembly;
}
else
{
foreach (CompilerError error in compilerResults.Errors)
{
Console.WriteLine(error.ErrorNumber + ": " + error.ErrorText + " " + error.IsWarning + " " + error.Line);
}
throw new Exception("Compiler Errors - unable to build Web Service Assembly");
}
最佳答案
问题在于生成器使用其自己的CodeCompileUnit,并生成具有相同名称的类。例如,有必要强迫它使用WsdlImporter使用的CodeCompileUnit(花一天时间找出来):
Collection<ContractDescription> contracts = importerWsdl.ImportAllContracts();
ServiceEndpointCollection allEndpoints = importerWsdl.ImportAllEndpoints();
CodeCompileUnit unit = (CodeCompileUnit) importerWsdl.State[typeof (CodeCompileUnit)];
ServiceContractGenerator generator = new ServiceContractGenerator(unit);
现在,我们有一个完全等效的“添加服务参考”