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.
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:
var uri = '/api/tests';
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}.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With