How to solve “The ChannelDispatcher is unable to open its IChannelListener” error?

前端 未结 7 1286
既然无缘
既然无缘 2021-01-11 11:51

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

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-11 12:00

    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
        }
    }
    

提交回复
热议问题