CallumVass
CallumVass

Reputation: 11458

Unable to add net.tcp as service reference

I have created a net.tcp WCF service like so:

const string tcpUri = "net.tcp://localhost:9038";
var netTcpHost = new WebServiceHost(typeof(DashboardService), new Uri(tcpUri));
netTcpHost.AddServiceEndpoint(typeof(IDashboardService), new NetTcpBinding(), 
                                                             "/dashboard");

netTcpHost.Open();

Console.WriteLine("Hosted at {0}. Hit any key to shut down", tcpUri);
Console.ReadKey();

netTcpHost.Close();

Here is my IDashboardService and DashboardSerivce definitions:

[ServiceContract]
public interface IDashboardService
{
    [OperationContract]
    PositionDashboard Loader();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class DashboardService : IDashboardService
{
    public PositionDashboard Loader()
    {
        ...
    }
}

However, when I try to connect to the service with the WCF Test Client, I get the following error:

Error: Cannot obtain Metadata from net.tcp://localhost:9038/dashboard
The socket connection was aborted. This could be caused by an error processing your 
message or a receive timeout being exceeded by the remote host, or an underlying 
network resource issue. Local socket timeout was '00:04:59.9890000'.    An existing 
connection was forcibly closed by the remote host

Upvotes: 1

Views: 561

Answers (1)

Belogix
Belogix

Reputation: 8147

The error is explicitly mentioning "Cannot obtain Metadata..." so it would be worth checking that you have a TCP Mex Endpoint. It should look something like this in config:

<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />

Alternatively, if you you want to do then you need to reference System.ServiceModel.Discovery and then your code should look like this:

const string tcpUri = "net.tcp://localhost:9038";
using (var netTcpHost = new WebServiceHost(
    typeof(DashboardService),
    new Uri(tcpUri)))
{
    netTcpHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
    netTcpHost.AddServiceEndpoint(
        typeof(IMetadataExchange),
        MetadataExchangeBindings.CreateMexTcpBinding(),
        "mex");
    netTcpHost.AddServiceEndpoint(
        typeof(IDashboardService),
        new NetTcpBinding(),
        "dashboard");

    netTcpHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
    netTcpHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

    netTcpHost.Open();
    Console.WriteLine("Hosted at {0}. Hit any key to shut down", tcpUri);
    Console.ReadLine();

    netTcpHost.Close();
}

Upvotes: 1

Related Questions