1

我想我已经检查了所有可能性,但是当我尝试启动我的服务时,我仍然不断收到这个错误(用 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 服务正常工作。据我所知,这不起作用的原因为零。

4

2 回答 2

1

你想让我做什么?

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

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

你真的想做什么??

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

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

还有很多很多的样本。这两个示例都只显示了一个普通的 Windows 服务,没有 WCF,ServiceHost看不到任何地方。

于 2013-07-08T10:33:40.247 回答
0

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

       Uri baseAddress = new Uri("http://localhost:8095/Service");
        serviceHost = new ServiceHost( typeof(YourService), baseAddress );
        serviceHost.AddServiceEndpoint( typeof(IYourService), new BasicHttpBinding(), baseAddress );
        serviceHost.Open();
于 2013-07-08T06:20:51.110 回答