您的位置:知识库 »

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

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

客户端测试

通过上面的配置,我们知道,只有SOAP消息的“To”标头中带有“terrylee”字符,消息才会被分发到相应的终结点上。现在编写一个简单的测试客户端程序,如下代码所示:

/// <summary>
/// Author: TerryLee
/// Url: http://www.cnblogs.com/terrylee
/// </summary>
static void Main()
{
    Uri serviceVia = new Uri("http://localhost:8887/EchoService");
    WSHttpBinding binding = new WSHttpBinding();
    ChannelFactory<IEchoService> factory = new ChannelFactory<IEchoService>
        (binding, new EndpointAddress(serviceVia));

    String address = "http://localhost/terrylee";

    Console.WriteLine(String.Format("Sending message to {0}...", address));
    IEchoService channel = factory.CreateChannel(
        new EndpointAddress(address), serviceVia);
    try
    {
        String reply = channel.Echo("cnblogs");
        Console.WriteLine(reply.ToString());
        ((IClientChannel)channel).Close();
    }
    catch (CommunicationException ce)
    {
        Console.WriteLine("Exception: {0}", ce.Message);
        ((IClientChannel)channel).Abort();
    }
    Console.WriteLine("Press <ENTER> to terminate client.");
    Console.ReadLine();
}

由于定义了终结点的逻辑地址为“http://localhost/terrylee”,符合我们上面所讲的条件,可以看到输出结果如图2所示:

TerryLee_WCF_11  

图2

可以看到,服务正确的返回我们想要的消息,但是如果修改一下address为“http://localhost/anytao”,由于没有包含特定的字符“terrylee”,服务端将返回错误的信息:

String address = "http://localhost/anytao";

输出结果如图3所示:

TerryLee_WCF_12

图3

结束语

本文我介绍了如何自定义消息筛选器,以及如何通过Behavior来修改EndpointDispatcher的默认过滤器,消息过滤是消息分发过程中非常重要的一个环节,也是WCF寻址中一个重要的部分,希望对于大家有所帮助。

0
0
 

热门文章

    最新文章

      最新新闻

        热门新闻