4
votes

I have created the WCF service and trying to host in Managed Windows Service (followed article). The service is up and running in services.

When trying to add the URL in the client application (net.tcp://localhost:8000/UserManagement) I get the error:

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8000/UserManagement'. Could not connect to net.tcp://localhost:8000/UserManagement. The connection attempt lasted for a time span of 00:00:00.9531433. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8000. No connection could be made because the target machine actively refused it 127.0.0.1:8000 If the service is defined in the current solution, try building the solution and adding the service reference again.

Service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;

namespace AddUser
{
public class UserManagement : IUserManagement
{

    public bool AddUser(string strName, DateTime dtDOB, string strGender, string strRole)
    {

        return true;
    }
}

[ServiceContract]
public interface IUserManagement
{
    [OperationContract]
    bool AddUser(string strLname,string strFName, string strUname, string strPswd, DateTime dtDOB, string strGender, string strRole, string strHobbies);

}

public class UserManagementService : ServiceBase
{
    public ServiceHost serviceHost = null;
    public UserManagementService()
    {
        ServiceName = "WCFUserManagementService";
    }

    public static void Main()
    {
        ServiceBase.Run(new UserManagementService());
    }

    protected override void OnStart(string[] args)
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
        }                        
        serviceHost = new ServiceHost(typeof(UserManagementService));
        serviceHost.Open();
    }

    protected override void OnStop()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }
}

[RunInstaller(true)]
public class ProjectInstaller : Installer
{
    private ServiceProcessInstaller process;
    private ServiceInstaller service;

    public ProjectInstaller()
    {
        process = new ServiceProcessInstaller();
        process.Account = ServiceAccount.LocalSystem;
        service = new ServiceInstaller();
        service.ServiceName = "WCFUserManagementService";
        Installers.Add(process);
        Installers.Add(service);
    }
}

}

app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <services>
        <service behaviorConfiguration="AddUser.UserManagementServiceBehavior" name="AddUser.UserManagement">
        <endpoint address="" binding="netTcpBinding" contract="AddUser.IUserManagement"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        <host>
        <baseAddresses>
        <add baseAddress="net.tcp://localhost:8000/UserManagement" />
        </baseAddresses>
        </host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="AddUser.UserManagementServiceBehavior">
                <serviceMetadata httpGetEnabled="false"/>
                <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>
</configuration>
2
shouldn't baseAddress be localhost:8000 like in the examplePetesh
i tried even with localhost:8000 still not workingBABA

2 Answers

1
votes

You need to use the address

net.tcp://localhost:8000/UserManagement/mex

when you are configuring your service reference.

Alternatively your metadata endpoint should use the mexHttpBinding and you should set your httpGetEnabled to true in your service behavior

<serviceMetadata httpGetEnabled="true"/>
1
votes

I also faced the same issue after following MSDN link you provided. There is a error in that code.

In you OnStart method,

serviceHost = new ServiceHost(typeof(UserManagementService));

instead of creating ServiceHost for UserManagementService, use your actual WCF service class name here. This line of code will create an instance of windows service and not the WCF service. I was able to fix mine using that.