Drunken Code Monkey
Drunken Code Monkey

Reputation: 1839

WCF, Net.Tcp endpoint, code-first

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.

Upvotes: 0

Views: 449

Answers (2)

Seymour
Seymour

Reputation: 7067

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

Upvotes: 2

Enes
Enes

Reputation: 1125

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.

Upvotes: 1

Related Questions