Bruno Klein
Bruno Klein

Reputation: 3367

Getting Connection Refused with WCF Service

I made an service using WCF and can't connect to it, I keep getting the following error:

Could not connect to net.tcp://localhost:5555/ControlChannel. The connection attempt lasted for a time span of 00:00:02.0139182. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:5555.

This is the code:

Contract:

[ServiceContract(CallbackContract = typeof(IControlChannelCallback))]
public interface IControlChannel
{
    [OperationContract]
    void Join();
}

CallBackContract:

public interface IControlChannelCallback
{
    [OperationContract(IsOneWay = true)]
    void ShowMessage(string message);
}

Service:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
public sealed class ControlChannel : IControlChannel
{
    public static readonly IList<IControlChannelCallback> Callbacks = new List<IControlChannelCallback>(); 

    public void Join()
    {
        var client = OperationContext.Current.GetCallbackChannel<IControlChannelCallback>();

        if (!Callbacks.Contains(client))
            Callbacks.Add(client);

        client.ShowMessage("This message came from the server");
    }
}

Server:

public MainForm()
{
     InitializeComponent();

     using (var host = new ServiceHost(typeof(ControlChannel)))
     {
          host.Open();
     }
 }

Client:

public MainForm()
{
     InitializeComponent();

     var callback = new ControlChannelCallbackClient();

     using (var factory = new DuplexChannelFactory<IControlChannel>(callback, "Client"))
     {
          var proxy = factory.CreateChannel();

          proxy.Join();
     }
 }

App.config client:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
      <client>
        <endpoint name="Client"
                  contract="Service.Contracts.Interfaces.IControlChannel"
                  binding="netTcpBinding"
                  address="net.tcp://localhost:5555/ControlChannel" />
      </client>
    </system.serviceModel>
</configuration>

App.config server

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
      <services>
        <service name="Service.Contracts.Objects.ControlChannel">
          <endpoint contract="Service.Contracts.Interfaces.IControlChannel"
                    binding="netTcpBinding"
                    address="net.tcp://localhost:5555/ControlChannel" >
          </endpoint>
        </service>
      </services>
    </system.serviceModel>
</configuration>

What am I doing wrong? Is it a code problem or should I start looking for problems elsewhere?

Upvotes: 1

Views: 10170

Answers (1)

Tim
Tim

Reputation: 28530

Just a hunch, but I suspect there's nothing listening because even though the application hosting your service may still be running, the service host isn't. In your constructor:

public MainForm()
{
    InitializeComponent();

    using (var host = new ServiceHost(typeof(ControlChannel)))
    {
        host.Open();
    }
}

You have your ServiceHost in a using block - once the using block exits (right before the constructor finishes), the ServiceHost will be closed and disposed of.

Chang your code to this:

public MainForm()
{
    InitializeComponent();

    var host = new ServiceHost(typeof(ControlChannel))
    host.Open();
}

You can hook into the application closing event to close the ServiceHost at the end of the program's run.

I'd also recommend wrapping the host.Open() in a try-catch block in case something goes wrong trying to open it.

Upvotes: 7

Related Questions