Vb.net 自托管WinForms应用程序中的WCF TCP绑定

Vb.net 自托管WinForms应用程序中的WCF TCP绑定,vb.net,winforms,wcf,wcf-binding,wcf-security,Vb.net,Winforms,Wcf,Wcf Binding,Wcf Security,我已决定在我的自托管wcf应用程序(使用传输级别加密)中进行net.tcp绑定 虽然我在获取关于使一个自托管wcf应用程序工作的主题的信息方面经历了相当有趣的一段时间,但我当前的工作解决方案并没有隐式地指定绑定,所以我猜它默认为BasicHttp 我不确定如何“添加/更改”到net.tcp和传输级加密的绑定?我对“测试”我的tcp安全连接也很好奇。将使用什么来运行一些测试安全场景 工作代码:未指定隐式绑定 'create URI Dim myServiceAddress As N

我已决定在我的自托管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的绑定 创建、安装和引用自签名证书 跟踪显示没有帮助的完整信息

这是我的新代码:

 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>
262171304LOUISVANTONDERhttp://msdn.microsoft.com/en-ZA/library/System.ServiceModel.Channels.SocketConnectionAbort.aspxSocketConnection 中止pluto.exeSystem.ServiceModel.Channels.SocketConnection/37489757

我还是找不到一个可行的解决办法。。。这是我的当前代码

您没有在原始代码中指定绑定,并且您指定了一个
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.Transport
NetTcpBinding
的默认值,因此不需要显式设置它。如果您使用的是证书,您可能需要告诉绑定在哪里可以找到证书(请看下面的示例)

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


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

网上一定有一百万个教程,你有什么问题?我肯定错过了一些基础知识。。。如上所述,为了更好地理解自托管实现,上述代码被“篡改”在一起。我不确定在哪里定义绑定类型和安全性?那么,您在TCP传输加密方面的最佳尝试是什么?除了没有谷歌搜索之外,你还有什么问题?他可能已经谷歌了,nvoigt。事实上,在网上很难找到合适的答案,特别是取决于一个人的搜索技能,所以这里有这样的理由。(也就是说,路易斯,如果你没有用谷歌搜索过它,你可能无论如何都会想。)伙计们,我在这已经做了48个小时了。。。就像我原来的帖子一样。。。。我找到了关于需要发生什么的“理论/规范”,我还没有找到关于手动创建服务主机和指定绑定的自托管示例/信息…谢谢Tim。自从发布这个问题以来,我一直在处理几乎相同的事情。我看到一些示例,其中创建了一个带有端点的servicehost,然后调用host.addServicendpoint来添加“另一个”端点。。。最后一个端点是否“替换”了第一个端点,使用第二个端点,我可以指定绑定信息…Tim,再次感谢您的详细回复。我感谢你的努力。我遇到了一个问题,似乎我需要指定一个证书。。。我现在不想使用证书。从我的研究中,我了解到tcp绑定在tcp上使用自己的特定于操作的安全性?我可以使用您提供的示例而不使用证书吗?我已将credentialtype设置为“无”。我已将clientcredentialType设置为“无”。我已完全删除clientcredentialType行,并且我可以编译。但是,我在尝试连接到服务时确实收到错误。元数据包含无法解析的引用:“ne.tcp URI”。套接字连接已中止。这可能是由于处理消息时出错、远程主机超过接收超时或基础网络资源问题造成的。本地套接字超时为“00:04:59.4489685”。远程主机已强制关闭现有连接。如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。