title: WCF TCP Binding in a self hosted WinForms App
body: <p>I have decided to do net.tcp binding in my self hosted wcf app (with transport level encryption).</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Working Code: No implicit binding specified...</p>
  
  <pre><code>'create URI
          Dim myServiceAddress As New Uri("http://" &amp; LocalIpAddress &amp; ":" &amp; tcp_port &amp; "/" &amp; 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()
  </code></pre>
  
  <h3>UPDATE</h3>
  
  <p>An Update on this... really starting to scratch my head here..</p>
  
  <p>I have now:</p>
  
  <p>Changed binding to tcp
  Created, installed and referenced self signed certificate
  trace shows no helpfull information...</p>
  
  <p>Here's my new code:</p>
  
  <pre><code> Dim myServiceAddress As New Uri("net.tcp://" &amp; localIpAddress &amp; ":" &amp; tcp_port &amp; "/" &amp; 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()
  </code></pre>
  
  <p>Heres my trace with a "warning" when trying to reference it, no real info on why...?</p>
  
  <pre><code>&lt;E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"&gt;&lt;System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"&gt;&lt;EventID&gt;262171&lt;/EventID&gt;&lt;Type&gt;3&lt;/Type&gt;&lt;SubType Name="Warning"&gt;0&lt;/SubType&gt;&lt;Level&gt;4&lt;/Level&gt;&lt;TimeCreated SystemTime="2013-05-28T01:16:53.0868677Z" /&gt;&lt;Source Name="System.ServiceModel" /&gt;&lt;Correlation ActivityID="{a696dcda-b24a-4838-9f23-cd0d67690af7}" /&gt;&lt;Execution ProcessName="pluto" ProcessID="8472" ThreadID="3" /&gt;&lt;Channel /&gt;&lt;Computer&gt;LOUISVANTONDER&lt;/Computer&gt;&lt;/System&gt;&lt;ApplicationData&gt;&lt;TraceData&gt;&lt;DataItem&gt;&lt;TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Warning"&gt;&lt;TraceIdentifier&gt;http://msdn.microsoft.com/en-ZA/library/System.ServiceModel.Channels.SocketConnectionAbort.aspx&lt;/TraceIdentifier&gt;&lt;Description&gt;SocketConnection aborted&lt;/Description&gt;&lt;AppDomain&gt;pluto.exe&lt;/AppDomain&gt;&lt;Source&gt;System.ServiceModel.Channels.SocketConnection/37489757&lt;/Source&gt;&lt;/TraceRecord&gt;&lt;/DataItem&gt;&lt;/TraceData&gt;&lt;/ApplicationData&gt;&lt;/E2ETraceEvent&gt;
  </code></pre>
  
  <p>I still cant get a workable solution... here is my current code</p>