0

I have a WCF service, this service works perfectly well on a WSHttp endpoint, running as a Windows service under ServiceHost. However I want to move to a TCP endpoint, because of scalability. For the life of me I cannot figure out how to host it correctly. Here is my host's OnStart routine, in VB:

Protected Overrides Sub OnStart(ByVal args() As String)
    If _svcHost IsNot Nothing Then
        _svcHost.Close()
    End If

    _svcHost = New ServiceHost(GetType(AutoTestsDataService), New Uri("net.tcp://" & GetIPv4Address() & ":8000"))

    Dim metaDataBehavior = _svcHost.Description.Behaviors.Find(Of ServiceMetadataBehavior)()
    If metaDataBehavior Is Nothing Then
        _svcHost.Description.Behaviors.Add(New ServiceMetadataBehavior() With {.HttpGetEnabled = False})
    Else
        metaDataBehavior.HttpGetEnabled = False
    End If

    _svcHost.AddServiceEndpoint(GetType(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding, "mex")
    _svcHost.AddServiceEndpoint(GetType(AutoTestsDataService), New NetTcpBinding(SecurityMode.None, False), ServiceName)

    Dim debugBehavior As ServiceDebugBehavior = _svcHost.Description.Behaviors.Find(Of ServiceDebugBehavior)()
    If debugBehavior Is Nothing Then
        _svcHost.Description.Behaviors.Add(New ServiceDebugBehavior() With {.IncludeExceptionDetailInFaults = My.Settings.flagDebug})
    Else
        debugBehavior.IncludeExceptionDetailInFaults = My.Settings.flagDebug
    End If

    Try
        _svcHost.Open()
    Catch ex As Exception
        _svcHost.Abort()
    End Try
End Sub

As is, the code compiles fine, installs fine, and the Windows service starts up fine. But there is nothing listening on port 8000. I have made sure the Net.Tcp Listener and Port Sharing services are running properly. I chose to not use the config file at all because I had lots of problems in the past and to me putting code in a config file is bad, nevermind what Microsoft wants me to believe. A code-first implementation is always easier to understand than XML anyways, and to me the above code puts all the right parts in the right places. It just refuses to work. Like I said I can stick with WSHttp, but I would prefer to understand why Net.Tcp is not working.

Drunken Code Monkey
  • 1,796
  • 1
  • 14
  • 18
  • Is `_svcHost.Open()` throwing? If so what is the exception? – ErnieL Apr 17 '14 at 05:07
  • Negative, everything behaves as it should, there is just nothing listening on port 8000. No exception is thrown at all. – Drunken Code Monkey Apr 17 '14 at 12:33
  • Did you tried http://stackoverflow.com/questions/1479081/wcf-there-was-no-endpoint-listening-at-net-tcp-querier-svc-that-could-a?rq=1 – Pranav Singh Apr 24 '14 at 04:56
  • That link points to an IIS-hosted service. Mine is hosted through ServiceHost, and as far as I know there is no protocol to enable. So long as the Net.Tcp Listener service is running it should work. – Drunken Code Monkey Apr 24 '14 at 12:19

2 Answers2

2

The best way to determine what the WCF Service listener is doing during startup is to enable WCF Tracing. Therefore, I recommend you configure tracing on the service, which should provide detailed information about any underlying exceptions occurring at startup.

WCF tracing provides diagnostic data for fault monitoring and analysis. You can use tracing instead of a debugger to understand how an application is behaving, or why it faults. You can also correlate faults and processing across components to provide an end-to-end experience, including traces for process milestones across all components of the applications, such as operation calls, code exceptions, warnings and other significant processing events.

http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

Seymour
  • 7,043
  • 12
  • 44
  • 51
  • I completely agree, but the whole point is that there is no fault. The Windows Service starts up properly, there is just nothing listening on the port I told it to listen on. The service also works perfectly fine in WsHttp. The WCF service is not the issue here, even a "Hello World" service will not work, since I cannot add a service reference in the first place. The service should be listening on TCP port 8000, but netstat -a tells me nothing is listening on port 8000. Yet the Windows service is running fine, and its event log is empty apart from start and stop messages. – Drunken Code Monkey Apr 23 '14 at 20:24
  • I think I understand the scenario … the service starts fine, no windows errors, but netstat confirms that WCF is not listening on the designated port. In my experience, in this scenario (not uncommon) the WCF trace file often reveals the issue. – Seymour Apr 24 '14 at 10:22
  • I have problems with some of my Net.TCP services not working correctly until the Net.Tcp Listener Adapter Service or Windows Process Activation Service on that machine is restarted. If you restart the Windows process Activation Service it should restart both. – Aaron Havens Apr 25 '14 at 14:49
1

The only problem I could see here is the method GetIPv4Address() and what it returns. Usually best choice is to use localhost, host name, or 0.0.0.0.

Enes
  • 1,115
  • 7
  • 12
  • The GetIPv4Address gets the specific IP address of the interface I want the service to listen on. There are multiple network interfaces on this machine. – Drunken Code Monkey Sep 19 '14 at 00:01