代码改变世界

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

2012-04-23 11:32  Eric.Hu  阅读(1845)  评论(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