gpt4 book ai didi

c# - ServiceContractGenerator CodeDomProvider 编译错误

转载 作者:行者123 更新时间:2023-11-30 16:09:03 29 4
gpt4 key购买 nike

我正在尝试使用 ServiceCodeGenerator 和 CodeDomProvider 动态创建服务引用。使用 CodeDomProvider 编译代码时会抛出以下错误。

看起来它只适用于特定的网络服务。我能够编译其他 Web 服务,但是这个会抛出下面的编译错误。

知道如何编辑源代码或忽略错误吗?

CS0579: Duplicate 'System.CodeDom.Compiler.GeneratedCodeAttribute' attribute 99 CS0579: Duplicate 'System.Diagnostics.DebuggerStepThroughAttribute' attribute 101 CS0579: Duplicate 'System.CodeDom.Compiler.GeneratedCodeAttribute' attribute 191 CS0579: Duplicate 'System.Diagnostics.DebuggerStepThroughAttribute' attribute 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);

现在我们有了一个完整的“添加服务引用”

关于c# - ServiceContractGenerator CodeDomProvider 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28218044/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com