C# WCF发现发现终结点,但主机为;“本地主机”;
我试图使用WCF中的发现功能作为起点。它在我的机器上运行得很好,但后来我想在另一台机器上运行该服务。服务被正确地发现了,但是找到的服务的主机名总是“localhost”,这当然没有多大用处 服务端点: 客户:C# WCF发现发现终结点,但主机为;“本地主机”;,c#,wcf,service-discovery,C#,Wcf,Service Discovery,我试图使用WCF中的发现功能作为起点。它在我的机器上运行得很好,但后来我想在另一台机器上运行该服务。服务被正确地发现了,但是找到的服务的主机名总是“localhost”,这当然没有多大用处 服务端点: 客户: static EndpointAddress FindServiceAddress() { 秒表秒表=新秒表(); 秒表。开始(); DiscoveryClient DiscoveryClient=newdiscoveryclient(new-UdpDiscoveryEndpoint())
static EndpointAddress FindServiceAddress()
{
秒表秒表=新秒表();
秒表。开始();
DiscoveryClient DiscoveryClient=newdiscoveryclient(new-UdpDiscoveryEndpoint());
//查找端点
FindResponse FindResponse=discoveryClient.Find(新的FindCriteria(typeof(T));
WriteLine(string.Format(“搜索{0}秒。找到{1}个端点)”,stopwatch.elapsedmillesons/1000,findResponse.Endpoints.Count));
如果(findResponse.Endpoints.Count>0)
{
返回findResponse.Endpoints[0]。地址;
}
返回null;
}
我是否应该简单地将主机设置为System.Environment.MachineName?在进行了更多的搜索之后,我发现除了使用System.Environment.MachineName之外,没有其他解决方案
new EndpointAddress(new UriBuilder {Scheme = Uri.UriSchemeNetTcp, Port = port, Host = System.Environment.MachineName}.Uri);
我花了很多时间调查这个问题。在代码中构建基址对我来说是不可接受的,因为它意味着硬编码传输方案和端口(当然,后者可以存储在单独的配置部分中,但为什么不使用现有部分呢?)。我希望能够像往常一样在config中配置基址。事实证明,像
这样的基址可以完美地工作。我认为编程配置也是如此。我也有这个问题。假设其原因UDP协议包含服务地址。。。它最终来自app.configI假设*绑定到每个可用的IP地址(跨所有网络适配器)?最终是的。准确地说,加载配置时,将解析此uri,并用调用Dns.GetHostName()的结果替换“*”字符。如果使用Reflector:)查看System.ServiceModel.Description.ConfigLoader.LoadHostConfig方法,您可以看到更多内容
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;
}
new EndpointAddress(new UriBuilder {Scheme = Uri.UriSchemeNetTcp, Port = port, Host = System.Environment.MachineName}.Uri);