Can't find the device thats self hosting a web api - c#

I'm using Owin and Topshelf to selfhost an ASP.net Web Api.
I am able to access it through localhost and 127.0.0.1 on the device that is self hosting it.
The idea is that any device that comes onto the network and has my computer or mobile application installed is able to communicate with that web api to request/update/remove data from the database hanging behind.
The issue i am having is that i have no way of finding out on what internal ip the service is actually running. I have no control/view over the network that it's installed onto (except through my code), this means i don't know what the ip or hostname might be, only the port number (on any other device that isn't hosting it).
I tried adding custom url's like shown below
StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:6969");
options.Urls.Add("http://127.0.0.1:6969");
options.Urls.Add(string.Format("http://{0}:6969", Environment.MachineName));
options.Urls.Add("http://+:6969");
Obviously localhost and 127.0.0.1 won't work for a remote device on the same network. The issue with the MachineName is that i don't have a say in the name of the machine either, meaning this could be anything. I assumued the wildcard would allow me to send a request on port 6969 and the host would catch it, this was sadly not the case.
This leaves me in a bit of an issue, i now have a self hosted Web api, on a machine with a random ip and name, and it needs to be accessed by other devices on the same network, The users are not able to manually enter ip, and i can't force a name onto the host computer. This all leaves me in a situation where im not sure wat else i could try, or if its even possible to reach the host computer without human interference.
Edit: I did come across something along the lines of a
network broadcast address
Which should in someway allow me to send out a message from a device, and let the server respond with its ip. Not sure how i would apply this to my self hosted web api though.

Basically, the idea is to have your web API application register its address to a service, such as DNS, which may be updating the IP address of a DNS A record. Then clients will query this service or DNS, to resolve the address to your application.

Related

c# restful selfhosted api winforms works with my local ip but dosent work with my plubic ip event with port redirect

So the problem is this, I created a restful selfhosted api, to work with my mobile app, on the app mobile I stored my public ip (190.xxx.xxx.xxx) when I try to consume the api from the same network donset work no response, If I go 3g or in anoter network its work fine.
I try on my browser those 2 situation:
http://localIp:port/api/Menu/... its work
http://externalIp:port/api/Menu/... dont work - ps. this work only if I on a diferent network or 3g.
But I need to keep sotored my plubic IP for the external users and I dont want to store 2 ips internal and external to check if the user is on local network or external.
in summary my clients can use the app outside the office, but in the office with wifi connected they cant.
Tks for anyhelp
I found this and solve my problem
https://docs.connectwise.com/ConnectWise_Control_Documentation/On-premises/On-premises_knowledge_base/Cannot_access_external_IP_address_from_LAN
Introduction
In some scenarios, a user cannot use an external IP address to access a machine on their local network. This is typically a result of security measures put in place by routers and referred to as a Network Address Translation (NAT) loopback issue. This article will discuss what this means for ConnectWise ControlĀ® on-premises users and will provide some suggestions.
What is NAT loopback?
Many routers and some security tools prevent loopback connections as a security feature. This means that a machine on your local network cannot connect to the external IP address (such as 208.112.93.73) of a machine that is also on your local network. Connecting to a local IP address (such as 192.168.0.2) of that same machine works fine.

Why is the uri for a self hosting application set as localhost?

I recently started looking into Asp.net Web Api self hosting. I'm following this article and I have two questions based on that.
Why is the URI for HttpSelfHostConfiguration pointing to localhost?
How would I give a website name like I would in IIS for the windows service? Because if I deploy this to a production server, how would the clients call the localhost?
Q. Why is the URI for HttpSelfHostConfiguration pointing to localhost?
A. localhost simply just means "this computer". It's typical to do it this way to make the intent clear. Nothing stops you from using an ip address.
Q. How would I give a website name like I would in IIS for the windows service? Because if I deploy this to a production server, how would the clients call the localhost?
A. You would change from HttpSelfHostConfiguration("http://localhost:80") to HttpSelfHostConfiguration("http://000.000.000:80") where 000.000.000 is the ip address of the host (the machine you are running the web application on)
Localhost is reserved loopback adres (ea 127.0.0.1) try ping localhost.
The origin is basic networking, it existed before .net
Pinging localhost was quite common to check if your own network card still worked. As it ping 127.0.0.1, is a ping to your own network card. Next step in conectivity issues was pinging your gateway and then some remote IP or so.
A client cannot connect to a remote localhost (it will point its own network card).
in regard to the article, a server listens to its own network card.
unless you have some multiple nic environment with specific routes to follow
but then there is a config file for that.
Localhost is the default allocation of Windows, if you want to change the default name binding,
answer is simple just go to
C:\Windows\System32\drivers\etc\ folder
you can find as hosts file there just open it in notepad, if you want to change the locally hosted name add a new line in that text file like
127.0.0.1 domain name you want to use
for example
127.0.0.1 techdoubts.net
then your localhost will be changed to techdoubts.net
and one more, add that new name in internet information services website binding also.

how should I host my service such that other computers can consume myAppService using Internet

I have created a WCF service called MyAppService.
Now, I don't want to host that service on any servers. I would like to host it in my Computer. I know how to host a WCF service in Console/Windows App, as a Windows Service and in IIS.
But I have a problem to host this service in my computer. I mean in short that I want my Computer to Work as server and other computers should be able to consume myAppService from my computer via internet.
So, I would like to ask what is the best hosting option for me and how should I host my service such that other computers can consume myAppService using Internet. 1 more question : If the above problem is solved, then can I use netTcpBinding or I have to use some HttpBinding?
I'm still not 100% certain that I am answering the right question, but I'll give it a shot.
I believe what you are asking is: given that you are hosting a service in IIS on your personal computer, how do you allow others access to it? If my interpretation is incorrect, then please accept my apology in advance.
Note: I'm assuming IPv4 in this answer. IPv6 may be an option, but this answer would not apply.
There are two steps in the solution, and there are a few obstacles that you may or may not be able to overcome.
Get the client's web traffic to your public IP address.
Route incoming traffic to your computer.
Step 1
You can determine your public IP address in many ways, one of which is to ask Google: https://www.google.com/#q=what+is+my+ip
Obstacles are as follows:
Your IP address may change from time to time. The solution is to use a dynamic DNS service such as http://dyn.com/dns/, http://www.thatip.com/, or any of several alternatives that can be found online. These services will provide a URL that routes to your public IP, and they will provide a way to keep your public IP up-to-date so that name resoslution generally works. Keep in mind that DNS does not reliably change in realtime, however, so you should expect some downtime if and when your IP address changes.
Your ISP may block incoming traffic. Whether and how you can get around this is specific to your IP. Specifically, it is common for ISPs to block incoming port 20, 22, 23, 25, 80, and 443. You may be able to work around port blocking by choosing a non-standard port for your service. (This may not affect your WCF and IIS configuration as detailed in step 2.)
Particularly if you are using any kind of shared apartment connection, you may find that you do not have your own public IP address. If that is the case, then there is nothing you can do. If your router's WAN address (or computer's IP addres, if you're not using a router) is something in the 192.168.* or 10.* ranges, then it's time to look into server options.
Step 2
Once a client request has routed through the public internet to your public IP address, it needs to be routed to your computer.
The easiest, though least common these days, scenario is if your computer is directly connected to your cable/DSL modem. In this case, traffic that routes to your public IP is handled by your computer. This is the scenario where your WCF and IIS configuration needs to be listening on the actual port used by the client. For example, if your ISP is blocking port 80, then maybe you could configure your service to listen on port 34323, and that is the port that the client needs to request in the service call.
The most common scenario is that you have a router attached to your cable/DSL modem. In this case, you need to get into the administration control panel on your router and forward a port. For example, if your computer is configured to listen to port 80 for incoming service requests, then you can forward a port (for example, 34323) to port 80 on your local machine. How to forward the port differs by router. You should be able to find it by poking around or by doing an internet search.
The obstacle here is that your local IP address may change. You can overcome this by setting it to a static address outside of your router's DHCP assignment range or by setting up a DHCP reservation within your router. Either way will work. I suggest searching the internet for setting up a static IP or poking around in your router administration settings for DHCP reservations.
If you get all of your settings correct, and if there are no obstacles outside of your control, then a client on the internet can access your WCF service.
Edit:
I missed talking about local Windows firewall concerns. I could go into that, but allow me to link to this how-to guide that covers it pretty well. (Remember, a WCF service is conceptually similar to a website from a hosting standpoint, so much of this applies.) http://www.pcstats.com/articleview.cfm?articleID=1774

Unabled to access locally hosted wcf service over the internet

Running wcf service on local machine and trying to connect from another machine by public IP address on specified port not LAN internal IP.
basically I wanted to make chat application without including any central server. client machine would be treat as a server like torrent.
You have to forward the port from your router to the computer on your local network. That's because the router is in between your computer and the Internet, and so your router is actually the one with the Public IP.
There is a site that has good guides for forwarding ports on many different router models. Also check your router's manual if you still have it.

c# socket server - deploying, starting, stopping

I'm a little confused when it comes to socket servers in terms of deploying it online.
Running locally is fine as most tutorials get you to make a server application and a client application which I can execute. Done all that for awhile now and I'm happy with it but now I want to try it using a web host.
How would I deploy the socket server to my web host and then run the server? do I just upload the program to the server and run www.mywebpage.com/mysocketserver (assuming the program is called mysocketserver.exe)
may sound like a stupid question but I'm having one of those brain dead moments.
[Edit]
Great answers guys thank you. Shame I can only mark one as the answer.
Most hosting services are website related: you upload your website to their server and the host takes care of provisioning the application.
However you haven't written a website that is served by a web server, but a socket server, which is more akin to the web server itself that runs a website.
As a result you will need a host that will allow you to install and run applications, such as a Virtual Private Server service, rent a physical server or use a cloud service such as Amazon EC2.
You usually Remote Desktop or SSH into the server you are renting (or own) to upload and start the application either as a normal application, a Service or a Daemon.
Once you have installed your socket server on the host server and started the application running you should be able to contact your service using the IP address for your server and the port that you are running your socket server on.
For example, if your ip address is 10.0.0.1 and you've written your socket server to listen on port 1234, you should be able to contact your service at address 10.0.0.1:1234.
Take into account firewalls allowing access to that port.
You will also then be able to use a DNS service such as DynDns to assign a domain name to that ip address.
You can't do that unless your socket server is using HTTP as a protocol.
You are using a port for your server, right? The ports are used to identify which application to talk with.
When you browse the web using "http://something" you really say let me get the stuff which can be found on the IP 1.2.3.4 (DNS lookup) using port 80 (which is registered for HTTP). You don't have to specify the port in the browser since all browsers know that HTTP uses port 80.
So what you are really should do is to put the socket server on your host and tell your customers/users that they can connect to your socket server at port XXX on host "www.mywebpage.com".
If you've built a client you'll just hard code the port in it or specify the default port automatically.
The problem in the Internet is the routing/name translation, so that mywebpage gets translated into the correct IP address.
Either your webserver needs a fixed public IP address or you need to use a service like dynds which will dynamically map your changing IP address to the static name.

Categories

Resources