您的位置:知识库 »

WCF专题系列(4):深入WCF寻址Part 4—自定义消息筛选器

作者: TerryLee  来源: 博客园  发布时间: 2008-10-30 16:33  阅读: 2561 次  推荐: 0   原文链接   [收藏]  

配置服务

定义一个简单的服务契约以及服务的实现,代码如下,不再多说:

/// <summary>
/// Author: TerryLee
/// Url: http://www.cnblogs.com/terrylee
/// </summary>
[ServiceContract(Namespace = "http://www.cnblogs.com/terrylee/")]
public interface IEchoService
{
    [OperationContract]
    string Echo(string msg);
}

public class EchoService : IEchoService
{
    public string Echo(string msg)
    {
        return "Hello:" + msg;
    }
}

现在来看一下服务端的配置,除了必须的终结点配置之外,为服务注册一个新的Behavior,代码如下所示:

<extensions>
  <behaviorExtensions>
    <add name="filteringEndpointBehavior" 
         type="TerryLee.CustomizeMessageFilter.Service.
         FilteringEndpointBehaviorExtension, Service, 
         Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>

创建EndpointBehavior配置:

<endpointBehaviors>
  <behavior name="filterBehavior">
    <filteringEndpointBehavior characters="terrylee" />
  </behavior>
</endpointBehaviors>

服务终结点使用behaviorConfiguration:

<endpoint address=""
          binding ="wsHttpBinding"
          contract="TerryLee.CustomizeMessageFilter.Service.IEchoService"
          behaviorConfiguration="filterBehavior">
</endpoint>

最终完整的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="TerryLee.CustomizeMessageFilter.Service.EchoService"
               behaviorConfiguration="echoBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8887/EchoService"/>
          </baseAddresses>
        </host>
        <endpoint address=""
                  binding ="wsHttpBinding"
                  contract="TerryLee.CustomizeMessageFilter.Service.IEchoService"
                  behaviorConfiguration="filterBehavior">
        </endpoint>
      </service>
    </services>
    <extensions>
      <behaviorExtensions>
        <add name="filteringEndpointBehavior" 
             type="TerryLee.CustomizeMessageFilter.Service.
             FilteringEndpointBehaviorExtension, Service, 
             Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
  <behaviors>
    <serviceBehaviors>
      <behavior name="echoBehavior">
        <serviceMetadata httpGetEnabled="true"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="filterBehavior">
        <filteringEndpointBehavior characters="terrylee" />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  </system.serviceModel>
</configuration>

至此服务端配置完成,根据配置,只有SOAP消息“To”标头中包含有“terrylee”的字符时,此时终结点的消息过滤器才与此消息匹配。现在在控制台中输出一下,看看针对配置的终结点它所使用的地址过滤器和契约过滤器分别是什么,如图1所示:

TerryLee_WCF_10

图1

可以看到,终结点所用的地址过滤器不再是默认的EndpointAddressMessageFilter,而是我们自定义的SpecialCharactersMessageFilter。

0
0

热门文章

    最新文章

      最新新闻

        热门新闻