Windows服务已停止,没有任何错误

时间:2013-09-19 08:33:45

标签: c# wcf windows-services

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

我检查了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);
    }
}

2 个答案:

答案 0 :(得分:1)

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

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


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

答案 1 :(得分:0)

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