2

I have read several articles about how does TopShelf works. All of them are saying:

  • Create a Console application

  • Add the Topshelf NuGet package

  • Create a simple testController : ApiController to represent the service logic (I want to have my existing WEB API project to be hosting instead of this testController)

  • ...

But now I want to have my existing WEB API project to be hosting instead of this testController. How should I replace my project with this testController in this TopShelf console application?

Obviously I can't configure my WEB API project itself with TopShelf instead of using a Console Application because the WEB API has not an exe file like console app.

I just want to know how should I replace this test controller inside console app with my real API project?

  • Basically you have to create a standalone Web API project. You can then add the testcontroller in the newly created project then host it in IIS. – Souvik Ghosh Aug 28 '17 at 12:06
  • @SouvikGhosh I want to host it in Windows Services instead of IIS by Topshelf. –  Aug 28 '17 at 12:06
  • Have you checked this- https://codeopinion.com/self-host-asp-net-web-api-as-a-windows-service/ – Souvik Ghosh Aug 28 '17 at 12:13
  • @SouvikGhosh This is one of the several articles I've read. As it states in the article you should use Topshelf in a console app. Where should I integrate my WEB API project to this Topshelf and Console app? –  Aug 28 '17 at 12:16

1 Answers1

2
  1. Add a new console application to your solution

  2. Install NuGet Package Microsoft.Owin.SelfHost, Microsoft.AspNet.WebApi.OwinSelfHost and Topshelf to the your new project

  3. Add a Startup.cs (see here)

  4. Add TopshelfService.cs

    public class TopshelfService
    {
        private IDisposable moDisposable = null;
    
        public void Start()
        {
            this.moDisposable = WebApp.Start<Startup>("http://localhost:9989");
        }
    
        public void Stop()
        {
            this.moDisposable?.Dispose();
        }
    }
    
  5. Add code from Topshelf Section to your Main methode

  6. Add a reference to your existing WebApi Project

  7. Create a dummy instance from you controller in your Startup class. (This is necessary to load you WebApi Project before start Owin)

    public void Configuration(IAppBuilder app)
    {
        DemoController dummy = new DemoController();
    
        // Configure Web API for self-host. 
        var config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    
        app.UseWebApi(config);
    }
    
  8. Compile and run

  9. Install the service with "Project.exe" install

  10. Now you have a windows service "Self Host Web API Demo'

canon
  • 40,609
  • 10
  • 73
  • 97
PinBack
  • 2,499
  • 12
  • 16
  • 1
    Thanks a lot. This is exactly what I need. I'm wondering why none of the articles points to this and how deal with real world API with Topshelf. –  Aug 28 '17 at 14:20
  • Also the only missing part that I needed was step 6 and 7. Create a dummy instance from you controller in your Startup class. (This is necessary to load you WebApi Project before start Owin) this is not mentioned anywhere. Although I even did step 6 but without step 7 it doesn't works. –  Aug 28 '17 at 14:37
  • If you start with a console application you don't need a WebApi Project. You can simply add the controller classes to the project. Then you don't need step 7. – PinBack Aug 28 '17 at 14:38
  • But is it a good idea to replace my WEB API project with a console app? –  Aug 28 '17 at 14:41
  • If you want to install the application just as a selfhosting windows service ... why not – PinBack Aug 28 '17 at 14:44
  • But if I want to host in other ways then console app doesn't works right? –  Aug 28 '17 at 14:58
  • If you want to deploy the application to IIS then you should use a Web Application. But you can also make a separate library for your controllers and then use this Dll in a console or/and in a web application. – PinBack Aug 28 '17 at 15:17
  • When we can do every thing with console app why we ever need to the WEb API project? Is there any case that console app will not suitable and should use WEB API? –  Aug 28 '17 at 15:26
  • Look here http://www.c-sharpcorner.com/UploadFile/2b481f/how-to-host-Asp-Net-web-api-on-iis-server/ – PinBack Aug 28 '17 at 15:37