1. 配置 IIS 綁定 IP地址 2. 在 SL 中引用 WebService 3. 在需要調(diào)用 WebService 的地方寫下列代碼: WCF : 1BasicHttpBinding basicBinding =new BasicHttpBinding(); 2 3CustomBinding binding =new CustomBinding(basicBinding); 4 5BindingElement binaryElement =new BinaryMessageEncodingBindingElement(); 6 7// 刪除原來 Elements 集合內(nèi)的 TextMessageEncodingBindingElement 8 9binding.Elements.Remove(binding.Elements[0]); 10 11// 添加 BinaryMessageEncodingBindingElement 12 13binding.Elements.Insert(0, binaryElement); 14 15// wcf 地址 16 17EndpointAddress endPoint =new EndpointAddress("http://172.168.1.100/DynamicInvokeWCF.Web/DynWCF.svc"); 18 19// 創(chuàng)建 wcf 客戶端 20 21DynWCFClient client = (DynWCFClient)Activator.CreateInstance(typeof(DynWCFClient), binding, endPoint); 22 23client.DoWorkCompleted +=new EventHandler<DoWorkCompletedEventArgs>(client_DoWorkCompleted); 24 25client.DoWorkAsync(); 26 27
傳統(tǒng) WebService: 1BasicHttpBinding basicBinding =new BasicHttpBinding(); 2 3CustomBinding binding =new CustomBinding(basicBinding); 4 5BindingElement binaryElement =new BinaryMessageEncodingBindingElement(); 6 7EndpointAddress endPoint =new EndpointAddress("http://172.168.1.100/DynamicInvokeWCF.Web/Asmx.asmx"); 8 9AsmxSoapClient client = (AsmxSoapClient)Activator.CreateInstance(typeof(AsmxSoapClient), binding, endPoint); 10 11client.HelloWorldCompleted +=new EventHandler<HelloWorldCompletedEventArgs>(client_HelloWorldCompleted); 12 13client.HelloWorldAsync(); 14 15 這樣就可以動態(tài)的調(diào)用 webservice 了. 完全不需要 ServiceReferences.ClientConfig 文件了. |
|