webserver动态调用类

一、异步调用:

 1      try

 2             {

 3 svrMonitor sm = new svrMonitor();

 4  sm.Url = sm.Url;

 5 sm.GetRecordsCompleted += new GetRecordsCompletedEventHandler(GetRecordsCompleted);

 6 sm.GetRecordsAsync();

 7             }

 8             catch (Exception ex)

 9             {

10 //异常不会出现在这里的哦

11                 MessageBox.Show("无法连接到服务器",可能网络异常或服务未启动 。", "系统提示");

12             }

13  

14  void GetRecordsCompleted(object sender, GetRecordsCompletedCompletedEventArgs e)

15 {

16          try

17             {

18 //dosomeing

19                }

20             catch (Exception ex)

21             {

22 //注意 :异常是在这里捕捉到的。

23                 MessageBox.Show("无法连接到服务器",可能网络异常或服务未启动 。", "系统提示");

24             }

25 

26 }

 

 
 
动态调用类:
 1 private void Page_Load(object sender, System.EventArgs e)

 2   {

 3    // 在此处放置用户代码以初始化页面

 4    object   obj   =   InvokeWebservice("http://localhost/getPro.asmx","EtoonProduct.web.manage","getPro","getArea",new   object[]{"1"}); 

 5    this.DataGrid1.DataSource=obj;

 6    this.DataGrid1.DataBind();

 7   } 

 8   /**//// 根据指定的信息,调用远程WebService方法 

 9   /// 

10   /// WebService的http形式的地址 

11   /// 欲调用的WebService的命名空间 

12   /// 欲调用的WebService的类名(不包括命名空间前缀) 

13   /// 欲调用的WebService的方法名 

14   /// 参数列表 

15   /// WebService的执行结果 

16   /// 

17   /// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。 

18   /// 异常信息可能会发生在两个地方: 

19   /// 1、动态构造WebService的时候,CompileAssembly失败。 

20   /// 2、WebService本身执行失败。 

21   /// 

22   /// 

23   /// 

24   /// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx","Genersoft.Platform.Service.Workflow","Common","GetToolType",new object[]{"1"}); 

25   /// 

26   /// 

27   private object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args) 

28   { 

29    try 

30    { 

31     System.Net.WebClient wc = new System.Net.WebClient(); 

32     System.IO.Stream stream = wc.OpenRead(url+"?WSDL"); 

33     System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream); 

34     System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter(); 

35     sdi.AddServiceDescription(sd,"",""); 

36     System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace); 

37     System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit(); 

38     ccu.Namespaces.Add(cn); 

39     sdi.Import(cn,ccu); 

40 

41     Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider(); 

42     System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler(); 

43 

44     System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters(); 

45     cplist.GenerateExecutable = false; 

46     cplist.GenerateInMemory = true; 

47     cplist.ReferencedAssemblies.Add("System.dll"); 

48     cplist.ReferencedAssemblies.Add("System.XML.dll"); 

49     cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); 

50     cplist.ReferencedAssemblies.Add("System.Data.dll"); 

51 

52     System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); 

53     if(true == cr.Errors.HasErrors) 

54     { 

55      System.Text.StringBuilder sb = new System.Text.StringBuilder(); 

56      foreach(System.CodeDom.Compiler.CompilerError ce in cr.Errors) 

57      { 

58       sb.Append(ce.ToString()); 

59       sb.Append(System.Environment.NewLine); 

60      } 

61      throw new Exception(sb.ToString()); 

62     } 

63     System.Reflection.Assembly assembly = cr.CompiledAssembly; 

64     Type t = assembly.GetType(@namespace+"."+classname,true,true); 

65     object obj = Activator.CreateInstance(t); 

66     System.Reflection.MethodInfo mi = t.GetMethod(methodname); 

67     return mi.Invoke(obj,args); 

68    } 

69    catch(Exception ex) 

70    { 

71     throw new Exception(ex.InnerException.Message,new Exception(ex.InnerException.StackTrace)); 

72    } 

73   }

 

你可能感兴趣的:(webserver)