48

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.)

3
  • 3
    You might want to check out this example GitHub project: github.com/danesparza/OWIN-WebAPI-Service Feb 14, 2014 at 14:59
  • 2
    Just for future reference: like justmara comment below: you could simply start it at http://*:80/ so it will respond on every address available. – justmara May 27 '14 at 14:08 Nov 13, 2015 at 6:43
  • I wonder how to put this in production though, if possible. How do you convert the localhost:portNumber to a real host name then?
    – Weihui Guo
    Oct 8, 2018 at 12:57

6 Answers 6

59

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

7
  • Just turn off the firewall to verify that it is indeed the issue and if it solves your problem, then add the rule.
    – 0leg
    Feb 7, 2014 at 18:14
  • Tried both, doesn't work. http://localhost/users works, but http://127.0.0.1:80/users or http://machine-name/users don't work. If I use port 80, I get 404 not found, if I use another port, I get 400 bad request - invalid request hostname. Does that signify something?
    – Narayana
    Feb 7, 2014 at 18:28
  • I've only one machine at home. Will check this tomorrow and update.
    – Narayana
    Feb 9, 2014 at 21:01
  • 55
    you could simply start it at http://*:9095/ so it will respond on every address available.
    – justmara
    May 27, 2014 at 14:08
  • Nice answer, would be nice to see it include using 'netsh.exe' to configure the url/ports: msdn.microsoft.com/en-us/library/ms733768(v=vs.110).aspx (I think another answer mentioned netsh)
    – peteski
    Oct 22, 2015 at 9:36
20

There are 2 things that will prevent you from using anything different from "localhost" in Owin service:

  1. The app needs to run as admin in order to open a port with a different hostname as "localhost". You can fix this by either run the app with admin privileges or add an exception for a given port using: netsh http add urlacl url=http://*:9000/ user=<your user>
  2. The windows firewall may block traffic from other computers. In my case the firewall was not blocking local traffic (I could get to http://localhost:9000 or http://127.0.0.1:9000 or http://192.168.1.193:9000 - which was my local IP address from the same computer, but needed to add a port exception to the firewall to allow getting to this service from another computer)
1
  • thanks for the tip on running with admin privileges , that was my issue.
    – mini998
    Mar 9, 2021 at 10:53
6

I was facing a similar issue. Below solution worked for me.

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))
{
    ...
}
4

This is actually a simple oversight I also tripped on: If you only listen to requests on localhost, 127.0.0.1, etc -- no one will ever see it because only YOU (self) understands you as that target. On my machine I am localhost, if I req your IP on any port, that service would see get http://your_ip:80, not localhost. All you have to do is serve on "http://*:{0}", and that way you can onStart() pass the port.

0
1

Similar issue. I had written a ServiceStack Service and hosted it inside a Windows service using an AppSelfHostBase implementation. Note: This was written as a Net Core application that compiles as a console application but, through the magic of PeterKottas.DotNetCore.WindowsService (available through NuGet) can be installed as a Windows Service.

I set the baseAddress to http://*.8088/ and passed it to my AppSelfHostBase implementation, but I could only reach the service from the same machine it was installed on and no other, as ascertained by trying to open the metadata page (http://[machine]:8088/api/metadata) in a browser. The solution was to add my console application .exe file to the list of allowed apps in Windows Firewall on the server where it was installed. Just go to the Firewall "allowed app" dialog in control panel on the server, click the "Allow another app..." button, and navigate to the Windows Service executable:

Windows Firewall Allowed App dialog

3
1

What worked for me in the end was this value:

StartOptions options = new StartOptions();
options.Urls.Add("http://*:9095");

using (WebApp.Start<Program>(options))
{
    ...
}

Be aware that after adding this, you can only run the application in Visual Studio using Admin mode and the release version of the app needs also to be run in Admin mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.