Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get started with TopShelf

Tags:

c#

topshelf

I recently discovered TopShelf. From everything I have read, it looks very cool. The only problem is that I have not been able to use it. I have to have missed something. Below is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;

namespace TestTopShelf {
public class FooBar {
    public FooBar() {

    }

    public void Start() { }
    public void Stop() { }
}

public class Program {
    public static void Main() {
        HostFactory.Run(x => {

            x.Service<FooBar>( s => { 

            });
        });
    }
}
}

You can see that it is a bit incomplete. When I am trying to set properties of the 's' object for ConstructUsing, WhenStarted, and WhenStopped Visual Studio is not inferring the correct type. I am new to lambda expressions and even newer to TopShelf, so I am not sure what I am doing.

I am using this page in the TopShelf documentation to get me started. It looks pretty straight forward, so I am not sure what I have missed.


updated code


using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;

namespace KeithLink.Svc.Windows.OrderService2 {
class FooBar {
    public FooBar() { }

    public void Start() { }
    public void Stop() { }
}

class Program {
    static void Main(string[] args) {
        HostFactory.Run(x => {

            x.Service<FooBar>(s => {
                s.ConstructUsing(name => new OrderService());
                s.WhenStarted(os => os.Start());
                s.WhenStopped(os => os.Stop());
            });

            x.RunAsLocalSystem();

            x.SetDescription("some service description");
            x.SetServiceName("ServiceName");
            x.SetDisplayName("Service Display Name");
        });
    }
}
}
like image 328
fizch Avatar asked Sep 15 '14 16:09

fizch


People also ask

What is a topshelf service?

Topshelf is a Windows service framework for the . NET platform. Topshelf makes it easy to create a Windows service, test the service, debug the service, and ultimately install it into the Windows Service Control Manager (SCM).

What is topshelf C#?

Topshelf is a framework for hosting services written using the . NET framework. The creation of services is simplified, allowing developers to create a simple console application that can be installed as a service using Topshelf.


1 Answers

Although VisualStudio's intellisense doesn't infer the correct type, it should still compile. I don't know what topshelf is doing but I remember having those issues the last time I tried using it.

like image 66
tobsen Avatar answered Oct 01 '22 03:10

tobsen