我正在尝试使用 WCF 中的发现功能,使用http://msdn.microsoft.com/en-us/library/dd456783(v=VS.100).aspx作为起点。它在我的机器上运行良好,但后来我想在另一台机器上运行该服务。该服务已正确发现,但找到的服务的主机名始终是“localhost”,这当然没有多大用处。
服务端点:
var endpointAddress = new EndpointAddress(new UriBuilder { Scheme = Uri.UriSchemeNetTcp, Port = port}.Uri);
var endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceInterface)), new NetTcpBinding (), endpointAddress);
客户:
static EndpointAddress FindServiceAddress<T>()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
// Find endpoints
FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(T)));
Console.WriteLine(string.Format("Searched for {0} seconds. Found {1} Endpoint(s).",stopwatch.ElapsedMilliseconds / 1000,findResponse.Endpoints.Count));
if (findResponse.Endpoints.Count > 0)
{
return findResponse.Endpoints[0].Address;
}
return null;
}
我应该简单地将主机设置为 System.Environment.MachineName 吗?