- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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/
我正在尝试使用 ServiceCodeGenerator 和 CodeDomProvider 动态创建服务引用。使用 CodeDomProvider 编译代码时会抛出以下错误。 看起来它只适用于特定的
有一个specific WSDL为此,ServiceContractGenerator 不断生成消息契约(Contract)(请求/响应包装器对象),这是我不想要的(我想要直接参数)。其他 WSDL
我是一名优秀的程序员,十分优秀!