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

前端 未结 7 1133
既然无缘
既然无缘 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条回答
  •  时光说笑
    2021-01-11 12:08

    I solved it :D

    Here's the explanaition of the problem:

    First BAD code:

    namespace WCFServer
    {
        public class Program : IWCFService
        {
            private ServiceHost host;
    
            static void Main(string[] args)
            {
                new Program();
            }
    
            public Program()
            {
                host = new ServiceHost(typeof(Program));
    
                host.Open();
    
                Console.WriteLine("Server Started!");
    
                Console.ReadKey();
            }
    
            #region IWCFService Members
    
            public int CheckHealth(int id)
            {
                return (1);
            }
    
            #endregion
        }
    }
    

    As you can see the service contract is implemented in class hosting the service. This caused the whole error thing (maybe typeof() runs a constructor, i don't know I'm open to constructive input in this matter).

    The GOOD code:

    namespace WCFServer
    {
        public class Program
        {
            private ServiceHost host;
    
            static void Main(string[] args)
            {
                new Program();
            }
    
            public Program()
            {
                host = new ServiceHost(typeof(WCF));
    
                host.Open();
    
                Console.WriteLine("Server Started!");
    
                Console.ReadKey();
            }
        }
    
        public class WCF : IWCFService
        {
    
            #region IWCFService Members
    
            public int CheckHealth(int id)
            {
                return (1);
            }
    
            #endregion
        }
    }
    

    Service Contract for both files:

    [ServiceContract]
    public interface IWCFService
    {
        [OperationContract]
        int CheckHealth(int id);
    }
    

    App.config

    
    
        
            
                
            
        
        
            
                
                    
                        
                            
                        
                    
                
            
        
        
            
                
            
        
    
    

提交回复
热议问题