如何解决“ChannelDispatcher无法打开其IChannelListener”错误?
我正在尝试在Windows服务中托管的WCF和我的服务GUI之间进行通信。 问题是当我尝试执行OperationContract方法时,我得到了
“’net.tcp:// localhost:7771 / MyService’中的ChannelDispatcher与合同’”IContract“’无法打开其IChannelListener。”
我的app.conf看起来像这样:
端口7771正在侦听(使用netstat检查),svcutil能够为我生成配置。
任何建议,将不胜感激。
从exception堆栈跟踪
Server stack trace: at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter) at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
有一个内部的exeption(但不在Exeption.InnerExeption下但在Exeption.Detail.InnerExeption下 – ToString()方法没有显示)
已经存在URI’net.tcp:// localhost:7771 / MyService’的注册。
但是我的服务只在app.config文件中指定了这个URI。 在整个解决方案中,此URI在服务器中一次和客户端一次。
对于这种类型的exception,内部exception具有对诊断问题有用的信息。 这是一个相当普遍的错误,可能是由一堆不同的东西引起的。
我解决了它:D
这是问题的解释:
第一个BAD代码:
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 } }
如您所见,服务合同是在托管服务的类中实现的。 这导致了整个错误的事情(也许typeof()运行一个构造函数,我不知道我在这个问题上愿意接受建设性的输入)。
好的代码:
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 } }
两个文件的服务合同:
[ServiceContract] public interface IWCFService { [OperationContract] int CheckHealth(int id); }
App.config中
我知道你已经解决了这个问题,但没有人指出它解决问题的原因,以及导致错误的原因。 你的第一个代码的问题是你的程序(类程序)指定了对本身为“类程序”的类的开放调用,换句话说,它是RECURSIVE。 难怪它无法打开听众错误! 这是一个很好的! 🙂
也许该端口已被您机器上的其他程序使用,如防病毒程序? 或者它是Windows保留端口。 你能尝试将端口设置为11111吗?
我认为这部分配置被忽略了
当你设置
我出于与OP相同的原因遇到了这个exception,但我无法将我的服务实现转移到新类中。 所以,我找到了另一个解决方案 ServiceHost
有第二个重载,它接受一个服务实例。 因此,以下是应用于OP的’BAD’代码的更改:
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 } }
在再次调用服务之前应该有一个host.Close()
调用。 因此使用try,catch和finally块。 在进行第二次呼叫之前,最后关闭连接。