This simple WCF discovery example works on a single machine but when the client and server are running on separate machines in the same subnet with no firewall, it doesn't work. What am I missing?
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; } }
}
I've run your code on 2 different machines (notebook (Win7) and tower PC (Win8), .NET FW 4.5, same WiFi network) and received following exception:
A remote side security requirement was not fulfilled during authentication. Try increasing the ProtectionLevel and/or ImpersonationLevel.
This one is due to the service security being underspecified, the endpoint were found. So, the guys in other answers are right - this is a network problem which can't be fixed by correcting code. I'd add that another possible source of the problem may be the network switch not permitting UDP broadcasts.
To be clear, is the windows firewall also turned off?
Also make sure you're binding your server to an address that the other computer uses to communicate with it.
Localhost or 127.0.0.1 will likely not pick up a connection to its externally(to the host) addressable IP which is what the multicast discovery packets would arrive on.
MSDN instructions on turning the windows firewall off
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With