简体   繁体   English

WCF端口没有监听

[英]WCF port not listening

Update 更新

The service is running and has a pid in Task Manager but when I run netstat -ano | find pid# 该服务正在运行,并在任务管理器中有一个pid,但是当我运行netstat -ano | find pid# netstat -ano | find pid# nothing is returned - the port is not listening. netstat -ano | find pid#没有返回 - 端口没有监听。

Also if I run netstat -ap tcp the port is not listed. 此外,如果我运行netstat -ap tcp ,则不会列出端口。

I really don't know how to fix this. 我真的不知道如何解决这个问题。 In summary so you don't have to read my previous post: 总之,所以你不必阅读我以前的帖子:

  • My WCF Service works perfectly under localhost (ie service and client on same server) 我的WCF服务在localhost下完美运行(即同一服务器上的服务和客户端)
  • When I publish the Service to a production server, the service installs fine but the 8523 port does not listen 当我将服务发布到生产服务器时,该服务安装正常,但8523端口不会监听
  • There are several similar posts that I have reviewed but none work for me 有几个类似的帖子我已经审查过,但没有一个对我有用
  • This is my first WCF project that I have attempted so I have exhausted my knowledge and I can't find any more examples on the Internet to try 这是我尝试的第一个WCF项目,所以我已经耗尽了我的知识,我在互联网上找不到更多的例子来尝试
  • At this point I will be very grateful for any pointers 在这一点上,我将非常感谢任何指针

environment 环境

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

Introduction 介绍

I have researched this issue for a few days now and although I have found a number of similar questions on Stack Overflow, none of those questions solve my problem; 我已经研究了这个问题几天了,尽管我在Stack Overflow上发现了许多类似的问题,但这些问题都没有解决我的问题; therefore I don't think this is a duplicate and it will be specifically helpful if someone is trying to follow the referenced Microsoft tutorial as I have done 因此,我不认为这是重复的,如果有人试图像我一样按照引用的Microsoft教程,它将特别有用

Problem details 问题详情

I have followed this Microsoft article "How to: Host WCF in a Windows Service Using TCP" at https://msdn.microsoft.com/en-us/library/ff649818.aspx . 我在https://msdn.microsoft.com/en-us/library/ff649818.aspx上关注了这篇Microsoft文章“如何:使用TCP在Windows服务中托管WCF”。

After creating the solution, it runs perfectly on my development machine within the Visual Studio localhost environment. 创建解决方案后,它在Visual Studio localhost环境中的开发计算机上运行完美。 The tutorial suggests that you create a Test Client to access the WCF solution and that Test client works successfully 本教程建议您创建一个测试客户端来访问WCF解决方案,并且测试客户端可以成功运行

When I publish the WCF Service and the Windows Service in the production machine (Windows Server 2008) it again installs without error. 当我在生产计算机(Windows Server 2008)中发布WCF服务和Windows服务时,它会再次安装而不会出错。 Here are the steps that I follow: 以下是我遵循的步骤:

IIS Setup IIS安装程序

  • add net.tcp to the enabled protocols 将net.tcp添加到已启用的协议

  • activate 'Windows Communication Foundation Non-HTTP Activation' 激活'Windows Communication Foundation非HTTP激活'

  • Changed bindings for net.tcp to 8532 将net.tcp的绑定更改为8532

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

Open 8532 port in firewall 在防火墙中打开8532端口

Browse to the bin directory of the project where WindowsService1.exe is located and run the exe as administrator 浏览到WindowsService1.exe所在项目的bin目录,并以管理员身份运行exe

Open command Prompt as administrator and run Installutil WindowsService1.exe 打开命令以管理员身份提示并运行Installutil WindowsService1.exe

Differences between Localhost (development environment in Visual Studio) and Production environment Localhost(Visual Studio中的开发环境)与生产环境之间的差异

The only changes between the localhost WCF system and production system is I changed the baseAddress in app.conf from localhost WCF系统和生产系统之间的唯一变化是我更改了app.conf中的baseAddress

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

to

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

Here is the deployed app.conf 这是部署的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> 

Adding Service Reference 添加服务参考

When I try to add the service reference in my application that will use the WCF service. 当我尝试在我的应用程序中添加将使用WCF服务的服务引用时。 I get this error: 我收到此错误:

There was an error downloading metadata from the address. 从地址下载元数据时出错。 Please verify that you have entered a valid address 请确认您输入的是有效地址

When I click on "Details" I see this: 当我点击“详细信息”时,我看到了这个:

The URI prefix is not recognized. 无法识别URI前缀。 Metadata contains a reference that cannot be resolved: 'net.tcp://10.2.1.1:8523/CustomWCF'. 元数据包含无法解析的引用:'net.tcp://10.2.1.1:8523 / CustomWCF'。 Could not connect to net.tcp://10.2.1.1:8523/CustomWCF. 无法连接到net.tcp://10.2.1.1:8523 / CustomWCF。 The connection attempt lasted for a time span of 00:00:01.0312368. 连接尝试持续时间跨度为00:00:01.0312368。 TCP error code 10061: No connection could be made because the target machine actively refused it 10.2.1.1:8523. TCP错误代码10061:无法建立连接,因为目标计算机主动拒绝它10.2.1.1:8523。 No connection could be made because the target machine actively refused it 10.2.1.1:8523 If the service is defined in the current solution, try building the solution and adding the service reference again. 无法建立连接,因为目标计算机主动拒绝它10.2.1.1:8523如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

What I have tried 我试过了什么

A lot of the other WCF questions asked on forums seem to be in regards to getting the service working in localhost which does not apply to me because the solution I've created works perfectly in the development environment. 在论坛上提出的许多其他WCF问题似乎都是关于让服务在localhost中工作,这对我不适用,因为我创建的解决方案在开发环境中完美运行。

Some of the answers suggest enabling tcp port sharing in the project but when I try and add this code in my solution I get an error saying "not allowed". 一些答案建议在项目中启用tcp端口共享,但是当我尝试在我的解决方案中添加此代码时,我收到错误“不允许”。

Other questions suggest that the url must be registered on the production server but that has no effect. 其他问题表明url必须在生产服务器上注册,但没有效果。

After trying all the forum suggestions that I can find and trying to correct everything that I think is wrong, I've still have no luck. 在尝试了我能找到的所有论坛建议并尝试纠正我认为错误的一切之后,我仍然没有运气。

Any suggestions or pointers will be greatly appreciated 任何建议或指示将不胜感激

I have not gotten WCF to work well with the configuration file in the past. 我没有让WCF在过去使用配置文件。 I have always created the bindings in code. 我总是在代码中创建绑定。

This gist does work. 这个要点确实奏效了。

The important parts are NetTcpBinding 重要的部分是NetTcpBinding

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

Also I would not specify an IP but rather let WCF bind it to every IP Address on the machine. 此外,我不会指定IP,而是让WCF将其绑定到计算机上的每个IP地址。

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

This does not prevent you putting in a specific IP, I just typically do not. 这并不妨碍您输入特定的IP,我通常不这样做。

If you want to run it in IIS you have to do a little bit more work. 如果你想在IIS中运行它,你必须做更多的工作。 IIS doesn't really want you to host a WCF server that is not bound in by http. IIS实际上并不希望您托管未受http约束的WCF服务器。

You have to do a little extra work after creating the ServiceHost 创建ServiceHost后,您必须做一些额外的工作

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

if (serviceBehavior == null) return;

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

I've added this post to detail the results of adding David Basarab suggested changes to my WCF Service Library project. 我已添加此帖子以详细说明将David Basarab建议更改添加到我的WCF服务库项目的结果。 Again many thanks for David's post, but unfortunately I couldn't get it to work. 再次感谢大卫的帖子,但不幸的是我无法让它发挥作用。 In fact it didn't even run under localhost in Visual Studio. 实际上它甚至没有在Visual Studio中的localhost下运行。 David mentioned that it worked for him so I probably make a mistake somewhere. 大卫提到它对他有用,所以我可能在某个地方犯了错误。 Hopefully the details that I provide here will either generate some additional pointers to what I did wrong or help someone else that tries David's approach. 希望我在这里提供的细节能够产生一些额外的指针,指出我做错了什么或帮助其他尝试David的方法。 I tried to keep the Visual Studio default values when creating the Solution except for David's changes. 我在创建解决方案时尝试保留Visual Studio的默认值,但David的更改除外。

Service Reference Error 服务参考错误

Here's the error that I received when I tried to add the Service Reference in my Test Client on the same machine as both the Windows Service and the actual WCF Service Library: 这是我尝试在与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 Library code WCF库代码

IService1.cs unit 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 code 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 class code 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); } } } 

Summary 摘要

Other than David's changes that I added to the project, everything else that I did was the same as what I did for the previous deployment using App.conf configuration. 除了David添加到项目中的更改之外,我所做的其他所有操作都与使用App.conf配置的上一次部署所做的相同。 I just followed the directions in Microsoft article "How to: Host WCF in a Windows Service Using TCP" at https://msdn.microsoft.com/en-us/library/ff649818.aspx 我只是按照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