使用服务端点
上次修改时间: 2011年2月15日
适用范围: SharePoint Foundation 2010
本文内容
支持的协议
定义服务端点
提供多个端点
在服务设置代码中定义端点
在客户端配置中定义端点
在代理代码中定义端点
与 Windows Communication Foundation (WCF) 服务进行的所有通信都是通过服务端点完成的。服务端点可指定一种合约,以定义可通过该端点访问哪些服务类的方法,并且每个端点可揭示一组不同的方法。端点还可定义绑定,以指定客户端与服务以及端点所在的网络地址进行通信的方式。
支持的协议
Service Application Framework 支持所有 WCF 支持协议,但建议对服务端点使用 http 和 https 协议:
HTTP
HTTPS
备注
Service Application Framework 会自动为 HTTPS 服务端点安装和配置安全套接字层 (SSL) 证书。虽然 Internet Information Services (IIS) 管理用户界面不显示该证书,但它确实已安装。可通过在命令提示符处键入以下命令来确认安装:
netsh http show sslcert ipport=0.0.0.0:32844
定义服务端点
必须在应用程序的 web.config 设置中定义 WCF 服务应用程序支持的每个端点。每个端点的协议和地址必须唯一。例如,如果两个端点在各自的绑定中指定不同的协议,则它们可具有相同的地址。但是,如果两个端点使用相同的协议,则它们必须指定不同的地址。
请为每个端点使用唯一的端点地址,以便这些地址不依赖于绑定协议。请指定相对于 Service Application Framework 配置的基址的端点地址。
例如,若要指定相对地址为"http"和"https"的两个端点,请使用以下代码。
<services>
<service
name="Microsoft.SharePoint.Test.SampleWebServiceApplication">
<endpoint
address="http"
contract="Microsoft.SharePoint.Test.ISampleWebServiceContract"
binding="customBinding"
bindingConfiguration="SampleWebServiceHttpBinding" />
<endpoint
address="https"
contract="Microsoft.SharePoint.Test.ISampleWebServiceContract"
binding="customBinding"
bindingConfiguration="SampleWebServiceHttpsBinding" />
</service>
</services>
在上面的示例中,如果服务基址为 http://machine:8080/application/calculator.svc,则端点地址为:
http://machine:8080/application/calculator.svc/http
http://machine:8080/application/calculator.svc/https
提供多个端点
一个服务应用程序可能支持两个端点:一个端点使用针对性能进行优化的绑定(例如,前端 Web 服务器和应用程序服务器之间的网络通信是专用的,不必加密后端 LAN),另一个端点使用针对安全性进行优化的绑定(必须加密网络通信)。Service Application Framework 提供的用户界面使服务器场管理员能够选择最适合网络拓扑的端点。管理员可通过以下方式来管理端点选择:使用管理中心网站中"服务应用程序管理"页上的"发布"选项,或使用 Set-SPServiceApplication Windows PowerShell cmdlet 的 DefaultEndpoint 参数。
在服务设置代码中定义端点
默认情况下,一个 Web 服务应用程序具有一个 HTTP 端点。如果您希望服务应用程序具有此配置,则不需要进行更改。如果要使用其他协议或支持多个端点,则必须显式定义服务应用程序将支持的所有端点。
按以下示例所示使用 SPIisWebServiceApplication 的 AddServiceEndpoint 方法。
// Construct your SPIisWebServiceApplication derived class as usual.
MyWebServiceApplication app = new MyWebServiceApplication(…);
// Commit the new application to the configuration database.
// NOTE: Update must be called before AddServiceEndpoint.
// The service application must be committed to the configuration database before endpoints can be added.
app.Update();
// Add the endpoints supported by the application.
// NOTE: AddServiceEndpoint should be called before app.Provision, because app.Provision will provision
// the default HTTP endpoint if no endpoints are explicitly added to the application.
// NOTE: The default endpoint name is always "http"
app.AddServiceEndpoint("", SPIisWebServiceBindingType.Http);
// Add an alternate HTTPS endpoint.
app.AddServiceEndpoint("secure", SPIisWebServiceBindingType.Https);
名为"http"的端点用作服务应用程序的默认端点。即使端点地址不唯一,服务端点名称也必须是唯一的。如果您在同一相对地址具有两个端点,则必须使用 AddServiceEndpoint 的第三个可选参数指定相对地址。第三个参数默认为端点名称。
下面的示例演示如何在相同的基本服务地址定义两个端点,第一个端点使用 HTTP 协议,第二个使用 HTTPS 协议。https 端点位于基本服务地址 "" 处。
app.AddServiceEndpoint("", SPIisWebServiceBindingType.Http);
// The default endpoint.
app.AddServiceEndpoint("https", SPIisWebServiceBindingType.Https, "");
在客户端配置中定义端点
还必须在客户端配置中定义每个端点。创建一个 client.config 文件,其绑定与服务应用程序的 web.config 文件相匹配。每个客户端端点必须具有唯一的 name 属性值,以便可从将读取配置文件的代理代码中引用该端点,如下面的示例所示。
此示例中所选择的端点配置名称与服务端点相对地址相匹配,尽管这不是必需的。
<configuration>
<system.serviceModel>
<client>
<endpoint
name="http"
contract="Microsoft.SharePoint.Test.ISampleWebServiceContract"
binding="customBinding"
bindingConfiguration="SampleWebServiceHttpBinding" />
<endpoint
name="https"
contract="Microsoft.SharePoint.Test.ISampleWebServiceContract"
binding="customBinding"
bindingConfiguration="SampleWebServiceHttpsBinding" />
</client>
在代理代码中定义端点
代理代码必须通过使用相应的端点配置来创建通道工厂。端点配置通过名称(endpoint 客户端配置元素的 name 属性)进行标识。要确定端点配置名称,请在创建通道前检查端点 URI,然后将该相对地址与已知端点地址进行比较。以下示例显示用于缓存客户端通道工厂的代码。如果仅通过协议区分端点地址,请在比较时使用 URI 架构。
private string m_EndpointConfigurationName;
private ChannelFactory<ISampleWebServiceContract> m_ChannelFactory;
private object m_ChannelFactoryLock = new object();
现在,获取给定端点地址的端点配置名称。<param name="address">The endpoint address.</param>
GetEndpointConfigurationName 返回端点配置名称。返回的端点名称必须与 client.config 文件中的端点元素名称之一相匹配。
private string GetEndpointConfigurationName(Uri address)
{
if (null == address)
{
throw new ArgumentNullException("address");
}
if (address.Scheme == Uri.UriSchemeHttp)
{
return "http";
}
if (address.Scheme == Uri.UriSchemeHttps)
{
return "https";
}
return String.Empty;
}
private ISampleWebServiceContract GetChannel(Uri address)
{
// Create an endpoint address for the service.
EndpointAddress endpointAddress = new EndpointAddress(address);
// Get the endpoint configuration name.
string endpointConfigurationName = GetEndpointConfigurationName(address);
// Check for a cached channel factory for the endpoint configuration.
if ((null == m_ChannelFactory) ||
(endpointConfigurationName != m_EndpointConfigurationName))
{
lock (m_ChannelFactoryLock)
{
if ((null == m_ChannelFactory) ||
(endpointConfigurationName != m_EndpointConfigurationName))
{
// Create a channel factory
// endpoint configuration name.
m_ChannelFactory =
CreateChannelFactory<ISampleWebServiceContract>
(endpointConfigurationName);
// Store the current endpoint configuration name.
m_EndpointConfigurationName = endpointConfigurationName;
}
}
}
// Create a channel from the channel factory.
return m_ChannelFactory.CreateChannel(endpointAddress);
}
备注
如果缓存了通道工厂,则必须禁用缓存。