本文介绍了WCF 发现找到端点,但主机是“localhost"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 http://msdn.microsoft.com/en-us/library/dd456783(v=VS.100).aspx 作为起点.它在我的机器上运行良好,但后来我想在另一台机器上运行该服务.该服务被正确发现,但被发现的服务的主机名始终是localhost",这当然没有多大用处.

I am trying to use the Discovery feature in WCF using http://msdn.microsoft.com/en-us/library/dd456783(v=VS.100).aspx as a starting point. It works fine on my machine, but then I wanted to run the service on a different machine. The service was discovered properly but the hostname of the found service is always "localhost" which is of course not much use.

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 吗?

Should I simply set the Host to System.Environment.MachineName?

推荐答案

在进行更多搜索后,我发现除了使用 System.Environment.MachineName 之外没有其他解决方案

After doing some more searchingI have found no other solution than to use the System.Environment.MachineName

 new EndpointAddress(new UriBuilder {Scheme = Uri.UriSchemeNetTcp, Port = port, Host = System.Environment.MachineName}.Uri);

这篇关于WCF 发现找到端点,但主机是“localhost"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:29