【问题标题】:C# ServiceDescriptionImporter NoCodeGenerated i cant find the issueC# ServiceDescriptionImporter NoCodeGenerated 我找不到问题
【发布时间】:2019-07-21 07:18:14
【问题描述】:

嘿,我遇到了 ServiceDescription 导入器的情况。

在代码(粘贴在下面)中,我正在尝试从 WSDL 文件(本地存储)导入和编译服务引用,遗憾的是,由于客户的影响,我不允许共享此 wsdl 文件。

但是,所有代码都运行良好,但是以下行:

ServiceDescriptionImportWarnings warnings = importer.Import(nm, unit);

不断返回:ServiceDescriptionImportWarnings.NoCodeGenerated

有人能告诉我这段代码是否以及哪里出错了吗?

public string[] GenerateProxyAssembly(string pathOrURL)
{
    Stream stream = File.OpenRead(pathOrURL);
    ServiceDescription desc = ServiceDescription.Read(stream);

    //find out the number of operations exposed by the web service
    //store the name of the operations inside the string array
    //iterating only through the first binding exposed as
    //the rest of the bindings will have the same number

    int i = 0;
    Binding binding = desc.Bindings[0];
    OperationBindingCollection opColl = binding.Operations;
    string[] listOfOperations = new string[opColl.Count];

    foreach (OperationBinding operation in opColl)
    {
        listOfOperations[i++] = operation.Name;
    }

    //initializing a ServiceDescriptionImporter object
    ServiceDescriptionImporter importer = new ServiceDescriptionImporter();

    //set the protocol to SOAP 1.1
    importer.ProtocolName = "Soap12";

    //setting the Style to Client in order to generate client proxy code
    importer.Style = ServiceDescriptionImportStyle.Client;

    //adding the ServiceDescription to the Importer object
    importer.AddServiceDescription(desc, null, null);
    importer.CodeGenerationOptions = CodeGenerationOptions.GenerateNewAsync;

    //Initialize the CODE DOM tree in which we will import the 
    //ServiceDescriptionImporter
    CodeNamespace nm = new CodeNamespace("test");
    CodeCompileUnit unit = new CodeCompileUnit();
    unit.Namespaces.Add(nm);

    //generating the client proxy code
    ServiceDescriptionImportWarnings warnings = importer.Import(nm, unit);

    if (warnings == 0)
    {
        //set the CodeDOMProvider to C# to generate the code in C#
        System.IO.StringWriter sw = new System.IO.StringWriter();
        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
        provider.GenerateCodeFromCompileUnit(unit, sw, new CodeGeneratorOptions());

        //creating TempFileCollection
        //the path of the temp folder is hardcoded
        TempFileCollection coll = new TempFileCollection(@"C:\wmpub\tempFiles");
        coll.KeepFiles = false;

        //setting the CompilerParameters for the temporary assembly
        string[] refAssembly = { "System.dll", "System.Data.dll",
                                         "System.Web.Services.dll", "System.Xml.dll" };
        CompilerParameters param = new CompilerParameters(refAssembly);
        param.GenerateInMemory = true;
        param.TreatWarningsAsErrors = false;
        param.OutputAssembly = "WebServiceReflector.dll";
        param.TempFiles = coll;

        //compile the generated code into an assembly
        //CompilerResults results = provider.CompileAssemblyFromDom(param, unitArr);
        CompilerResults results = provider.CompileAssemblyFromSource(param, sw.ToString());
        this.assem = results.CompiledAssembly;
    }

    //return the list of operations exposed by the web service
    return listOfOperations;
}

【问题讨论】:

    标签: c# dynamic wsdl service-reference


    【解决方案1】:

    你没有分享足够的信息,但我会尽力回答。

    协议可能是问题,我有类似的问题,删除此行解决了问题

    importer.ProtocolName = "Soap12";
    

    所以在我的情况下,我试图以错误的协议设置连接到 Soap 1.1 服务。 肥皂 1.1 是默认值。

    请查看文档:

    https://docs.microsoft.com/en-us/dotnet/api/system.web.services.description.servicedescriptionimporter.protocolname?view=netframework-4.8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      相关资源
      最近更新 更多