c# - 远程主机使用 WCF 关闭了现有连接
问题描述
我已经使用 net tcp 绑定开发了 WCF windows 服务。当 wcf 客户端和 wcf 服务都在域中(在两个不同的系统中)时,它工作正常
当两个系统都在工作组而不在域中时出现错误。
抛出异常。来源:System.ServiceModel 4.0.0.0。异常详细信息:System.ServiceModel.CommunicationException:套接字连接已中止。这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题造成的。本地套接字超时为“04:59:59.7955781”。---> System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭
我尝试过的事情:
- 关闭防火墙
- 检查端口
- 增加超时
解决方案
首先,您需要在请求时添加 windows 凭据,因为 nettcpbinding 默认为 windows 身份验证:
ServiceReference1.CalculatorClient calculatorClient = new ServiceReference1.CalculatorClient();
calculatorClient.ClientCredentials.Windows.ClientCredential.UserName = "Administrator";
calculatorClient.ClientCredentials.Windows.ClientCredential.Password = "Password";
如果添加凭据后仍有问题,还需要添加 mex 端点:
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange"></endpoint>
这是我的 App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.serviceModel>
<services>
<service name="ConsoleApp5.CalculatorService" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:10234/GettingStarted/"/>
</baseAddresses>
</host>
<endpoint address="Test"
binding="netTcpBinding"
contract="ConsoleApp5.ICalculator"/>
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
更新
将 Mode 值设置为 Message:
<binding name="Binding">
<security mode="Message">
<message clientCredentialType="Certificate" />
</security>
</binding>
这是我的 App.config:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="Microsoft.Samples.X509CertificateValidator.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
<!-- use host/baseAddresses to configure base address provided by host -->
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8001/servicemodelsamples/service"/>
</baseAddresses>
</host>
<!-- use base address specified above, provide one endpoint -->
<endpoint address="certificate" binding="netTcpBinding" bindingConfiguration="Binding" contract="Microsoft.Samples.X509CertificateValidator.ICalculator"/>
</service>
</services>
<bindings>
<netTcpBinding>
<!-- X509 certificate binding -->
<binding name="Binding">
<security mode="Message">
<message clientCredentialType="Certificate" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata/>
<serviceCredentials>
<!--
The serviceCredentials behavior allows one to specify authentication constraints on client certificates.
-->
<clientCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</clientCertificate>
<!--
The serviceCredentials behavior allows one to define a service certificate.
A service certificate is used by a client to authenticate the service and provide message protection.
This configuration references the "localhost" certificate installed during the setup instructions.
-->
<serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
如果问题仍然存在,请随时告诉我。
推荐阅读
- ruby - 用Ruby(TCPServer)创建的简单服务器,如何从另一个IP访问?
- javascript - 滚动页面导航栏活动删除活动类
- microsoft-graph-api - 用于获取通道线程的 Microsoft Teams REST API
- r - 向量列表和序列填充
- maven - 我们可以在 selenium 中添加驱动程序作为 maven depandancy
- iframe - Rocket.Chat 自动登录
- php - 如何将父菜单和子菜单类设置为活动
- angularjs - 从外部 javascript 正确访问 django 静态文件
- html - 本地字体仅在 iPhone 和我的同事 Mac 上不显示
- javascript - infowindow.close() 无法使用 reactjs 在谷歌地图上工作