TCP端口未在WCF Windows服务上侦听

时间:2018-03-01 15:31:21

标签: c# .net visual-studio wcf port

我按照说明创建了一个Windows服务来侦听此处显示的TCP端口:https://msdn.microsoft.com/en-us/library/ff649818.aspx

但是,在正确测试服务安装时,在服务应用程序中运行并且当前正在使用管理员的权限,但是没有进程侦听指定的端口。

Windows服务代码:

using System.ServiceModel;
using WcfServiceLibrary2010;

namespace WindowsService1
{
    public partial class Service1: ServiceBase
    {
        internal static ServiceHost myServiceHost = null; 

        public Service1()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            if (myServiceHost != null)
            {
                myServiceHost.Close();
            }
            myServiceHost = new ServiceHost(typeof(Service1));
            myServiceHost.Open();
        }
        protected override void OnStop()
        {
            if (myServiceHost != null)
            {
                myServiceHost.Close();
                myServiceHost = null;
            }
        }
    }
}

为了尝试识别发生了什么,我创建了一个控制台应用程序项目,引用与以前相同的自定义WCF库( WcfServiceLibrary2010 ),运行与Windows服务项目相同的代码:

using System.ServiceModel;
using WcfServiceLibrary2010;

namespace ConsoleHost
{
    class Program
    {
        static void Main(string[] args)
        {
            var sh = new ServiceHost(typeof(Service1));
            sh.Open();

            Console.WriteLine("sh has {0} ChannelDispatchers", sh.ChannelDispatchers.Count);

            foreach (var disp in sh.ChannelDispatchers)
            {
                Console.WriteLine(" - Binding: {0}", disp);
            }

            Console.WriteLine("Listening - press enter to exit...");
            Console.ReadLine();

            sh.Close();
        }
    }

在ConsoleHost和Windows服务之间创建新ServiceHost并打开它的过程是相同的,似乎ConsoleHost成功运行,监听指定的端口。

将Window Service项目合并到我的解决方案中会导致程序不在端口上侦听时可能会有什么不同?即使使用的代码是相同的。

提前致谢

编辑:app.config文件...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary2010.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WcfServiceLibrary2010.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8989/Service1" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

0 个答案:

没有答案
相关问题