I\'m trying to communicate between WCF hosted in Windows Service and my service GUI. The problem is when I\'m trying to execute OperationContract method I\'m getting
I encountered this exception for the same reason as the OP but I couldn't move my service's implementation into a new class. So, I found another solution. ServiceHost
has a second overload that takes an instance of a service. Thus, here are the changes applied to the OP's 'BAD' code:
namespace WCFServer
{
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] // required
public class Program : IWCFService
{
private ServiceHost host;
static void Main(string[] args)
{
new Program();
}
public Program()
{
host = new ServiceHost(this); // changed
host.Open();
Console.WriteLine("Server Started!");
Console.ReadKey();
}
#region IWCFService Members
public int CheckHealth(int id)
{
return (1);
}
#endregion
}
}