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服务,比如第一种方式,如果没有定义配置文件,则会报 找不到键值的错误提示.