Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting WebAPI using OWIN in a windows service

I've self-hosted Web API using OWIN (inside a windows service). From what I understand, this is enough to make HTTP requests come to the windows service. I'm able to hit the WebAPI URL (http://localhost/users) locally (from the same machine), but not from other machines. I'm using port 80, IIS is stopped. Other websites (hosted in IIS, on port 80) work fine when IIS is running.

//In the windows service:

public partial class Service1 : ServiceBase {     ...     ...      protected override void OnStart(string[] args)     {         Console.WriteLine("Starting service...");         string baseAddress = "http://localhost:80/";         WebApp.Start<Startup>(baseAddress);  //This is OWIN stuff.     }     ...     ... }  public class Startup {     // This code configures Web API. The Startup class is specified as a type     // parameter in the WebApp.Start method.     public void Configuration(IAppBuilder appBuilder)     {         // Configure Web API for self-host.         var config = new HttpConfiguration();         WebApiConfig.Register(config);         appBuilder.UseWebApi(config);     } } 

Do I need to do something more to get this working from other machines? (I've a feeling that the incoming http requests are not being forwarded to the windows service, but only to IIS. When you hit locally, probably it does not go through the OS module that listens for http requests. Just a guess.)

like image 567
Narayana Avatar asked Feb 07 '14 17:02

Narayana


People also ask

Can Web API be hosted in Windows Service?

It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by . NET framework. You need to do following steps in order to self-host a web API. Let's see how to host a simple Web API in console application.

How do I host a web service in Windows service?

Install and run the serviceOpen Developer Command Prompt for Visual Studio and navigate to the project directory. Type installutil bin\service.exe at the command prompt to install the Windows service. Type services. msc at the command prompt to access the Service Control Manager (SCM).

What is Microsoft OWIN used for in Web API?

OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.


1 Answers

Your machine's firewall could be blocking the incoming requests. You could do:

You could Run wf.msc command to open up Windows Firewall with Advanced Security and add a new Inbound Rule for TCP port 80.

(You should notice couple of inbound rules starting with World Wide Web Services.... These are for IIS. I am not sure if enabling these rules would be enough to even allow your windows service to receive the requests...you can try and see if this works otherwise as suggested before, you can create a new inbound rule..)

Update:
Based on your comment, it could be that because of your Url registrations you are unable to hit the service. Following are some examples of registering multiple urls with HttpListener.

StartOptions options = new StartOptions(); options.Urls.Add("http://localhost:9095"); options.Urls.Add("http://127.0.0.1:9095"); options.Urls.Add(string.Format("http://{0}:9095", Environment.MachineName));  using (WebApp.Start<Program>(options)) { 

You can read more about the url registration in the following links:
http://technet.microsoft.com/en-us/library/bb630429.aspx
http://technet.microsoft.com/en-us/library/bb677364.aspx

like image 91
Kiran Avatar answered Sep 27 '22 18:09

Kiran