WCF Discovery finds endpoint but host is “localhost”

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 07:17:58

问题


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.

Service Endpoint:

var endpointAddress = new EndpointAddress(new UriBuilder { Scheme = Uri.UriSchemeNetTcp, Port = port}.Uri);
var endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceInterface)), new NetTcpBinding (), endpointAddress);

Client:

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;
 }

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


回答1:


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);



回答2:


I spent a lot of time investigating this problem. Building base addresses in the code was not acceptable for me, as it implies hardcoding transport scheme and port (the latter, of course, can be stored in a separate config section, but then why not just to use the existing section?). I wanted to have an ability to just configure the base address in config as usual. And it turns out that a base address like <add baseAddress="net.tcp://*:8731/"/> will perfectly work. I think the same is true for programmatic configuration.



来源:https://stackoverflow.com/questions/2508274/wcf-discovery-finds-endpoint-but-host-is-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!