【问题标题】:WCF Discovery finds endpoint but host is "localhost"WCF 发现找到端点,但主机是“localhost”
【发布时间】:2010-03-24 14:09:22
【问题描述】:

我正在尝试使用 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 吗?

【问题讨论】:

  • 我也有这个问题。假设它的原因是 UDP 协议包括服务地址......最终来自 app.config

标签: c# wcf service-discovery


【解决方案1】:

在做了更多搜索之后,除了使用 System.Environment.MachineName 之外,我没有找到其他解决方案

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

【讨论】:

    【解决方案2】:

    我花了很多时间研究这个问题。在代码中构建基地址对我来说是不可接受的,因为它意味着硬编码传输方案和端口(后者当然可以存储在单独的配置部分中,但为什么不直接使用现有部分呢?)。我希望能够像往常一样在 config 中配置基地址。事实证明,像&lt;add baseAddress="net.tcp://*:8731/"/&gt; 这样的基地址将完美地工作。我认为编程配置也是如此。

    【讨论】:

    • 我假设 * 绑定到每个可用的 IP 地址(跨所有网络适配器)?
    • 最终是的。准确地说,加载配置时,会解析此 uri,并将“*”字符替换为调用 Dns.GetHostName() 的结果。如果您查看 System.ServiceModel.Description.ConfigLoader.LoadHostConfig 使用 Reflector 的方法,您可以看到更多信息:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多