引用WCF服務(wù)經(jīng)驗(yàn)分享
WCF服務(wù)的引用是一個(gè)比較簡單的方式。我們可以通過兩種方式來實(shí)現(xiàn)引用WCF服務(wù)。在這里,就向大家詳細(xì)講解引用WCF服務(wù)的方法。#t#
引用WCF服務(wù)1.在項(xiàng)目的ServiceReferences.ClientConfig文件中加入WCF服務(wù)定義,如下:
- view plaincopy to clipboardprint?
- < 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ù)
- view plaincopy to clipboardprint?
- var client = new
ServiceReference1.Service1Client();- var client = new
ServiceReference1.Service1Client();
引用WCF服務(wù)2:在CS文件中,直接定義WCF服務(wù),代碼如下:
- view plaincopy to clipboardprint?
- 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ù)方式都能設(shè)用WCF服務(wù),比如第一種方式,如果沒有定義配置文件,則會(huì)報(bào) 找不到鍵值的錯(cuò)誤提示.