动态调用Webservice

动态调用Webservice:

说明:要运行此程序,必须先有配置文件(如果是web程序。配置文件就是Web.config;如果是windows程序就是app.config)至于如何把这两个文件加入工程中请自行搜索“配置文件”。  
  打开配置文件,在紧挨这</configuration>的上方添加如下代码:  
      <appSettings>  
          <add   key="WSDL"   value="提供web服务说明的网址,例如:  http://localhost/DynamicWebService/WSReturnString.asmx?wsdl"   />  
           </appSettings>  
   
  然后在你的工程主类中添加如下方法:  
  private   void   getSoapService(string   WSDL)    

   //   vinent     050715     成功了!!!   //并且不需要安装SOAP   ToolKit   3.0   !!!  
  {  

          //1)用XML阅读器从一个文件路径或者是网络路径中读入WSDL文件:   

       XmlTextReader   reader   =   new   XmlTextReader(System.Configuration.ConfigurationSettings.AppSettings[WSDL]);         //加入   using   System.Xml;  

          // 2〉把读到的内容用ServiceDescription来描述:  
          ServiceDescription   sd   =   ServiceDescription.Read(reader);  

         //   加入   using   System.Web.Services.Description;    

         //3)用服务导入器把得到的描述导入服务描述中:

          ServiceDescriptionImporter   sdi   =   new   ServiceDescriptionImporter();  
          sdi.AddServiceDescription(sd,   null,   null);    

           //4)指定要动态编译的代码的命名空间:    

            CodeNamespace   cn   =   new   CodeNamespace("DynamicServices");    

             //5)指定要动态编译的代码单元:    

            CodeCompileUnit   ccu   =   new   CodeCompileUnit();

            //6)把命名空间添加到动态编译的代码单元的命名空间组中去:   

            ccu.Namespaces.Add(cn);    

            //7)指示服务导入器把该动态编译单元内该命名空间的代码导进去,作为新的动态服务描述:    

             sdi.Import(cn,   ccu);    

             //8)新建一个   C#编译器来编译:

             ICodeCompiler   icc   =   new   Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();

              //CSharpCodeProvider().CreateCompiler();  

              //9)新建一个编译参数对象(在这里我们设置它为空的编译参数):  

              CompilerParameters   cp=new   CompilerParameters();                 //   add   by   GUOGUO  

               //10)指示C#编译器,按照   CP   编译参数对象提供的参数来进行   CCU   单元的编译。并把编译结果传给     编译结果对象     cr:   

             CompilerResults   cr   =   icc.CompileAssemblyFromDom(cp,   ccu);  

             //11)从动态编译结果中获取   某个   字符串(通常是该类型名字)所表示的真实类   :  

             Type   t   =   cr.CompiledAssembly.GetType("DynamicServices.WSReturnString");  

             //   其中的DynamicServices必须和CodeNamespace   cn   =   new   CodeNamespace("DynamicServices");定义的同名      

            //Type   t   =   cr.CompiledAssembly.GetType("CodeDom.Service1");   //something   wrong   here  

           //12)创建该类的实例:   
             Object   obj   =   Activator.CreateInstance(t);            

            //13)根据方法名称字符串   反射到该方法的实体:   
            System.Reflection.MethodInfo     mi   =   obj.GetType().GetMethod("要调用的方法名称");

            //MethodInfo   mi   =   obj.GetType().GetMethod("Add");    

            //14)用该方法实体的.Invoke()来调用该方法:   
             Label1.Text   =   System.Convert.ToString(mi.Invoke(obj,new   object[]{"传给方法的参数1","传给方法的参数2"}))   ;   //,new   object[]{1,2}))   ;     //Console.WriteLine(System.Convert.ToString(mi.Invoke(obj,   new   object[]{1,2})));  
  }  
  调用这个方法,只要写语句:     getSoapService("WSDL");   就可以了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值