Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API self hosted client configuration

I managed to run my self hosted WEP API using OWIN in a console application by starting it with a code like this:

//string baseAddress = "http://192.168.1.6:8111/";
string baseAddress = "http://+:8111/";

// Start OWIN host 
using (Microsoft.Owin.Hosting.WebApp.Start<Startup>(url: baseAddress))
{
    Console.ReadLine();
}

By using and registering an address like "http://+:9000/" on the service host machine the idea was to use a generic IP address for the host that would not affect the clients when the IP of the host might change.

The clients are on other machines than the one that is running the service. Something like a mobile phone from the LAN or another laptop from the LAN, and in the future if possible also outside the LAN.

In the client of my self hosted service, which is a html page, I have a JavaScript code like:

//var uri = 'http://192.168.1.6:8111/api/tests';
var uri = 'http://+:8111/api/tests';

function Read()
{
    $.getJSON(uri + '/' + id)
}

By using the static commented IP address of the host in the client I can get to the self hosted WEB API but when I try to use the generic one "http://+:9000/api/tests" it fails to connect to the service.

Is there a way to connect from the client to the service by using such a generic configuration ? or how should I configure the service host machine and the client so that an IP change on the host will not stop the service on the client machine ?

I need to take into account that the IP address of my self hosted machine might change and the clients will lose the connection since they will use an old outdated IP address of the service host machine.

like image 783
Clock Avatar asked Feb 11 '18 16:02

Clock


2 Answers

In your top example, where you host a service listening on a specific port, you are stating that no matter how the request found the server, direct IP, DNS resolution or what have you, if it is came in through http and on port 8111 then send it through your code.

In your second example, the javascript, you are making a request. This request can not say send this request to any IP on port 8111. See the difference?

I would assume that server that served up the javascript is also the one you wish to connect with. If that is the case there are two options:

  1. Simply change your the uri value in your javascript to this:var uri = '/api/tests';
  2. Make your javascript dynamic and have it use the Request.Url.Authority from the request that generated the web page containing the javascript. I will refrain from an example due to me not knowing what you are using to deliver the javascript.

If that is not the case, then you need to understand that the client needs to know where it is to make this api/test/{id}.

like image 44
Larry Dukek Avatar answered Oct 27 '22 00:10

Larry Dukek


If the clients are within the same LAN you can request the host by name instead of IP address.

To find the host name, open a command prompt on the host machine and type: hostname

It will show the host name, for example myhost. Then you can request it as http://myhost:8111 or whatever the port is.

For clients outside of your LAN you have to use DNS. Or connect via VPN if that's an option.

like image 153
Daniel P Avatar answered Oct 27 '22 00:10

Daniel P