引用视频tutorial关于 Windows 服务中的 WCF 服务,我创建了一个示例 WCF 服务,并在 Windows 服务中使用 netTcpBinding 托管该服务。(因为我希望此 WCF 服务作为 Windows 服务运行)
它是一个简单的服务,可以添加/删除/加载员工详细信息,并由 Windows 表单应用程序使用。当我构建整个解决方案(由 wcf 服务 + Windows 服务 + 客户端应用程序组成)时,效果很好,但是当我想要验证我的客户是否直接引用解决方案中的项目,因此我从我的解决方案中排除了这两个服务(wcf+windows)。它停止工作并抛出错误,读取:
无法连接到 net.tcp://localhost:8010/EmployeeService.Service1/。连接尝试持续的时间跨度为 00:00:02.0180000。 TCP错误代码10061:无法建立连接,因为目标机器主动拒绝它127.0.0.1:8010。
可能有助于回答的要点:
- WCF 服务和 Windows 服务具有相同的 app.config
- Windows 服务作为服务运行
这是我的客户端 app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpEndPoint" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8010/EmployeeService.Service1/"
binding="netTcpBinding" bindingConfiguration="netTcpEndPoint"
contract="Service1.IService1" name="netTcpEndPoint">
<identity>
<userPrincipalName value="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4d1d7c1d6e4c7cbc9d4c5cadd8ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
任何帮助将不胜感激......
最佳答案
我自己找到了答案,希望它能帮助其他人寻找答案......
我发现worker_DoWork()事件没有触发,所以添加worker.RunWorkerAsync();如下面的代码所示到您的 Windows 服务
protected override void OnStart(string[] args)
{
worker = new BackgroundWorker();
worker.RunWorkerAsync();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
}
从客户端项目中删除服务引用,然后再次添加它,因为它会更改您的 app.config 文件。
关于windows-services - 无法连接到 net.tcp ://localhost. .. TCP 错误代码 10061,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6747952/