此方法只要知道SERVICE地址与要执行的方法名与参数即可调用webservice,
注意这个只是在普通工程中才可用。silverlight调用方法请往后看:
 
C# 代码
  • /// <summary>  
  •         /// 动态调用WebService  
  •         /// </summary>  
  •         /// <param name="url">WebService地址</param>  
  •         /// <param name="classname">类名</param>  
  •         /// <param name="methodname">方法名(模块名)</param>  
  •         /// <param name="args">参数列表</param>  
  •         /// <returns>object</returns>  
  •         public static object InvokeWebService(string url, string classname, string methodname, object[] args)  
  •         {  
  •             object obj = GetWebServiceClassObject(url,classname);  
  •             Type t = obj.GetType();  
  •             System.Reflection.MethodInfo mi = t.GetMethod(methodname);//通过反射获得方法名  
  •             return mi.Invoke(obj, args);//使用制定的参数调用当前实例所表示的方法,执行方法  
  •         }  
  •         /// <summary>  
  •         /// 获取webService远程对象  
  •         /// </summary>  
  •         /// <param name="url"></param>  
  •         /// <param name="classname"></param>  
  •         /// <returns></returns>  
  •         public static object GetWebServiceClassObject(string url, string classname)  
  •         {  
  •             string @namespace = "ServiceBase.WebService.DynamicWebLoad";  
  •             if (classname == null || classname == "")  
  •             {  
  •                 classname = GetClassName(url);  
  •             }  
  •             ///动态调用类所执行的过程  
  •             //1.获取服务描述语言(WSDL)  
  •             WebClient wc = new WebClient();  
  •             Stream stream = wc.OpenRead(url + "?WSDL");  
  •             ServiceDescription sd = ServiceDescription.Read(stream);//设置Web服务描述语言  
  •             ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();//生成客户端代理类  
  •             sdi.AddServiceDescription(sd, """");  
  •             CodeNamespace cn = new CodeNamespace(@namespace);//声明命名空间  
  •             //2.生成客户端代理类代码  
  •             CodeCompileUnit ccu = new CodeCompileUnit();//为CodeDOM程序图形提供容器  
  •             ccu.Namespaces.Add(cn);//获取命名空间集合  
  •             sdi.Import(cn, ccu);  
  •             CSharpCodeProvider csc = new CSharpCodeProvider();//提供对 C# 代码生成器和代码编译器的实例的访问  
  •             //ICodeCompiler icc = csc.CreateCompiler();//定义用于调用源代码编译的接口或使用指定编译器的 CodeDOM 树  
  •             //3.设定编译器的参数  
  •             CompilerParameters cplist = new CompilerParameters();  
  •             cplist.GenerateExecutable = false;//设置是否为可执行文件  
  •             cplist.GenerateInMemory = true;//设置是否在内存中生成输出  
  •             cplist.ReferencedAssemblies.Add("System.dll");  
  •             cplist.ReferencedAssemblies.Add("System.XML.dll");  
  •             cplist.ReferencedAssemblies.Add("System.Web.Services.dll");  
  •             cplist.ReferencedAssemblies.Add("System.Data.dll");  
  •             //4.编译代理类  
  •             CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);// icc.CompileAssemblyFromDom(cplist, ccu);//使用指定的编译器设置编译程序集  
  •             if (true == cr.Errors.HasErrors)  
  •             {  
  •                 System.Text.StringBuilder sb = new StringBuilder();  
  •                 foreach (CompilerError ce in cr.Errors)  
  •                 {  
  •                     sb.Append(ce.ToString());  
  •                     sb.Append(System.Environment.NewLine);  
  •                 }  
  •                 throw new Exception(sb.ToString());  
  •             }  
  •   
  •             //5.生成代理实例,并调用方法  
  •             System.Reflection.Assembly assembly = cr.CompiledAssembly;//获取或设置已编译的程序集  
  •             Type t = assembly.GetType(@namespace + "." + classname, truetrue);  
  •             object obj = Activator.CreateInstance(t);//为 COM 对象提供对方法的版本无关的访问  
  •             return obj;  
  •         }  
  •         private static string GetClassName(string url)  
  •         {  
  •             string[] parts = url.Split('/');  
  •             string[] pps = parts[parts.Length - 1].Split('.');  
  •             return pps[0];  
  •         }  
  •  
    下面来看一下动态调用WCF服务:
     
    这是我在一个项目中,为了不去配置服务地址,直接解析当前访问路径,加上服务的后面部分生成与SILVERLIGHT同一域名下的服务:
    这个方法的前提是要先引用这个服务,这是silverlight下用的,普通程序中也差不多
    C# 代码
  • /// <summary>  
  •         /// 获取配置远程WCF服务对象  
  •         /// </summary>  
  •         public DMSTypeConfigWcfService.DMSTypeConfigServiceClient DmsTypeConfigWcfService  
  •         {  
  •             get  
  •             {  
  •                 if (_DmsTypeConfigWcfService == null)  
  •                 {  
  •                     string url = Application.Current.Host.Source.Scheme + "://" + Application.Current.Host.Source.DnsSafeHost + ":" +  
  •                         Application.Current.Host.Source.Port + TypeConfigWcfUri;  //这里就是获得当前URL再加上服务路径,当然你也可以配置在一个地方。
  •                     EndpointAddress address = new EndpointAddress(url);  
  •   
  •                     BinaryMessageEncodingBindingElement binary = new BinaryMessageEncodingBindingElement();  
  •                     HttpTransportBindingElement transport = new HttpTransportBindingElement();  
  •                     CustomBinding binding = new CustomBinding(binary, transport);  
  •   
  •                     _DmsTypeConfigWcfService = new DMSTypeConfigWcfService.DMSTypeConfigServiceClient(binding, address);  
  •                 }  
  •                 return _DmsTypeConfigWcfService;  
  •             }  
  •         }  
  •  silverlight下调用webservice与WCF类似。主要是把CustomBinding 改为Http的一个binding
    代码如下:FenbiSl.WimUpFile.UpFileServiceSoapClient就是引用进来的WEBSERVICE服务对象
    url 是与silverlight在同一域名下.所以我这样就可以做到更换域名或部署时不需要重新改配置...
     
    C# 代码
  • string url = Application.Current.Host.Source.Scheme + "://" + Application.Current.Host.Source.DnsSafeHost + ":" +
  •  Application.Current.Host.Source.Port + "/wim/UpFileService.asmx";  
  •                EndpointAddress address = new EndpointAddress(url);  
  •   
  •                BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);  
  •   
  •                UpFileClient = new FenbiSl.WimUpFile.UpFileServiceSoapClient(binding, address);  
  •  

    相关文章:

    • 2022-12-23
    • 2022-12-23
    • 2021-10-26
    • 2021-11-02
    • 2021-09-14
    • 2022-12-23
    • 2022-02-16
    • 2022-12-23
    猜你喜欢
    • 2021-06-03
    • 2022-02-26
    • 2021-05-16
    相关资源
    相似解决方案