我想创建这样的服务。 当服务启动时,它可以在同一台机器上找到其他正在运行的服务,如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;
}
}
但问题是,它只能找到第一个启动的服务。 我该怎么办?