Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6721419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:19:42+00:00 2026-05-26T09:19:42+00:00

I need to do some background tasks for a web application i’m building. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:19:42+00:00Added an answer on May 26, 2026 at 9:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to run some background tasks whenever an application is activated / or
Our Swing application performs some long-running tasks in a background thread using the excellent
I need to write some scripts to carry out some tasks on my server
I have tasks completed by AsyncTask in background. At some point I need to
I've a console Java application, which does some tasks in the background. Java doesn't
How to change HTML background with JavaScript Function? I need some simple function to
I'm writing some code where the UI thread need to communicate with the background
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for
I usually use Dispatcher.Invoke when i need to access some UI elements from background
I want to create a background Thread handling some tasks (Runnable) and executing them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.