问题
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
"The ChannelDispatcher at 'net.tcp://localhost:7771/MyService' with contract(s) '"IContract"' is unable to open its IChannelListener."
My app.conf looks like that:
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding">
<security>
<transport protectionLevel="EncryptAndSign" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:7772/MyService" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior"
name="MyService.Service">
<endpoint address="net.tcp://localhost:7771/MyService" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" name="netTcp" contract="MyService.IContract" />
</service>
</services>
</system.serviceModel>
Port 7771 is listening (checked using netstat) and svcutil is able to generate configs for me.
Any suggestions would be appreciated.
Stack trace from 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)
There's one inner exeption (but not under Exeption.InnerExeption but under Exeption.Detail.InnerExeption - ToString() method doesn't show that)
A registration already exists for URI 'net.tcp://localhost:7771/MyService'.
But my service have specified this URI only in app.config file nowhere else. In entire solution this URI apears in server once and client once.
回答1:
With this type of exception it's the Inner exception that has the information that is useful to diagnose the problem. This is a rather generic error that could be caused by a bunch of different things.
回答2:
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
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WCFBehavior" />
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="tcpBinding">
<security>
<transport>
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="WCFServer.WCF">
<endpoint address="net.tcp://localhost:1111/TcpIHostMonitor" binding="netTcpBinding"
bindingConfiguration="tcpBinding" name="netTcpEndpoint" contract="WCFServer.IWCFService" />
</service>
</services>
</system.serviceModel>
回答3:
I know you already solved the problem, but nobody pointed out why it solved the problem, and what caused the error. The issue w/ your first code was that your program (class Program) specified an open call to the class that was itself "class Program", in other words, it's RECURSIVE. No wonder it was giving the unable to open the listener error! This was a good one! :-)
回答4:
maybe the port is already used by another program on your machine, like an antivirus program? Or it's a Windows reserved port. Could you try setting the port to something like 11111?
回答5:
I think this part of the configuration is ignored
<message clientCredentialType="UserName" />
when you set
<security mode = "Transport">
回答6:
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
}
}
回答7:
There should be a host.Close()
call before calling the service again. Therefore use try, catch and finally block. Close the connection in finally before making second call.
来源:https://stackoverflow.com/questions/1252791/how-to-solve-the-channeldispatcher-is-unable-to-open-its-ichannellistener-erro