WCF:如何设置AnnouncementService只接收localhost开/关线路消息

时间:2012-06-27 01:40:05

标签: wcf discovery

我创建了一个wcf-lib,我只想进行进程间通信。 当我打开一个使用lib的app时,会互相宣布。 我使用UdpAnnouncementEndpoint,它的工作原理。 但它将收到内联网的公告。 我该怎么办?

我用这样的代码创建端点:

    private void ActionInitClientService()
    {
        // Create ClientSelt ServiceHost
        _clientServiceHost = new ServiceHost(_clientInstance);
        _clientServiceHost.AddServiceEndpoint((typeof (IClientService)), new NetNamedPipeBinding(), Info.Address);

        // Make the client discoverable via Udp
        // and Broadcast itself to Online announcement
        _clientServiceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
        var discoveryBehavior = new ServiceDiscoveryBehavior();
        discoveryBehavior.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());
        _clientServiceHost.Description.Behaviors.Add(discoveryBehavior);

        _clientServiceHost.Opened += OnOpenedClientServiceHost;
        _clientServiceHost.Closed += OnClosedClientServiceHost;
    }

并在主机中添加AnnouncementsListener:

    private void ActionInitAnnouncementsListener()
    {
        var announcementService = new AnnouncementService();
        announcementService.OnlineAnnouncementReceived += OnOnlineAnnouncementReceived;
        announcementService.OfflineAnnouncementReceived += OnOfflineAnnouncementReceived;

        _announcementsListener = new ServiceHost(announcementService);
        _announcementsListener.AddServiceEndpoint(new UdpAnnouncementEndpoint());
    }

2 个答案:

答案 0 :(得分:0)

不,不会。您正在使用NetNamedPipeBinding,仅当客户端和服务在同一台计算机上运行时才有效。根据其定义,NetNamePipeBinding提供了一种安全可靠的绑定,该绑定针对机上通信进行了优化。

答案 1 :(得分:0)

我道歉,我正在查看代码的错误部分。您没有使用命名管道,以使您的客户端可被发现。

这里是你如何获取传入消息的IP地址(我假设如果你的消息来自你的内部网,你可以使用某些代码逻辑来推断该IP地址。)

OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

上面的代码在以下链接中显示:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f14520be-d77d-401f-b339-9c58585857f7/

您还可以在http://www.danrigsby.com/blog/index.php/2008/08/20/observableservicehost-an-instancecontext-creation-aware-wcf-servicehost/找到更多详细信息。