I'm working on a project where I have two websites (client and a server). I've opened web service and I'm trying to "send" a picture from my server to the client

public string PictureUrl(string id)
    return "http://localhost:49400/Website13/images/" + "PICTURE NAME";

It works only when I know the port for my web service. What I'm looking for is if there's any method where I can get the port of my we service even if it changes, and making the code "looking" like this :

public string PictureUrl(string id)
    return "http://localhost:" + GET_PORT_METHOD + "/Website13/images/" + "PICTURE NAME";

and the, I'll be able to use this method in my client and receive the picture. (it's like uploading a picture into an online website and then getting the URL, but since It's only a project I'm not buying a domain my server will not be online).

  • 3
    The port is integral part of the address of your service. You are asking: If I know the street how can I find the house number? In general you cannot or you should walk the whole street (port scanning). This is the reason why most services have well-known port numbers like: 80 = web, 21 = ftp etcetera. Only way you could know it is if the server contacts the client (but then needs to know the address of the client). Apr 16 '15 at 8:13
  • 1
    Your best bet is to bind to a known port... you should be able to do this in your project configuration file... Right click the project and go to web settings. Set the port to a fixed number and save all. You can then pass this to the client. At in your main master page/layout template, add a script field before your client script blocks that injects this as a value from your web config. Use a config transform for your production settings.
    – Tracker1
    Aug 15 '15 at 5:57

You can avoid this issue by using a host name without a port and leaving the mapping of that host name to the target port to the server. This way, when the port changes (which is something you do on the server) you can change the mapping as well (which would also be on the server).

Depending on the web server you are using, you could use Host Headers (IIS) or Name-Based Virtual Hosts (Apache). This way, you define 'http://myservice/images' as the URL and let the webserver handle what port the URL goes to.

On a more low-level approach you could scan all the ports (probably the worst option), or implement something similar to the way DHCP works where your client broadcasts a message via UDP and the server (who knows the address for the client from the broadcast) responds with the information. But there is nothing in your question that you couldn't solve using the above method without resolving to these more extreme implementations.



There are specific implementation answers to this problem developed by various software stacks, see JDTs answer.


I think a fairly simple solution is to get your router to do this for you. You can define a mapping between incoming requests at a known and static port on the router and the server (including the port number) the requests should be forwarded to.


The general problem though of trying to refer to an endpoint (host + port of a web service) when you don't know all the variables has been solved in large enterprises by using a registry service (e.g. WSRR). A third application at a known location in the network that will answer the question: Where is this web service? Whenever the webservice changes its host or port it needs to up date the registry with its new location.

Config Store

The more modern equivalent of the registry is to keep a distributed config store, such as Apache ZooKeeper, up to date with the location of all the systems in your network. This can act the other way round to the registry, in that you can update the config store with what you would like the network configuration to look like and watchers will respond to that change by spinning up the service at the required location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.