代码改变世界

一个容易忽略的问题-- 调用ServiceDescriptionImporter出错

  Eric.Hu  阅读(1858)  评论(0编辑  收藏  举报

最近在研究动态调用WEBService的方法,其中一个地方要用到ServiceDescriptionImporter

System.Web.Services.Description.ServiceDescriptionImporter

 命名空间:  System.Web.Services.Description
程序集:  System.Web.Services(在 System.Web.Services.dll 中)

 

问题在于我已经添加了System.Web.Services.Description的引用,但编辑器依然无法识别该类,我用的是vs2010,.net framework 4.0;

查找问题,跟踪问题,结果发现问题在我用的是.net framework 4.0 client profile,更改为.net framework 4.0 问题就处理了.

 

符ServiceDescriptionImporter使用参考:

 

复制代码
 1 using System;
 2 using System.Web.Services.Description;
 3 using System.CodeDom;
 4 using System.CodeDom.Compiler;
 5 using System.Security.Permissions;
 6 
 7 public class Import {
 8 
 9     public static void Main() 
10     {
11         Run();
12     }
13 
14     [PermissionSetAttribute(SecurityAction.Demand, Name = "Full Trust")]
15     public static void Run()
16     {
17     // Get a WSDL file describing a service.
18     ServiceDescription description = ServiceDescription.Read("service.wsdl");
19 
20     // Initialize a service description importer.
21     ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
22     importer.ProtocolName = "Soap12";  // Use SOAP 1.2.
23     importer.AddServiceDescription(description,null,null);
24 
25     // Report on the service descriptions.
26     Console.WriteLine("Importing {0} service descriptions with {1} associated schemas.",
27                       importer.ServiceDescriptions.Count, importer.Schemas.Count);
28 
29     // Generate a proxy client.
30     importer.Style = ServiceDescriptionImportStyle.Client;
31 
32     // Generate properties to represent primitive values.
33     importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
34 
35     // Initialize a Code-DOM tree into which we will import the service.
36     CodeNamespace nmspace = new CodeNamespace();
37     CodeCompileUnit unit = new CodeCompileUnit();
38     unit.Namespaces.Add(nmspace);
39 
40     // Import the service into the Code-DOM tree. This creates proxy code
41     // that uses the service.
42     ServiceDescriptionImportWarnings warning = importer.Import(nmspace,unit);
43 
44     if (warning == 0)
45     {
46         // Generate and print the proxy code in C#.
47         CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
48         provider.GenerateCodeFromCompileUnit(unit, Console.Out, new CodeGeneratorOptions() );
49     }
50     else
51     {
52         // Print an error message.
53         Console.WriteLine(warning); 
54     }
55 }
56 
57 
58 }
复制代码

 

 

http://msdn.microsoft.com/zh-cn/library/system.web.services.description.servicedescriptionimporter.aspx

 

 

 

 

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
历史上的今天:
2010-04-23 雨过天晴——一系列问题
点击右上角即可分享
微信分享提示