How to make an ASP.NET Web API web service available on a local network

I am going to write a JSON web service (VS Express 2012 - ASP.NET WEB API) and now I want to test it while trying to get data from my Android application. Therefore, I tried to make my service available for the local network by doing the following:

-> 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 the project properties → Internet → "Use local IIS-Webserver" → project-url to http://192.168.1.52:61802/

But when I try to start the service now, it gives me an error message:

Failed to start the IIS-Express web server (Translated from German)

Unfortunately, I do not see what the exact error is, because the VS output is empty

Does anyone know how to fix this or how to configure it correctly?

+6
source share
3 answers

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

IIS Express allows external request

Once you do this, you can connect to ANY other computer on your network.

Also make sure you open the port in your firewall.

If this does not work, I would create a site using local IIS, and thus test your service.

Hope this helps, Daniel.

+5
source

You can run scripts so that IIS Express allows remote connections, and another option is to use the self-service wrapper for your api web code for local testing. We had the same problem, and we started using this method. Here's how you do it:

Create a new console project and add the following code:

 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 already noted, run Visual Studio as an administrator.

We use AutoFac for dependency injection, which is pretty well connected with this, see the manual here .

Now run the console project (as a launch project, or right-click> debug> start a new instance).

+3
source

Hey. Remove the IP name from ApplicationHost.config.

Create a virtual directory and publish the webservice files.

Set IP Address: - Right-click on the default website. - Change the binding - Assign an IP address

Restart IIS.

Browe "Default Website" It will be displayed with your IP address.

+1
source

Source: https://habr.com/ru/post/952781/


All Articles