Question

I need to do some background tasks for a web application i'm building. I like the look of using Topshelf to run a service on the server. I was wondering the best way of initialising the service. I was wondering if it would be a bad idea to start the service from the MVC application start up

For example:

  protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        InitService();
    }

    private void InitService()
    {
        Task.Factory.StartNew(() => HostFactory.Run(x =>
                                                        {
                                                            x.Service<TestService>(s =>
                                                                                       {
                                                                                           s.SetServiceName("TestService");
                                                                                           s.ConstructUsing(
                                                                                               name => new TestService());
                                                                                           s.WhenStarted(ts => ts.Start());                                                                                               s.WhenStopped(ts => ts.Stop());
                                                                                       });
                                                            x.RunAsLocalSystem();
                                                            x.SetDescription("Sample test service");
                                                            x.SetDisplayName("Test display name");
                                                            x.SetServiceName("Test service name");
                                                        }));
    }

obviously the job of starting the application is run in its own task. Are there better ways of doing this? Could you see a problem with this?

My idea is that once the service is started, it will handle its self from then on.

My only thought was: if you restart your web application multiple times, but I thought I could just put some logic in to check that if the service is already running then don't start another one.

Was it helpful?

Solution

This works, but this makes the service live in the scope of the web app (managed inside IIS) - you aren't really installing it as a windows service. This means if the app pool drops, so does your service.

If you wanted to install the service as a windows service, you would need to check for it's existence, then run "servicename install start" with it inside a console app. It really depends on what you want. Having it part of the app might give you something but if you need to run the app on multiple machines it might be awkward.

Personally, I would always make it run as a separate service and not in the scope of the web app inside IIS.

OTHER TIPS

You can do this with Quartz without any concern too

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top