标准终结点的用法

StandardEndpoints 示例演示如何在服务配置文件中使用标准终结点。 标准终结点允许用户通过使用单个属性来描述地址、绑定和协定组合以及与之关联的其他属性来简化终结点定义。 此示例演示如何定义和实现自定义标准终结点以及如何定义终结点中的特定属性。

示例详细信息

可以通过提供三个参数来指定服务终结点:地址、绑定和协定。 可以提供的其他参数包括行为配置、标头、侦听 URI 等。 在某些情况下,任何或所有地址、绑定和协定都具有无法更改的值。 因此,可以使用标准终结点。 此类终结点的一些示例包括元数据交换终结点和发现终结点。 标准终结点还可以通过允许服务终结点的配置来提高可用性,而无需提供固定性质的信息或创建自己的标准终结点,例如,通过提供一组合理的默认值来提高可用性,从而减少配置文件的详细程度。

此示例包含两个项目:定义两个标准终结点的服务以及与服务通信的客户端。 以下示例显示了为配置文件中的服务定义标准终结点的方式。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <extensions>
      <endpointExtensions>
        <add name="customEndpoint" type="Microsoft.Samples.StandardEndpoints.CustomEndpointCollectionElement, service" />
      </endpointExtensions>
    </extensions>
    <services>
      <service name="Microsoft.Samples.StandardEndpoints.CalculatorService">
        <endpoint address="http://localhost:8000/Samples/Service.svc/customEndpoint" contract="Microsoft.Samples.StandardEndpoints.ICalculator" kind="customEndpoint" />
        <endpoint kind="mexEndpoint" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <standardEndpoints>
      <customEndpoint>
        <standardEndpoint property="True" />
      </customEndpoint>
    </standardEndpoints>
  </system.serviceModel>
</configuration>

为服务定义的第一个终结点是类型 customEndpoint,其定义可以在 <standardEndpoints> 部分中找到,其中属性 property 被赋值为 true。 这是使用新属性自定义的终结点的情况。 第二个终结点对应于元数据终结点,其中固定了地址、绑定和协定的值。

若要定义标准终结点元素,必须创建派生自 StandardEndpointElement 的类。 在本示例中,如以下示例所示,类CustomEndpointElement已定义。

public class CustomEndpointElement : StandardEndpointElement
{
    public bool Property
    {
        get { return (bool)base["property"]; }
        set { base["property"] = value; }
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            ConfigurationPropertyCollection properties = base.Properties;
            properties.Add(new ConfigurationProperty("property", typeof(bool), false, ConfigurationPropertyOptions.None));
            return properties;
        }
    }

    protected override Type EndpointType
    {
        get { return typeof(CustomEndpoint); }
    }

    protected override ServiceEndpoint CreateServiceEndpoint(ContractDescription contract)
    {
        return new CustomEndpoint();
    }

    protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
    {
        CustomEndpoint customEndpoint = (CustomEndpoint)endpoint;
        customEndpoint.Property = this.Property;
    }

    protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ChannelEndpointElement channelEndpointElement)
    {
        CustomEndpoint customEndpoint = (CustomEndpoint)endpoint;
        customEndpoint.Property = this.Property;
    }

    protected override void OnInitializeAndValidate(ServiceEndpointElement serviceEndpointElement)
    {
    }

    protected override void OnInitializeAndValidate(ChannelEndpointElement channelEndpointElement)
    {
    }
}

CreateServiceEndpoint 函数中,将创建一个 CustomEndpoint 对象。 其定义如以下示例所示:

public class CustomEndpoint : ServiceEndpoint
{
    public CustomEndpoint()
        : this(string.Empty)
    {
    }

    public CustomEndpoint(string address)
        : this(address, ContractDescription.GetContract(typeof(ICalculator)))
    {
    }

    public CustomEndpoint(string address, ContractDescription contract)
        : base(contract)
    {
        this.Binding = new BasicHttpBinding();
        this.IsSystemEndpoint = false;
    }

    public bool Property
    {
        get;
        set;
    }
}

若要在服务和客户端之间执行通信,请在服务客户端中创建服务引用。 生成和执行示例后,服务将执行,客户端与之通信。 请注意,每次服务发生一些更改时,都应更新服务引用。

使用此示例

  1. 使用 Visual Studio 打开StandardEndpoints.sln文件。

  2. 使多个项目可以启动。

    1. 解决方案资源管理器中,右键单击标准终结点解决方案,然后选择 “属性”。

    2. “常用属性”中,选择“ 启动项目”,然后单击“ 多个启动项目”。

    3. 将服务项目移动到列表的开头,行动设置为开始

    4. 将客户端项目移动至服务项目之后,也将“操作”设置为“启动”

      这指定在服务项目之后执行客户端项目。

  3. 若要运行解决方案,请按 F5

注释

如果这些步骤不起作用,请使用以下步骤确保环境已正确设置:

  1. 确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
  2. 要生成解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。
  3. 若要在单个或多台计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行作。