0

I have decided to do net.tcp binding in my self hosted wcf app (with transport level encryption).

While I had quite an interesting time in getting info on the subject of making a self hosted wcf app work, my current working solution does not implicitly specify binding, so I guess it defaults to BasicHttp.

I am unsure of how to "add/change" the binding to net.tcp and transport level encryption ? I am also curious in "testing" my tcp secured connection. What would be used to run some test security scenarios?

Working Code: No implicit binding specified...

'create URI
        Dim myServiceAddress As New Uri("http://" & LocalIpAddress & ":" & tcp_port & "/" & servicename)

        Dim myservicehost As New ServiceHost(GetType(plutocomm), myServiceAddress)

        ' Enable metadata publishing.
        Dim smb As New ServiceMetadataBehavior()
        smb.HttpGetEnabled = True
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
        myservicehost.Description.Behaviors.Add(smb)

        myservicehost.Open()

UPDATE

An Update on this... really starting to scratch my head here..

I have now:

Changed binding to tcp Created, installed and referenced self signed certificate trace shows no helpfull information...

Here's my new code:

 Dim myServiceAddress As New Uri("net.tcp://" & localIpAddress & ":" & tcp_port & "/" & servicename)

        Dim myservicehost As New ServiceHost(GetType(plutocomm))
        'create binding
        Dim myNetTcpBinding = New NetTcpBinding()
        myNetTcpBinding.Security.Mode = SecurityMode.Transport
        myNetTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None



        ' Enable metadata publishing.
        Dim smb As New ServiceMetadataBehavior()
        smb.HttpGetEnabled = False
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
        myservicehost.Description.Behaviors.Add(smb)

        myservicehost.AddServiceEndpoint(GetType(Iplutocomm), myNetTcpBinding, myServiceAddress)
        myservicehost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "louisvantonder")


        myservicehost.Open()

Heres my trace with a "warning" when trying to reference it, no real info on why...?

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>262171</EventID><Type>3</Type><SubType Name="Warning">0</SubType><Level>4</Level><TimeCreated SystemTime="2013-05-28T01:16:53.0868677Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{a696dcda-b24a-4838-9f23-cd0d67690af7}" /><Execution ProcessName="pluto" ProcessID="8472" ThreadID="3" /><Channel /><Computer>LOUISVANTONDER</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Warning"><TraceIdentifier>http://msdn.microsoft.com/en-ZA/library/System.ServiceModel.Channels.SocketConnectionAbort.aspx</TraceIdentifier><Description>SocketConnection aborted</Description><AppDomain>pluto.exe</AppDomain><Source>System.ServiceModel.Channels.SocketConnection/37489757</Source></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent>

I still cant get a workable solution... here is my current code

Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
  • There must be a million tutorials on the net, what is it you are having problems with? – nvoigt May 27 '13 at 18:47
  • I'm surely missing some fundamentals... as stated, the above code was "mangled" together to get some grasp on the self hosted implementation. I am uncertain where to define the binding type, and security? – Louis van Tonder May 27 '13 at 18:48
  • So what is your best try at TCP with transport encryption? What is your problem besides not having googled it? – nvoigt May 27 '13 at 18:51
  • 2
    He might HAVE Googled it, nvoigt. It can actually be quite hard to find decent answers online, especially depending on one's search skills, thus why SO is here. (That said, Louis, if you haven't Googled it, you might want to anyway.) – CodeMouse92 May 27 '13 at 18:54
  • Guys, I've been at this for the last 48 hours... as in my original post.... I find "theory / specification" on what needs to happen, I am yet to find a self hosted example/info on manually creating the service host and specifying the binding... – Louis van Tonder May 27 '13 at 19:02
  • I.E, this is great.. http://msdn.microsoft.com/en-us/library/ms729700.aspx , but is focuses on iis hosting, and specifics to binding, not how to implement the binding, once created... – Louis van Tonder May 27 '13 at 19:04
  • *If I specify net.tcp in the URI, it seems that it might do the trick, but I still fail to see where this would apply? Dim b As NetTcpBinding = New NetTcpBinding() b.Security.Mode = SecurityMode.Transport b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate – Louis van Tonder May 27 '13 at 19:29
  • How about reading [this](http://stackoverflow.com/questions/703051/wcf-net-tcp-multiple-bindings-same-port-different-ip-addresses?rq=1) it's almost identical to your scenario, just that it's C#. It's the first link in the related section to the right on this page. It will give you a good start to produce some code and come up with an actual problem we may be able to help with. – nvoigt May 27 '13 at 19:53
  • Updated original question – Louis van Tonder May 28 '13 at 01:27

1 Answers1

0

You didn't specify a binding in your original code and you specified a protocol of http (via your address), which is why you're getting BasicHttpBinding (which is the default for http).

The following code snippet should get you going in the right direction:

Dim myServiceAddress As New Uri("net.tcp://" & LocalIpAddress & ":" & tcp_port & "/" & servicename)

Dim myservicehost As New ServiceHost(GetType(plutocomm), myServiceAddress)

Note the net.tcp protocol in the address. In theory, that should be sufficient to get you the default NetTCPBinding. If you want to explicitly define it, here's one way:

Create a NetTCP endpoint and add it to your ServiceHost:

Dim myservicehost As New ServiceHost(GetType(plutocomm))

Note that you're not creating the endpoint yet. The following code will create the endpoint for your sevice:

Dim myNetTcpBinding = New NetTcpBinding()
myNetTcpBinding.Security.Mode = SecurityMode.Transport
myNetTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
myservicehost.AddServiceEndpoint(typeof(plutocomm), myNetTcpBinding, myServiceAddress)

Note that SecurityMode.Transport is the default for NetTcpBinding, so you don't need to explicitly set it. If you're using certificate you'll probably need to tell the binding where to find the certificate (take a look at the MSDN Example).

This is just one of several ways you can set up the binding (you can also do it in the config file, for example).

The key here is that WCF (with version 4 and later) has default settings that will come into play unless you explicitly specify something else.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • Thanks Tim. I have fiddled with pretty much the same thing since posting this question. I see examples where a servicehost is created, with an endpoint, and then host.addServicendpoint is called to add "another" endpoint... does this last endpoint "replace" the initial one, With the second one, I can specify binding info... – Louis van Tonder May 27 '13 at 20:40
  • Tim, thank you again for your detailed reply. I appreciate the effort. I am running into an issue where it seems I need to specify a certificate... I do not wish to use a certificate at this time. From my research, it was my understanding that tcp binding uses its own operating specific security on the tcp ? Can I use your provided example without using a certificate? I have credentialtype set to "none". – Louis van Tonder May 27 '13 at 21:59
  • I have clientcredentialType set to "none" – Louis van Tonder May 27 '13 at 22:06
  • I have removed the clientcredentialtype line completely, and I can compile. I do however receive an error when trying to connect to the service.. – Louis van Tonder May 27 '13 at 22:24
  • Metadata contains a reference that cannot be resolved: 'ne.tcp-URI'. 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.4489685'. An existing connection was forcibly closed by the remote host If the service is defined in the current solution, try building the solution and adding the service reference again. – Louis van Tonder May 27 '13 at 22:25
  • Updated the original question – Louis van Tonder May 28 '13 at 01:26
  • @LouisvanTonder - Sorry - was away from my computer for a while. Yes, you can set the ClientCredentialType to None - that is valid. Regarding your error, did it actually say 'ne.tcp-URI'? I ask because it should be ne**t**.tcp (note the t at the end of net). – Tim May 28 '13 at 02:38
  • HI, sorry, spelling mistake on my part, I just removed the URI for security purposes. – Louis van Tonder May 28 '13 at 08:19