[WCF] Endpoint
每一个 WCF 服务都会关系到地址(Address)、绑定(Binding)和契约(Contract),而 WCF 则通过 Endpoint 将 ABC 三个方面联系在一起。每一个 Endpoint 都必须包括 ABC 三个方面,缺一不可,而 host 进程会提供 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>
<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>
使用配置文件的好处自不待言,无论是修改了服务的地址、绑定还是契约,都不需要重新编译甚至部署。配置完成后,就能在浏览器中看到如下的画面了。
编程控制 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();
}
}
}
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();
}
}
}