WCF 发现找到端点,但主机是“localhost”
我正在尝试使用 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 吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过更多搜索后,我发现除了使用 System.Environment.MachineName 之外没有其他解决方案
After doing some more searchingI have found no other solution than to use the System.Environment.MachineName
我花了很多时间研究这个问题。在代码中构建基地址对我来说是不可接受的,因为它意味着硬编码传输方案和端口(当然,后者可以存储在单独的配置部分中,但为什么不直接使用现有部分呢?)。我希望能够像往常一样在配置中配置基址。事实证明,像
这样的基地址将完美地工作。我认为编程配置也是如此。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.