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:
Any idea how I can get around this issue?
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:
object proxyInstance = null;
// Define the WSDL Get address, contract name and parameters, with this we can extract WSDL details any time
Uri address = new Uri("url");
// For HttpGet endpoints use a Service WSDL address a mexMode of .HttpGet and for MEX endpoints use a MEX address and a mexMode of .MetadataExchange
MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;
string contractName = "IService1";
// Get the metadata file from the service.
MetadataExchangeClient metadataExchangeClient = new MetadataExchangeClient(address, mexMode);
metadataExchangeClient.ResolveMetadataReferences = true;
//Trust all certificates
System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
//One can also provide credentials if service needs that by the help following two lines.
ICredentials networkCredential = new NetworkCredential("user", "pass", "domain");
metadataExchangeClient.HttpCredentials = networkCredential;
//Gets the meta data information of the service.
MetadataSet metadataSet = metadataExchangeClient.GetMetadata();
// Import all contracts and endpoints.
WsdlImporter wsdlImporter = new WsdlImporter(metadataSet);
//Import all contracts.
Collection<ContractDescription> contracts = wsdlImporter.ImportAllContracts();
//Import all end points.
ServiceEndpointCollection allEndpoints = wsdlImporter.ImportAllEndpoints();
// Generate type information for each contract.
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator();
//Dictinary has been defined to keep all the contract endpoints present, contract name is key of the dictionary item.
var endpointsForContracts = new Dictionary<string, IEnumerable<ServiceEndpoint>>();
foreach (ContractDescription contract in contracts)
{
serviceContractGenerator.GenerateServiceContractType(contract);
// Keep a list of each contract's endpoints.
endpointsForContracts[contract.Name] = allEndpoints.Where(ep => ep.Contract.Name == contract.Name).ToList();
}
// Generate a code file for the contracts.
CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions();
codeGeneratorOptions.BracingStyle = "C";
// Create Compiler instance of a specified language.
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 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)
Aucun commentaire:
Enregistrer un commentaire