【问题标题】:Windows service stopped without any errorsWindows 服务停止,没有任何错误
【发布时间】:2013-09-19 08:33:45
【问题描述】:

我创建了一个 Windows 服务来托管一些 WCF 服务,
但是当我启动它时,它会停止并显示一条消息:

我检查了 windows 日志查看器,没有任何错误

我之前在控制台应用程序上测试了所有内容,并且可以正常工作。

我的代码是:

ServiceHost host1;
ServiceHost host2;
ServiceHost host3;

public ServicesHost()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    if (host1 != null)            
        host1.Close();
    if (host2 != null)
        host2.Close();
    if (host3 != null)
        host3.Close();            
    host1 = new ServiceHost(typeof(Service1));
    host1.Open();    
    host2 = new ServiceHost(typeof(Service2));
    host2.Open();    
    host3 = new ServiceHost(typeof(Service3));
    host3.Open();                       
}

protected override void OnStop()
{
    host1.Close();
    host1 = null;
    host2.Close();
    host2 = null;
    host3.Close();
    host3 = null;
}        

app.config:

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

  <service behaviorConfiguration="MyServiceBehavior"
           name="Service2">
    <endpoint address=""
              binding="basicHttpBinding"
              contract="IService2">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding"
              contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Service2" />
      </baseAddresses>
    </host>
  </service>

  <service behaviorConfiguration="MyServiceBehavior"
           name="Service3">
    <endpoint address=""
              binding="basicHttpBinding"
              contract="IService3">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding"
              contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8733/Service3" />
      </baseAddresses>
    </host>
  </service>

</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True" />
    </behavior>
  </serviceBehaviors>
</behaviors>

编辑: 我有安装程序:

[RunInstaller(true)]
public partial class ServiceInstaller : System.Configuration.Install.Installer
{
    private System.ServiceProcess.ServiceProcessInstaller process;
    private System.ServiceProcess.ServiceInstaller service;

    public ServiceInstaller()
    {            
        process = new System.ServiceProcess.ServiceProcessInstaller();
        process.Account = System.ServiceProcess.ServiceAccount.NetworkService;
        service = new System.ServiceProcess.ServiceInstaller();
        service.ServiceName = "WCFHostService";
        service.DisplayName = "WCFHostService";
        service.Description = "WCF Service Hosted";
        service.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
        Installers.Add(process);
        Installers.Add(service);
    }
}

【问题讨论】:

  • 这取决于您在调用 host.Open() 时所做的事情。此外,我相信该服务不会记录任何内容,除非您明确声明必须这样做。
  • 哪个用户帐户用于运行 NT Serice?作为控制台运行时,您在启动控制台的凭据下运行(通常是开发人员以高安全权限登录)。当作为 NTService 运行时,它是一个分配的 windows 帐户。这可能与帐户拥有的权限有关吗?我也似乎在配置部分缺少“Service1”。您使用的是哪个版本的 .Net 框架? (NTServices 配置加载已更改)
  • .net 4,不知道是用哪个账号来运行服务的,现在试了一个简单的代码,同样的情况,只是简单的文字写不行。跨度>

标签: c# wcf windows-services


【解决方案1】:

当控制台/桌面应用程序运行但不是作为服务时,这主要是用户权限问题。这同样适用于使用 COM/DCOM 或使用文件,因为服务的当前路径是 windows\system32。

尝试用 try/catch 包装 OnStart 并将异常写入 EventLog -> http://support.microsoft.com/kb/307024


您是否为该服务创建了任何安装程序? 如何:将安装程序添加到您的服务应用程序 http://msdn.microsoft.com/en-us/library/ddhy0byf.aspx

【讨论】:

  • 我这样做了,我也尝试了一个文本文件写入错误,它们都没有报告任何错误。
  • 您是否为该服务创建了任何安装程序?我认为您可能会错过那个,因为当您将其包装在 try/catch 中时,它不应该停止。有关安装程序的更多信息:如何:将安装程序添加到您的服务应用程序 msdn.microsoft.com/en-us/library/ddhy0byf.aspx
  • 另外,现在我尝试了一个简单的代码来编写一个文本文件,它没有做任何事情。
  • 你是否使用了类似 string filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "error.log") ; 因为默认它会尝试写入 system32 目录。 (这是不允许的)
  • 不,我认为这会发生,所以我指定了一个绝对路径。
【解决方案2】:

好吧,你根本没有记录。一个窗口服务应该有一个良好的日志系统以在生产/维护阶段生存。添加一个登录系统,在开始时放一个try catch,捕捉并记录异常在一个文件中,这将帮助您解决您现在面临的问题,但在您的项目生命周期中的每一天都有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多