1

我想创建这样的服务。当一个服务启动时,它可以在同一台机器上找到其他正在运行的服务,比如 p2p。我想使用 WCF 的 NetNamedPipeBinding。

以及,如何实施?

更新' 我试试这个。

开始服务'

    private void ActionInitService()
    {
        try
        {
            _host = new ServiceHost(this, new Uri(ADDRESS_PIPE_BASE));

            var binding = new NetNamedPipeBinding();
            _host.AddServiceEndpoint((typeof (IClientService)), binding, Address.ToString());
            // ** DISCOVERY ** //
            _host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
            _host.AddServiceEndpoint(new DiscoveryEndpoint(binding, new EndpointAddress(ADDRESS_PIPE_BASE)));
        }
        catch (Exception ex)
        {
            Debug.WriteLine("exp: " + ex);
        }
    }

寻找服务'

public ObservableCollection<string> FindRunningClient()
    {
        var endpoints = new ObservableCollection<string>();
        try
        {
            var binding = new NetNamedPipeBinding();
            var address = new EndpointAddress(ADDRESS_PIPE_BASE);
            var discoveryClient = new DiscoveryClient(new DiscoveryEndpoint(binding, address));

            FindResponse rk2Clients = discoveryClient.Find(new FindCriteria(typeof(IClientService)));

            discoveryClient.Close();

            if (rk2Clients.Endpoints.Count != 0)
            {
                foreach (EndpointDiscoveryMetadata endpoint in rk2Clients.Endpoints)
                {
                    endpoints.Add(endpoint.Address.ToString());
                }
            }

            return endpoints;
        }
        catch (Exception e)
        {
            return endpoints;
        }
    }

但问题是,它只能找到第一个启动的服务。我能做些什么 ?

4

1 回答 1

0

查看WCF 发现协议

于 2012-05-08T00:11:26.860 回答