C# ServiceDescriptionImporter NoCodeGenerated 我找不到问题
C# ServiceDescriptionImporter NoCodeGenerated i cant find the issue
嘿,我 运行 遇到了 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;
}
你分享的信息不够,我会尽力解答。
协议可能是问题所在,我遇到了类似的问题,删除这一行解决了问题
importer.ProtocolName = "Soap12";
因此,就我而言,我尝试使用错误的协议设置连接到 Soap 1.1 服务。
Soap 1.1 是默认值。
请检查文档:
嘿,我 运行 遇到了 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;
}
你分享的信息不够,我会尽力解答。
协议可能是问题所在,我遇到了类似的问题,删除这一行解决了问题
importer.ProtocolName = "Soap12";
因此,就我而言,我尝试使用错误的协议设置连接到 Soap 1.1 服务。 Soap 1.1 是默认值。
请检查文档: