ServiceContractGenerator CodeDomProvider编译错误

时间:2015-01-29 15:13:10

标签: c# web-services soap reflection

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

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

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

  

CS0579:复制' System.CodeDom.Compiler.GeneratedCodeAttribute'   属性99 CS0579:重复   ' System.Diagnostics.DebuggerStepThroughAttribute'属性101   CS0579:Duplicate' 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");
        }           

2 个答案:

答案 0 :(得分:3)

问题是生成器使用自己的CodeCompileUnit,并生成具有相同名称的类。有必要强制它使用WsdlImporter使用的CodeCompileUnit,例如(花一天时间查找):

Collection<ContractDescription> contracts = importerWsdl.ImportAllContracts();
ServiceEndpointCollection allEndpoints = importerWsdl.ImportAllEndpoints();
CodeCompileUnit unit = (CodeCompileUnit) importerWsdl.State[typeof (CodeCompileUnit)];
ServiceContractGenerator generator = new ServiceContractGenerator(unit);

现在我们已经完全相当于&#34;添加服务参考&#34;

答案 1 :(得分:0)

您可能需要从 WsdlImporter

明确删除 XmlSerializerMessageContractImporter 扩展名

试试这个..

        var wsdlImporter = new WsdlImporter(metadataSet);

        // when you initialize the WsdlImporter with a 'MetadataSet', i
        // the WsdlImporter.WsdlImportExtensions will already be 
        // loaded with 7 or so extensions.
        // one of them being, XmlSerializerMessageContractImporter
        // Removing this extension seems to be solving the problem of 
        // Duplicate Data Contracts
        // and Compile errors on Duplicate 'GeneratedCode' attribute etc.

        wsdlImporter.WsdlImportExtensions
                    .Remove(typeof(XmlSerializerMessageContractImporter));