【问题标题】:Zero application endpoint error when I try to start a service尝试启动服务时应用程序端点错误为零
【发布时间】:2013-07-05 09:28:50
【问题描述】:

我想我已经检查了所有可能性,但是当我尝试启动我的服务时,我仍然不断收到这个错误(写在 eventvwr 中):

服务无法启动。 System.InvalidOperationException:服务 'NexolNotifierWinService.NexolNotifier' 应用为零 (非基础设施)端点。这可能是因为没有配置 为您的应用程序找到了文件,或者因为没有服务元素 可以在配置文件中找到匹配的服务名称,或者 因为在服务元素中没有定义端点。

使用 installutil 可以顺利安装服务。

我真的不确定为什么会出现此错误。这只是一个简单的 Windows 服务项目,所以也没有 app.config 可以搞砸。

这是我的代码:

程序.cs

static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new NexolNotifierService() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}

NexolNotifierService.cs

public partial class NexolNotifierService : ServiceBase
{
    private ServiceHost host;
    public NexolNotifierService()
    {
        InitializeComponent();
        this.ServiceName = "NexolNotifierService";
    }

    protected override void OnStart(string[] args)
    {
        Type serviceType = typeof(NexolNotifier);
        host = new ServiceHost(serviceType);
        host.Open();
    }

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

ProjectInstaller.Designer.cs(用于安装服务)

private void InitializeComponent()
    {
        this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
        this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
        // 
        // serviceProcessInstaller1
        // 
        this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; 
        this.serviceProcessInstaller1.Password = null;
        this.serviceProcessInstaller1.Username = null;
        // 
        // serviceInstaller1
        // 
        this.serviceInstaller1.ServiceName = "NexolNotifierService";
        this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
        // 
        // ProjectInstaller
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.serviceProcessInstaller1,
        this.serviceInstaller1});

    }

和我的实际服务:

NexolNotifier.cs

public class NexolNotifier
{
    public NexolNotifier()
    {
        ....
    }

服务是从添加新项目->Visual Studio 2008 中的 Windows 服务添加的。

我只是想让一个非常简单的 Windows 服务正常工作。据我所知,这不起作用的原因为零。

【问题讨论】:

  • 看这里stackoverflow.com/questions/2328840/…,你需要配置文件
  • @wudzik 我的问题是,为什么?它不是 WCF 项目,它是由 Windows 服务向导生成的本地 Windows 服务,它没有为我提供配置文件。
  • 您是否在管理员模式下运行服务?否则它无法创建端点。检查您的事件日志...
  • @Aron 是的,我在管理员模式下运行服务。
  • @marc_s 所以,让我直说吧。我的意图是创建一个不会做任何网络的本地 Windows 服务,但我不得不使用 WCF 来做到这一点?那么我必须使用空的 http 绑定设置添加 app.config 吗?严重地?这是 MSFT 的真正设计方式还是我的假设太天真了?

标签: c# .net visual-studio-2008 windows-services


【解决方案1】:

从这样的代码中添加服务端点

       Uri baseAddress = new Uri("http://localhost:8095/Service");
        serviceHost = new ServiceHost( typeof(YourService), baseAddress );
        serviceHost.AddServiceEndpoint( typeof(IYourService), new BasicHttpBinding(), baseAddress );
        serviceHost.Open();

【讨论】:

  • 再次,同样的错误。 MSDN 教程没有提到任何关于为不会进行任何联网的本地 Windows 服务添加端点的内容。我很好奇为什么添加端点会解决问题。
  • 我想需要一些端点,但不知道如何制作。也许您应该尝试使用topshelf-project.com 作为Windows 服务主机?使用起来很简单:)
【解决方案2】:

你想做什么?

如果你只想要一个普通的 Windows 服务 - 没有通信,什么都没有 - 那么 你不需要 ServiceHost 你只需要从 @987654324 派生@ .NET 框架中的类并实现/覆盖一些方法 - 仅此而已。从数据库中读取值,对它们进行处理,发送电子邮件 - 无论如何 - 你将永远不会为此需要 ServiceHost

如果您使用ServiceHost,那么您使用的是WCF 基础架构,这意味着您正在编写一个自托管的Web 服务。

那么你真的想做什么?

您的 Windows 服务应该执行什么任务/工作? ServiceHost 与普通的 Windows 服务无关 ServiceHost == WCF - 总是。对于普通的 Windows 服务,您不需要ServiceHost

对于只是普通的 Windows 服务(无 WCF),请参阅例如

还有很多很多个样本。两个示例都只显示了一个普通的 Windows 服务,没有 WCF,也没有 ServiceHost

【讨论】:

  • 是的,这正是我想要的答案。出于某种原因,谷歌搜索 C# Windows 服务教程只显示了涉及 ServiceHost 的结果(包括 MSDN)。非常感谢。
猜你喜欢
  • 2013-05-12
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
相关资源
最近更新 更多