WCF是由微软公司开发的一种功能强大的开发插件,主要应用于.NET Framework 3.5,可以帮助我们实现许多特定的功能需求。在这里我们竟会为大家详细介绍一下有关WCF Endpoint的相关应用方法。#t#

网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了信丰免费建站欢迎大家使用!
每一个 WCF 服务都会关系到地址(Address)、绑定(Binding)和契约(Contract),而 WCF 则通过 Endpoint 将 ABC 三个方面联系在一起。每一个 Endpoint 都必须包括 ABC 三个方面,缺一不可,而 host 进程会提供WCF Endpoint供客户端调用。每个 Endpoint 都对应一个唯一地址,但是多个 Endpoint 可以共享相同的绑定和契约,每个服务又可以提供多个 Endpoint 供客户端掉用。
使用配置文件
再次体现 Microsoft 的傻瓜式编程。唯一值得注意的地方是在 service 节点中添加了 behaviorConfiguration 属性。
- < ?xml version="1.0"?>
 - < configuration xmlns="http://schemas.microsoft.com/.
 
NetConfiguration/v2.0">- < system.serviceModel>
 - < services>
 - < !--< service name="MyService"
 
behaviorConfiguration="returnFaults">- < endpoint contract="IMyService" binding="wsHttpBinding"/>
 - < /service>-->
 - < service name="Anrs.Service.AnrsService"
 
behaviorConfiguration="returnFaults">- < endpoint contract = "Anrs.Service.IAnrsServiceContract1"
 - binding = "wsHttpBinding"
 - address = "http://localhost:4021/AnrsServiceByIIS/AnrsService/" />
 - < /service>
 - < /services>
 - < behaviors>
 - < serviceBehaviors>
 - < behavior name="returnFaults" >
 - < serviceMetadata httpGetEnabled="true">< /serviceMetadata>
 - < serviceDebug includeExceptionDetailInFaults="true" />
 - < /behavior>
 - < /serviceBehaviors>
 - < /behaviors>
 - < /system.serviceModel>
 - < system.web>
 - < compilation debug="true"/>
 - < /system.web>
 - < /configuration>
 
使用配置文件的好处自不待言,无论是修改了服务的地址、绑定还是契约,都不需要重新编译甚至部署。配置完成后,就能在浏览器中看到如下的画面了。
编程控制WCF Endpoint
相对于配置文件的简单,编程控制 Endpoint 也不会多几行代码。下面的代码就相当于上面的配置文档。
- using System;
 - using System.ServiceModel;
 - using System.ServiceModel.Channels;
 - namespace Anrs.Service
 - {
 - class Program
 - {
 - static void Main(string[] args)
 - {
 - ServiceHost sh = new ServiceHost(typeof(AnrsService));
 - Binding wsHttpBinding = new WSHttpBinding();
 - sh.AddServiceEndpoint(typeof(IAnrsServiceContract1),
 - wsHttpBinding,
 - new Uri("http://localhost:8086/AnrsService/"));
 - sh.Open();
 - Console.Write("Press any key to exit");
 - Console.ReadLine();
 - sh.Close();
 - }
 - }
 - }
 
WCF Endpoint的相关内容就为大家介绍到这里。