繁体   English   中英

WCF端口没有监听

[英]WCF port not listening

更新

该服务正在运行,并在任务管理器中有一个pid,但是当我运行netstat -ano | find pid# netstat -ano | find pid#没有返回 - 端口没有监听。

此外,如果我运行netstat -ap tcp ,则不会列出端口。

我真的不知道如何解决这个问题。 总之,所以你不必阅读我以前的帖子:

  • 我的WCF服务在localhost下完美运行(即同一服务器上的服务和客户端)
  • 当我将服务发布到生产服务器时,该服务安装正常,但8523端口不会监听
  • 有几个类似的帖子我已经审查过,但没有一个对我有用
  • 这是我尝试的第一个WCF项目,所以我已经耗尽了我的知识,我在互联网上找不到更多的例子来尝试
  • 在这一点上,我将非常感谢任何指针

环境

  • 开发:Windows 7
  • Visual Studio:2015
  • 生产服务器:Windows Server 2008 R2 Datacenter
  • IIS版本7.5.7600.16385

介绍

我已经研究了这个问题几天了,尽管我在Stack Overflow上发现了许多类似的问题,但这些问题都没有解决我的问题; 因此,我不认为这是重复的,如果有人试图像我一样按照引用的Microsoft教程,它将特别有用

问题详情

我在https://msdn.microsoft.com/en-us/library/ff649818.aspx上关注了这篇Microsoft文章“如何:使用TCP在Windows服务中托管WCF”。

创建解决方案后,它在Visual Studio localhost环境中的开发计算机上运行完美。 本教程建议您创建一个测试客户端来访问WCF解决方案,并且测试客户端可以成功运行

当我在生产计算机(Windows Server 2008)中发布WCF服务和Windows服务时,它会再次安装而不会出错。 以下是我遵循的步骤:

IIS安装程序

  • 将net.tcp添加到已启用的协议

  • 激活'Windows Communication Foundation非HTTP激活'

  • 将net.tcp的绑定更改为8532

  • 确保'Net.Tcp Listener Adapter'正在运行

在防火墙中打开8532端口

浏览到WindowsService1.exe所在项目的bin目录,并以管理员身份运行exe

打开命令以管理员身份提示并运行Installutil WindowsService1.exe

Localhost(Visual Studio中的开发环境)与生产环境之间的差异

localhost WCF系统和生产系统之间的唯一变化是我更改了app.conf中的baseAddress

  • “的net.tcp://本地主机:8523 / CustomWCFService”

  • “的net.tcp://10.2.1.1:8523 / CustomWCFService”

这是部署的app.conf

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" /> </system.web> <system.serviceModel> <services> <service name="MyCustomServiceLib.CustomServiceLib"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract="MyCustomServiceLib.ICustomServiceLib"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="net.tcp://10.2.1.1:8523/CustomWCF" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> 

添加服务参考

当我尝试在我的应用程序中添加将使用WCF服务的服务引用时。 我收到此错误:

从地址下载元数据时出错。 请确认您输入的是有效地址

当我点击“详细信息”时,我看到了这个:

无法识别URI前缀。 元数据包含无法解析的引用:'net.tcp://10.2.1.1:8523 / CustomWCF'。 无法连接到net.tcp://10.2.1.1:8523 / CustomWCF。 连接尝试持续时间跨度为00:00:01.0312368。 TCP错误代码10061:无法建立连接,因为目标计算机主动拒绝它10.2.1.1:8523。 无法建立连接,因为目标计算机主动拒绝它10.2.1.1:8523如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

我试过了什么

在论坛上提出的许多其他WCF问题似乎都是关于让服务在localhost中工作,这对我不适用,因为我创建的解决方案在开发环境中完美运行。

一些答案建议在项目中启用tcp端口共享,但是当我尝试在我的解决方案中添加此代码时,我收到错误“不允许”。

其他问题表明url必须在生产服务器上注册,但没有效果。

在尝试了我能找到的所有论坛建议并尝试纠正我认为错误的一切之后,我仍然没有运气。

任何建议或指示将不胜感激

我没有让WCF在过去使用配置文件。 我总是在代码中创建绑定。

这个要点确实奏效了。

重要的部分是NetTcpBinding

 netTcpBinding = new NetTcpBinding
                            {
                                MaxReceivedMessageSize = int.MaxValue,
                                MaxBufferPoolSize = int.MaxValue,
                                Security = new NetTcpSecurity
                                           {
                                               Mode = SecurityMode.None,
                                               Transport = new TcpTransportSecurity()
                                           }
                            };

此外,我不会指定IP,而是让WCF将其绑定到计算机上的每个IP地址。

serviceHost.AddServiceEndpoint(typeof(ITestService), netTcpBinding, string.Format("{1}://0.0.0.0:{0}/", port, "net.tcp"));

这并不妨碍您输入特定的IP,我通常不这样做。

如果你想在IIS中运行它,你必须做更多的工作。 IIS实际上并不希望您托管未受http约束的WCF服务器。

创建ServiceHost后,您必须做一些额外的工作

 var serviceBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

if (serviceBehavior == null) return;

serviceBehavior.HttpGetEnabled = false;
serviceBehavior.HttpsGetEnabled = false;

我已添加此帖子以详细说明将David Basarab建议更改添加到我的WCF服务库项目的结果。 再次感谢大卫的帖子,但不幸的是我无法让它发挥作用。 实际上它甚至没有在Visual Studio中的localhost下运行。 大卫提到它对他有用,所以我可能在某个地方犯了错误。 希望我在这里提供的细节能够产生一些额外的指针,指出我做错了什么或帮助其他尝试David的方法。 我在创建解决方案时尝试保留Visual Studio的默认值,但David的更改除外。

服务参考错误

这是我尝试在与Windows服务和实际WCF服务库相同的计算机上的测试客户端中添加服务引用时收到的错误:

The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8523/Service1'. Could not connect to net.tcp://localhost:8523/Service1. The connection attempt lasted for a time span of 00:00:02.0028015. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8523. No connection could be made because the target machine actively refused it 127.0.0.1:8523 If the service is defined in the current solution, try building the solution and adding the service reference again.

WCF库代码

IService1.cs单位

 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace CustomServiceLibrary { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: Add your service operations here } // Use a data contract as illustrated in the sample below to add composite types to service operations. // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "CustomServiceLibrary.ContractType". [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } } 

Service1.cs代码

 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace CustomServiceLibrary { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } } } 

Server.cs类代码

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel; using System.ServiceModel.Description; namespace CustomServiceLibrary { class Server { private readonly ushort port; private readonly Service1 Service1; private NetTcpBinding netTcpBinding; private ServiceHost serviceHost; public Server(ushort port) { this.port = port; Service1 = new Service1(); } public void Start() { try { CreateServiceHost(); CreateBinding(); AddEndpoint(); serviceHost.Open(); Console.WriteLine("Service has been opended"); ConfigureHTTP(); } catch (Exception ex) { Console.WriteLine($"ERROR := {ex.Message}"); Console.WriteLine($"StackTrace := {ex.StackTrace}"); } } private void AddEndpoint() { serviceHost.AddServiceEndpoint(typeof(IService1), netTcpBinding, string.Format("{1}://0.0.0.0:{0}/", port, "net.tcp")); } private void ConfigureHTTP() { var serviceBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (serviceBehavior == null) return; serviceBehavior.HttpGetEnabled = false; serviceBehavior.HttpsGetEnabled = false; } private void CreateBinding() { netTcpBinding = new NetTcpBinding { MaxReceivedMessageSize = int.MaxValue, MaxBufferPoolSize = int.MaxValue, Security = new NetTcpSecurity { Mode = SecurityMode.None, Transport = new TcpTransportSecurity() } }; } private void CreateServiceHost() { serviceHost = new ServiceHost(Service1); } } } 

摘要

除了David添加到项目中的更改之外,我所做的其他所有操作都与使用App.conf配置的上一次部署所做的相同。 我只是按照Microsoft文章“如何:使用TCP在Windows服务中托管WCF”中的说明进行操作, 网址https://msdn.microsoft.com/en-us/library/ff649818.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM