I am trying to use ServiceCodeGenerator and CodeDomProvider to dynamically create a service reference. When compiling the code using CodeDomProvider it throws the following errors.
It looks like it is only for a specific web service. I am able to compile other web services but this one throws the compile errors below.
Any idea how I can edit the source code or ignore the errors?
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
The code is below:
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");
}
The problem is that the generator uses its own CodeCompileUnit, and generates classes with same name . It is necessary to force it to use CodeCompileUnit that used by WsdlImporter, for example (Spent a day to find out):
Collection<ContractDescription> contracts = importerWsdl.ImportAllContracts();
ServiceEndpointCollection allEndpoints = importerWsdl.ImportAllEndpoints();
CodeCompileUnit unit = (CodeCompileUnit) importerWsdl.State[typeof (CodeCompileUnit)];
ServiceContractGenerator generator = new ServiceContractGenerator(unit);
And now we have a full equivalent of "Add Service Reference"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With