引用WCF服務正確實現方法介紹
作者:佚名
我們今天將會通過這篇文章為大家詳細講解有關引用WCF服務的相關實現方法。主要包括我們介紹的兩種方式來實現。
當我們在實際編程中需要對WCF服務進行引用的時候,需要兩種方法來實現。那么今天我們就針對引用WCF服務的相關方法來進行一個詳細介紹。希望這里介紹的內容可以解決朋友在實際應用中碰到的問題。
1.在項目的ServiceReferences.ClientConfig文件中加入WCF服務定義,如下:
- < 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服務
- var client = new ServiceReference1.Service1Client();
- var client = new ServiceReference1.Service1Client();
第二種方式:在CS文件中,直接定義引用WCF服務,代碼如下:
- 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服務,比如***種方式,如果沒有定義配置文件,則會報 找不到鍵值的錯誤提示.
【編輯推薦】
責任編輯:曹凱
來源:
CSDN

















