本地计算机上的此服务已启动然后停止。如果某些服务未被其他服务或程序使用,则会自动停止

时间:2013-12-10 06:09:32

标签: c# .net wcf windows-services

在构建安装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>

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

就像PoweredByOrange alreadys所说,在你的app.config中几乎没有配置,也没有定义端点。您的system.servicemodel中应该有一个app.config元素来定义您的服务,包括哪个合同,端点,绑定等

在这里,您可以看到Simple configuration on MSDN

答案 2 :(得分:0)

您实际上可以检查事件处理程序。它会让你知道你在配置文件中所犯的错误。配置文件中的一些语法错误将是此类消息框的原因。

答案 3 :(得分:0)

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

问题在于配置服务。如何调试Windows服务很困难,我使用try / catch块并在catch中,我将消息作为日志写在文本文件上。您可以获得有关真实错误的更多详细信息。

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