0

我创建了一个 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;
}        

应用程序配置:

<?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);
    }
}
4

2 回答 2

1

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

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


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

于 2013-09-19T08:39:14.617 回答
0

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

于 2013-09-19T10:15:15.683 回答