none
WCF - net.tcp binding - No connection could be made because the target machine actively refused it RRS feed

  • Question

  • I am having this issue over Windows 8 x 64 as well as Windows 7 x 64 OS. I have a simple WCF service which uses net.tcp binding. 

    I have followed many articles to write this simple service including this following on MSDN

    Hosting and Consuming WCF services : https://msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic4

    How to host WCF service in Manged Windows Service: https://msdn.microsoft.com/en-us/library/ms733069(v=vs.110).aspx

    Here is my config file. 

    <system.serviceMo

    del> <bindings /> <client /> <services> <service name="MyWcfServiceLibrary.WCFService"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="" name="TCPEndPoint" contract="MyWcfServiceLibrary.IWCFService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" name="mexEndPoint" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="net.tcp://127.0.0.1:8523/MyWCFService" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>

    After installing this WCF service using InstallUtil.exe as a windows 
    service, I can see the service getting installed correctly and it 
    runs without any error. I have tried to add traces on OnStart and 
    OnStop methods of Windows service and found nothing wrong.
    As soon as I try to reference this endpoint from another solution, I 
    get following error.

    Could not connect to net.tcp://localhost:8523/Inbound. The connection 
    attempt lasted for a time span of 00:00:00.9531494. TCP error code 
    10061: No connection could be made because the target machine 
    actively refused it 127.0.0.1:8523.
    I have already tried most of the posts related to this issue over the internet and tried following without success. 

    1. Open firewalls.
    2. Uninstalling antivirus software.
    3. Tried different ports.
    4. Explicitly created inbound and outbound rules for these ports in firewall.
    5. Used SvcUtil to generate Service Proxy file and reference it to create service reference – Give same error though

    My TCP port sharing service and TCP adaptor listener services are ON and also I have turned ON necessary Windows features (Windows Communication Foundation Non-HTTP Activation) required to run these TCP services.

    One thing I can see that I can always access this service in the same solution in another project because, visual studio creates a Wcf Service Host (WcfSvcHost) immediately when I try to reference this service, which finds all endpoints in all project in current solution and loads them up. As long as this host is running, I can reference this service in another solution as well without any issues. 

    The windows service which hosts my service has following code.

    public partial class MyWCFService : ServiceBase
        {
            internal static ServiceHost myServiceHost = null; 
    
            public MyWCFService()
            {
                InitializeComponent();
                ServiceName = "MyWCFService";
            }
    
            protected override void OnStart(string[] args)
            {            
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                }
                myServiceHost = new ServiceHost(typeof(MyWCFService));
                myServiceHost.Open();            
            }
    
            protected override void OnStop()
            {            
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                    myServiceHost = null;
                }
            }        
        }

    Clearly this creates the instance of ServiceHost and runs it. I have verified this with some logs that it gets executed successfully when service starts. But I am not able to see this process running in task manager when my service starts. I could see this service when Visual studio runs it when I am debugging in the same solution or trying to reference in same solution as I mentioned above.

    Now my question is, am I supposed to see this WcfSvceHost service in task manager when my service starts.? I believe I should. Please correct me if I 
    am wrong.  

    Do I need to grant any special permissions to my windows service to run this ServiceHost or the issue is completely different. Am I overlooking something very obvious here? Any help will be highly appreciated. Thanks.

     
    • Edited by VipinM2015 Tuesday, December 29, 2015 3:53 PM
    Tuesday, December 29, 2015 3:42 PM

Answers

  • The issue was small error in the following code. 

    myServiceHost = new ServiceHost(typeof(MyWCFService));

    The MyWCFService is actually windows service class which is getting started here. I need to specify actual WCFService library service class name here instead. It works fine now. Thanks everyone.

    • Marked as answer by VipinM2015 Tuesday, December 29, 2015 5:46 PM
    • Edited by VipinM2015 Tuesday, December 29, 2015 5:53 PM
    Tuesday, December 29, 2015 5:46 PM

All replies

  • Now my question is, am I supposed to see this WcfSvceHost service in task manager when my service starts.? I believe I should. Please correct me if I 
    am wrong.  

    There is a Services tab in Task Manager at least on Win 10. You can also use Process Explorer (free) that is a more powerful Task Manager you can use.

    Maybe the WCF service is just not listening on the port, because the Windows Service is not running.

    http://active-ports.software.informer.com/1.4/

    http://www.makeuseof.com/tag/process-explorer-the-most-powerful-task-manager-replacement-windows/

    Tuesday, December 29, 2015 4:06 PM
  • I can see the service running. In fact I have a log file which logs successful start of service and also event viewer shows service started successfully. I do not see the WcfSvcHost started in any process or service in task manager. Also I have checked all switches on NetStat command, TCPView app as well as tried using Telnet commands to access the ports configured. No success.
    • Edited by VipinM2015 Tuesday, December 29, 2015 4:34 PM
    Tuesday, December 29, 2015 4:33 PM
  • The issue was small error in the following code. 

    myServiceHost = new ServiceHost(typeof(MyWCFService));

    The MyWCFService is actually windows service class which is getting started here. I need to specify actual WCFService library service class name here instead. It works fine now. Thanks everyone.

    • Marked as answer by VipinM2015 Tuesday, December 29, 2015 5:46 PM
    • Edited by VipinM2015 Tuesday, December 29, 2015 5:53 PM
    Tuesday, December 29, 2015 5:46 PM