这个简单的WCF发现示例可在一台机器上运行,但是当客户端和服务器在没有防火墙的同一子网中的不同机器上运行时,它将无法正常工作。我想念什么?

using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.ServiceModel;
using System.ServiceModel.Discovery;

namespace WCFDiscovery
{
   class Program
   {
      static void Main(string[] args)
      {
         try { if (args.Length > 0) StartClient(); else StartServer(); }
         catch (Exception ex) { Console.WriteLine(ex); }
         finally { Console.WriteLine("press enter to quit..."); Console.ReadLine(); }
      }

      private static void StartServer()
      {
         var ipAddress = Dns.GetHostAddresses(Dns.GetHostName()).First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
         var address = new Uri(string.Format("net.tcp://{0}:3702", ipAddress));
         var host = new ServiceHost(typeof(Service), address);
         host.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), address);
         host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
         host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
         host.Open();
         Console.WriteLine("Started on {0}", address);
      }

      private static void StartClient()
      {
         var dc = new DiscoveryClient(new UdpDiscoveryEndpoint());
         Console.WriteLine("Searching for service...");
         var findResponse = dc.Find(new FindCriteria(typeof(IService)));
         var response = ChannelFactory<IService>.CreateChannel(new NetTcpBinding(), findResponse.Endpoints[0].Address).Add(1, 2);
         Console.WriteLine("Service response: {0}", response);
      }
   }

   [ServiceContract] interface IService { [OperationContract] int Add(int x, int y); }

   class Service : IService { public int Add(int x, int y) { return x + y; } }
}

最佳答案

我已经在2台不同的计算机(笔记本电脑(Win7)和塔式PC(Win8)、. NET FW 4.5,相同的WiFi网络)上运行您的代码,并收到以下异常:

A remote side security requirement was not fulfilled during authentication. Try increasing the ProtectionLevel and/or ImpersonationLevel.

这是由于未指定服务安全性,已找到端点。因此,其他答案中的人是正确的-这是一个网络问题,无法通过更正代码来解决。
我还要补充说明,问题的另一个可能原因可能是网络交换机不允许UDP广播。

关于c# - 在关闭了防火墙的情况下,WCF发现在同一子网上的两台计算机之间不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14059493/

10-10 17:49