Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ASP.NET Web API web-service available in local network

I'm about to write a JSON-web-service (VS Express 2012 - ASP.NET WEB API), and now I want to test it by trying to get the data from my Android-app. So i tried to make my service available for the local network by doing this :

-> changed from localhost to 192.168.1.52 in applicationhost.config

<site name="MyFirstWebApi" id="5">
     <application path="/" applicationPool="Clr4IntegratedAppPool">
         <virtualDirectory path="/" physicalPath="c:\projects\MyFirstWebApi" />
     </application>
     <bindings>
         <binding protocol="http" bindingInformation="*:61802:192.168.1.52" />
     </bindings>
</site>

-> and also changed Project-properties -> Web -> "Use local IIS-Webserver" -> project-url to http://192.168.1.52:61802/

But when I'm now trying to start the service, it gives me an error-messagebox:

The IIS-Express-webserver could not be started (Translated from german)

unfortunatly I can't see what's the exact error, because the VS-output is empty

Anyone know's how to fix that or how to set this up correct?

like image 215
Postback Avatar asked Aug 29 '13 19:08

Postback


2 Answers

IIS Express by default does not allow "remote" connections... this post shows you what to do:

IIS Express enable external request

Once you do that, you should be able to connect from ANY other computer in your network.

Also, make sure to open the port in your Firewall.

If that does not work, I would create a site using your local IIS and testing your service that way.

Hope it helps, Daniel.

like image 138
Daniel Avatar answered Sep 19 '22 23:09

Daniel


You could run the scripts to make IIS Express allow remote connections but another option is to use a self-host wrapper for your web api code when testing locally. We had the same issue and started off using this method. Here's how you do it:

Create a new console project and add code like this:

private static void Main()
{

  var config = new HttpSelfHostConfiguration(new Uri("http://localhost:8086"));

  // remove all formatters and only provide json
  config.Formatters.Clear();
  config.Formatters.Add(new JsonMediaTypeFormatter());

  // if needed
  SetupLogging();
  SetupDependencyInjection(config);

  // required for the new Attribute Routing (if you are using it)
  config.MapHttpAttributeRoutes();

  using (var server = new HttpSelfHostServer(config))
  {
    server.OpenAsync().Wait();
    Console.WriteLine("Self Host Server listening on port 8086 ...");
    Console.WriteLine("(You might need administrator privileges)");
    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
  }
}

As noted, please run Visual Studio with Administrator rights.

We're using AutoFac for dependency injection which ties into this quite well, see the guide here.

Now run the console project (set as startup project or right click > debug > start new instance).

like image 31
mjduminy Avatar answered Sep 19 '22 23:09

mjduminy