WCF TCP绑定在自托管的WinForms应用程序中

时间:2013-05-27 18:43:12

标签: vb.net winforms wcf wcf-binding wcf-security

我决定在我自己托管的wcf应用程序中进行net.tcp绑定(使用传输级加密)。

虽然我在获取有关自托管wcf应用程序主题的信息时非常有趣,但我当前的工作解决方案并没有隐式指定绑定,所以我猜它默认为BasicHttp。

我不确定如何“添加/更改”对net.tcp和传输级加密的绑定?我也很好奇“测试”我的tcp安全连接。用于运行某些测试安全方案的内容是什么?

工作代码:未指定隐式绑定...

'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()

更新

对此的更新......真的开始在这里刮开我的脑袋......

我现在有:

更改了绑定到tcp 创建,安装和引用自签名证书 trace显示没有有用的信息......

这是我的新代码:

 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()

当试图引用它时,我的跟踪带有“警告”,没有真正的信息为什么......?

<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>

我仍然无法获得可行的解决方案......这是我当前的代码

1 个答案:

答案 0 :(得分:0)

您没有在原始代码中指定绑定您指定了http协议(通过您的地址),这就是您获得BasicHttpBinding的原因(这是http的默认值。)

以下代码段应该让您朝着正确的方向前进:

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

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

请注意地址中的net.tcp协议。从理论上讲,这应该足以让您获得默认的NetTCPBinding。如果你想明确定义它,这是一种方式:

创建NetTCP端点并将其添加到ServiceHost

Dim myservicehost As New ServiceHost(GetType(plutocomm))

请注意,您尚未创建端点。以下代码将为您的服务创建端点:

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

请注意,SecurityMode.TransportNetTcpBinding默认,因此您无需明确设置它。如果你正在使用证书,你可能需要告诉绑定在哪里找到证书(看看MSDN Example)。

这只是您可以设置绑定的几种方法之一(例如,您也可以在配置文件中执行此操作)。

这里的关键是WCF(版本4及更高版本)具有默认设置,除非您明确指定其他内容,否则它们将起作用。

相关问题