6

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Clock
  • 974
  • 3
  • 17
  • 35
  • 2
    This is what DNS is for: registering names for IP addresses. Otherwise you can’t do anything. + is just for the binding, it isn’t allowed in actual URLs. Best would be if you can make the host IP not change. Or you need some form of discovery system. – Sami Kuhmonen Feb 11 '18 at 16:23
  • Thank you for reply ! So the DNS should be created on the host, but that as far as I understood will require the IP of the host to be a static one ? – Clock Feb 11 '18 at 17:23
  • The clients need a way to find the host. If you can't guarantee that the host has a static IP you need some sort of discovery system. Or perhaps you can use dynamic DNS. – RasmusW Feb 11 '18 at 19:04
  • @RasmusW, I am not sure what is a dynamic DNS, could you please give me a link about that ? – Clock Feb 11 '18 at 21:37
  • @Lex Li You mean to set into the client the server host address to localhost ? That sounds a little strange, may be I did not understood you well ? – Clock Feb 11 '18 at 21:41
  • @Lex Li The clients are on other machines than the one that is running the service. Something like a mobile phone from the LAN or another PC from the LAN, and in the future if possible also outside the LAN. – Clock Feb 11 '18 at 21:44
  • @Lex Li Ok, I will update also the question body. – Clock Feb 11 '18 at 21:47
  • In active directory environment, you can usually use FQDN of the machine to replace the IP address. Again, update the question to reflect your environment, or others have to guess. – Lex Li Feb 11 '18 at 21:47
  • @Lex Li, I updated the question. Hopefully it can be better understood now. For you is it more clear now ? – Clock Feb 11 '18 at 21:52
  • Are you sure you tried FQDN (full name including domain) and not just hostname alone? You can look for "computer name" and "domain suffix" in "ipconfig /all" output then combine them with dot (to get something like "myhostname.my.domain"). – Evk Feb 15 '18 at 07:33
  • Clients should be aware of server's address (Name or IP). If the IP may change, then you need to use DNS name. Also by change of the IP, you need to update the DNS record. – Reza Aghaei Feb 15 '18 at 11:53
  • @RezaAghaei Hi, thank you for suggestion, regarding the DNS do you consider that this should work also for clients that are running Android, like a phone ? – Clock Feb 15 '18 at 12:07
  • 1
    Hi, All clients can resolve domain names. For example, the same way that your android client can see `google.com`, they can see your domain name. For a LAN, it's enough to register that name on local DNS servers and you don't need to register the name globally. – Reza Aghaei Feb 15 '18 at 12:10
  • Thank you for your suggestion, now things tend to became a little clear, do you know a link or something where I can read the steps that needs to be done in order to register the hostname on the local DNS server so that I can give it a try on my local network (a WIFI router that stands over the Internet, 2 laptops, one running the service and an Android phone :) ) ? – Clock Feb 15 '18 at 20:19

2 Answers2

10

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.

Daniel P
  • 3,314
  • 1
  • 36
  • 38
  • Hi, thank you a lot for suggestion ! I tried something like that, using the full qualified domain name of the host as @Lex Li suggested in a comment above, and for clients that were running on Windows machines it was working fine (the host IP change was not affecting the clients). But for instance for a client that was a mobile phone running Android this was not working, only when the IP address of the host (which might change) was used the service was working fine also on mobile Android devices. Probably an Android device does not know to recognize a Windows host name... I am not sure why. – Clock Feb 15 '18 at 06:35
  • 1
    @Clock That seems to be an Android specific issue. Check this out: https://android.stackexchange.com/questions/52140/how-to-reach-local-computer-via-machine-name-instead-of-ip – Daniel P Feb 15 '18 at 10:08
  • Thank you for the given link I will check it ! – Clock Feb 15 '18 at 20:03
  • 1
    You also should make sure that the firewall isn't blocking – Yitzchok Feb 18 '18 at 05:33
  • Just one more thing to ask, as far as I read about this, I understood that the service from the host beside the DNS, can be accessed also from a VPN that will bring the client to the local network of the service host, will this work, what do you think ? – Clock Feb 18 '18 at 20:28
  • @Clock Yes, it will work. As you said with VPN it's as if you are on the local network. – Daniel P Feb 18 '18 at 20:48
2

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

Larry Dukek
  • 2,179
  • 15
  • 16