【问题标题】:This service on local computer started and then stopped. some services stop automatically if then are not in use by other servces or programs本地计算机上的此服务启动然后停止。如果某些服务没有被其他服务或程序使用,则某些服务会自动停止
【发布时间】:2013-12-27 13:14:26
【问题描述】:

在构建安装 Windows 服务后,当我尝试启动 Windows 服务时,出现第一个错误“Windows 无法在本地计算机上启动服务错误 5 访问被拒绝”。我按照以下解决方案解决了第一个错误:Cannot Start Windows Service in NetworkService account 。在此之后,第一个错误的通知消失了,但出现了另一个错误通知“本地计算机上的此服务启动然后停止。如果没有使用,某些服务会自动停止通过其他服务或程序”。我该如何解决这个问题?

注意: 我访问了许多发布的答案,但他们没有解决问题。

Windows service on Local Computer started and then stopped error

Windows 事件查看器通知:

Service cannot be started. System.InvalidOperationException: Service 'CustomerServiceLibrary.CustomersService'  has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
   at System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreApplicationEndpoints(ServiceDescription description)
   at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
   at System.ServiceModel.ServiceHostBase.InitializeRuntime()
   at System.ServiceModel.ServiceHostBase.OnBeginOpen()
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open()
   at WindowsServiceHost.WCFService.OnStart(String[] a...

The Service on local computer started and then stopped ,Some services stop automatically if there are not in use by other services or programs

已编辑:

namespace WindowsServiceHost
{
    public partial class WCFService : ServiceBase
    {
        public WCFService()
        {
            InitializeComponent();
        }

        private ServiceHost host = null;
        protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();
            host = new ServiceHost(typeof(CustomersService));
            host.Open();
        }

        protected override void OnStop()
        {
            if (host != null)
            {
                host.Close();
            }
            host = null;
        }
    }
}

app .config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

【问题讨论】:

  • 您可以发布您的服务代码吗?您的代码中的某处可能会引发异常。你试过调试吗?
  • 我正在关注“AppDev WCF_Using_C#_2008”的视频教程。他们只是在安装和启动之前构建它..
  • 跟踪错误日志非常明显:没有为服务主机定义端点。您可以发布app.config 文件(或者是否在代码中生成端点?)

标签: c# .net wcf windows-services


【解决方案1】:

就像PoweredByOrange 已经说过的那样,在您的app.config 中几乎没有配置,也没有定义端点。您的app.config 中应该有一个system.servicemodel 元素,它定义了您的服务,包括合同、端点、绑定等

在这里你可以看到Simple configuration on MSDN

【讨论】:

    【解决方案2】:

    您实际上可以检查事件处理程序。它会提示您在配置文件中所犯的错误。配置文件中的一些语法错误将是这种消息框的原因。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,在我的情况下是当我尝试在 OnStart 方法中打开主机时。

      问题在于服务的配置。调试 Windows 服务有多难,我使用 try/catch 块,在 catch 中,我将消息作为日志写入文本文件。您可以了解更多关于真正错误的详细信息。

      另一种选择是创建一个控制台应用程序来托管服务并在此控制台应用程序中调试,在控制台应用程序中调试服务更容易。

      【讨论】:

        【解决方案4】:

        【讨论】:

        • OP 在 Windows 服务 中托管服务 - 他们没有(也无法使用)Web.config。
        猜你喜欢
        • 2013-09-11
        • 1970-01-01
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多