引用WCF服務經驗分享
作者:佚名
引用WCF服務可以通過在項目的ServiceReferences.ClientConfig文件中加入WCF服務定義來實現,或者,在CS文件中,直接定義WCF服務。
WCF服務的引用是一個比較簡單的方式。我們可以通過兩種方式來實現引用WCF服務。在這里,就向大家詳細講解引用WCF服務的方法。#t#
引用WCF服務1.在項目的ServiceReferences.ClientConfig文件中加入WCF服務定義,如下:
- 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服務
- view plaincopy to clipboardprint?
- var client = new
ServiceReference1.Service1Client();- var client = new
ServiceReference1.Service1Client();
引用WCF服務2:在CS文件中,直接定義WCF服務,代碼如下:
- 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服務方式都能設用WCF服務,比如第一種方式,如果沒有定義配置文件,則會報 找不到鍵值的錯誤提示.
責任編輯:曹凱
來源:
搜狐博客