我花了 3 个小时寻找任何样本并浏览了许多文章。我正在尝试让 WCF 即席发现机制为自托管的 Windows 服务工作。如果我在同一台机器上运行客户端它可以工作,但在另一台机器上它不会。每个教程/示例(方便地)在同一台工作的机器上显示它。
- 我在两台机器上都禁用了防火墙。
- 如果我直接在客户端使用端点,它就可以工作。因此,只有服务发现不起作用。
这是我的服务器代码:
static void Main(string[] args)
{
Uri baseAddress = new Uri(string.Format("http://{0}:12345/discovery/Myservice/", System.Net.Dns.GetHostName()));
Console.WriteLine(baseAddress);
using (ServiceHost serviceHost = new ServiceHost(typeof(SampleService.Service1), baseAddress))
{
serviceHost.AddServiceEndpoint(typeof(SampleService.IService1), new BasicHttpBinding(), string.Empty);
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
ServiceMetadataBehavior metaDataBehavior = new ServiceMetadataBehavior();
metaDataBehavior.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(metaDataBehavior);
serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
serviceHost.Open();
Console.WriteLine("Press to terminate service.");
Console.ReadLine();
}
}
这是我的客户代码:
static void InvokeService()
{
Console.WriteLine("\nFinding Service ..");
DiscoveryClientBindingElement discoveryClientBindingElement =
new DiscoveryClientBindingElement();
var Services =
discoveryClientBindingElement.FindCriteria = new FindCriteria(typeof(ServiceReference1.IService1));
discoveryClientBindingElement.DiscoveryEndpointProvider = new UdpDiscoveryEndpointProvider();
// The service uses the BasicHttpBinding, so use that and insert the DiscoveryClientBindingElement at the
// top of the stack
CustomBinding binding = new CustomBinding(new BasicHttpBinding());
binding.Elements.Insert(0, discoveryClientBindingElement);
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(binding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
Console.WriteLine("Data = " + client.GetData(1));
}
任何帮助是极大的赞赏。