【问题标题】:Windows service hosting a WCF service closing immediately托管 WCF 服务的 Windows 服务立即关闭
【发布时间】:2011-06-30 05:20:02
【问题描述】:

我尝试使用 Windows 服务项目托管 WCF 库服务,我安装了该服务,但是,当我在 services.msc 中启动该服务时,该服务立即启动并关闭。按照显示的消息:

Local 上的 Servicel 服务 计算机启动然后停止。 某些服务会自动停止,如果 它们未被其他服务使用 或程序。

wcf和windows服务项目的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="WorkMateWCF.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/WorkMate1" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

整个项目/解决方案可在此处下载:https://skydrive.live.com/?cid=d358d316fa2c3a37&sc=documents&uc=1&id=D358D316FA2C3A37%21135#

请您指导我如何进一步进行。谢谢。

附加信息: 以下是windows服务项目中service1.cs文件的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using WorkMateWCF;

namespace WorkMateWinService
{
    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;
            }
        }
    }
}

【问题讨论】:

  • 您需要在帖子中提供有关此问题的更多信息,而不是依赖可下载的项目。这个问题及其答案应该对所有人都有益。
  • 你能告诉我还需要什么信息吗,因为我想我提到了这个问题,我是编码新手,请告诉我还需要什么信息。谢谢。
  • 一个好的开始是在服务的 Start 方法中显示代码。欢迎提供任何其他相关信息。
  • 谢谢 Roy Dictus,我已经添加了。

标签: wcf windows-services


【解决方案1】:

我觉得非常令人困惑(可能还有 .NET 运行时)是您的 Windows 服务被称为 Service1,而您的 WCF 服务被称为 Service1(没有命名空间或任何东西)。

那么这里会用到Service1这两个类类型中的哪一个???

MyServiceHost = new ServiceHost(typeof(Service1));

我不确定 - 恐怕是 错误 类(Windows NT 服务类)。

你应该给你的东西起更有意义的名字,并把它们分开(也按名字)!

【讨论】:

  • 感谢 marc_s,我希望 wcf 在这种情况下运行。您能否告诉我如何进行更改。非常感谢让我理解错误。我尝试从msdn.microsoft.com/en-us/library/ff649818.aspx 做,但我不太擅长 c#
  • @surpavan:只需将 WCF 服务类重命名为 MyWCFService,然后使用 MyServiceHost = new ServiceHost(typeof(MyWCFService));。这样,您就有了两个不同的名称 - Service1 是您的主机,NT 服务,而 MyWCFService 是 WCF 服务(顾名思义)
  • 我很抱歉说我不能做那件事,这让我很困惑,请你做出改变并给我文件,以便我可以比较和了解它在哪里或是什么。感谢到目前为止所做的一切。
  • @surpavan:你看到源文件中的注释了吗? NOTE: You can use the "Rename" command on the context menu to change the class name "Service1" in both code and config file together. - 你能做到吗??
  • sorry marc_s,还是同样的问题,能否请您进行必要的修改并附上文件。我所做的是在服务项目中,我重构>将“Service1:ServiceBase”类名重命名为“TService1:ServiceBase”,但同样的错误仍然存​​在。感谢您的宝贵时间。
【解决方案2】:

遇到了这个问题,当我查看我的事件日志时发现:

"Service cannot be started. System.InvalidOperationException: The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address.  Either supply an http base address or set HttpGetUrl to an absolute address.
   at System.ServiceModel.Description.ServiceMetadataBehavior.EnsureGetDispatcher(ServiceHostBase host, ServiceMetadataExtension mex, Uri url, String scheme)
   at System.ServiceModel.Description.ServiceMetadataBehavior.CreateHttpGetEndpoints(ServiceDescription description, ServiceHostBase host, ServiceMetadataExtension mex)
   at System.ServiceModel.Description.ServiceMetadataBehavior.ApplyBehavior(ServiceDescription description, ServiceHostBase host)
   at System.ServiceModel.Description.ServiceMetadataBehavior.System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
   at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescript..."

然后经过彻底审查,问题是我只为一个将 HTTPSGETENABLED 设置为 false,实际上有两个,在对另一个进行更改后,该应用程序开始像魅力一样工作。

我很特别

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 2013-08-06
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多