引用WCF服務(wù)正確實(shí)現(xiàn)方法介紹
當(dāng)我們?cè)趯?shí)際編程中需要對(duì)WCF服務(wù)進(jìn)行引用的時(shí)候,需要兩種方法來實(shí)現(xiàn)。那么今天我們就針對(duì)引用WCF服務(wù)的相關(guān)方法來進(jìn)行一個(gè)詳細(xì)介紹。希望這里介紹的內(nèi)容可以解決朋友在實(shí)際應(yīng)用中碰到的問題。
1.在項(xiàng)目的ServiceReferences.ClientConfig文件中加入WCF服務(wù)定義,如下:
- < configuration>
- < system.serviceModel>
- < bindings>
- < basicHttpBinding>
- < binding name="BasicHttpBinding_IService"
maxBufferSize="2147483647"- maxReceivedMessageSize="2147483647">
- < security mode="None" />
- < /binding>
- < /basicHttpBinding>
- < /bindings>
- < client>
- < endpoint address="http://localhost:2442/Service1.svc"
binding="basicHttpBinding"- bindingConfiguration="BasicHttpBinding_IService"
contract="ServiceReference1.IService1"- name="BasicHttpBinding_IService" />
- < /client>
- < /system.serviceModel>
- < /configuration>
- < configuration>
- < system.serviceModel>
- < bindings>
- < basicHttpBinding>
- < binding name="BasicHttpBinding_IService"
maxBufferSize="2147483647"- maxReceivedMessageSize="2147483647">
- < security mode="None" />
- < /binding>
- < /basicHttpBinding>
- < /bindings>
- < client>
- < endpoint address="http://localhost:2442/Service1.svc"
binding="basicHttpBinding"- bindingConfiguration="BasicHttpBinding_IService"
contract="ServiceReference1.IService1"- name="BasicHttpBinding_IService" />
- < /client>
- < /system.serviceModel>
- < /configuration>
在CS文件中,使用如下代碼引用WCF服務(wù)
- var client = new ServiceReference1.Service1Client();
- var client = new ServiceReference1.Service1Client();
第二種方式:在CS文件中,直接定義引用WCF服務(wù),代碼如下:
- Binding binding = new BasicHttpBinding();
- EndpointAddress endPoint = new EndpointAddress(
- "http://localhost:2442/Service1.svc");
- Service1Client client = new Service1Client(binding, endPoint);
- Binding binding = new BasicHttpBinding();
- EndpointAddress endPoint = new EndpointAddress(
- "http://localhost:2442/Service1.svc");
- Service1Client client = new Service1Client(binding, endPoint);
以上兩種方式都能引用WCF服務(wù),比如***種方式,如果沒有定義配置文件,則會(huì)報(bào) 找不到鍵值的錯(cuò)誤提示.
【編輯推薦】