当我们在实际编程中需要对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服务,比如***种方式,如果没有定义配置文件,则会报 找不到键值的错误提示.
【编辑推荐】