简体   繁体   English

Topshelf C#Windows服务-无法通过HostControl

[英]Topshelf C# Windows Service - Cannot pass HostControl

I am trying to pass a HostControl instance to the start method of a topshelf service but I am getting the following compiler errors: 我试图将HostControl实例传递给topshelf服务的start方法,但遇到以下编译器错误:

  • Not all code paths return a value in lambda expression of type 并非所有代码路径都在类型为lambda的表达式中返回值
  • 'HostSettings' does not contain a definition for 'ConstructUsing' “ HostSettings”不包含“ ConstructUsing”的定义
  • 'HostSettings' does not contain a definition for 'WhenStarted' and no extension method 'WhenStarted' accepting a first argument of type 'HostSettings' could be found (are you missing a using directive or an assembly reference?) “ HostSettings”不包含“ WhenStarted”的定义,并且找不到扩展方法“ WhenStarted”,该扩展方法接受“ HostSettings”类型的第一个参数 (您是否缺少using指令或程序集引用?)

I have implement the interface 'ServiceControl' for the EventBroker class. 我已经为EventBroker类实现了接口“ ServiceControl”。

using Topshelf;
namespace Sample
{
  class Program
  {
    static void Main(string[] args)
    {
        HostFactory.Run(x =>
        {
            x.Service<EventBroker>(s =>
            {
                s.ConstructUsing(name => new EventBroker());
                s.WhenStarted((tc, hostControl) => tc.Start(hostControl));
                s.WhenStopped(tc => tc.Stop());
            });
            x.RunAsLocalSystem();

Am I missing some an assembly reference or something else? 我是否缺少一些装配参考或其他参考? I am using TopShelf v3.3.154.0. 我正在使用TopShelf v3.3.154.0。 Without the 'hostControl' it is working fine. 没有“ hostControl”,它可以正常工作。

After some trouble shooting, I found that the 'ServiceControl' wasn't implemented correctly. 经过一些故障排除后,我发现“ ServiceControl”未正确实现。 The Stop/Start methods of the EventBroker didn't have a return type (bool). EventBroker的Stop / Start方法没有返回类型(布尔型)。

Now, with the following Stop/Start Methods it is working. 现在,通过以下“停止/启动方法”,它可以正常工作。

    bool Start(HostControl hostControl)
    {
        return true;
    }

    bool Stop(HostControl hostControl)
    {
        return true;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM