How to get a host name?

WCF: How to get Host IP or Server Name

  • I have a WCF service that is hosted via IIS on multiple web servers. I have a logging method that logs the calls to the database and I'd like to log which server the call is executing on. Does anyone know how to get the host server name or IP address that the WCF call is executing on?

  • Answer:

    This is what worked for us. Make sure you have a reference to System.ServiceModel. Then implement the following code in your service method: var context = System.ServiceModel.OperationContext.Current; RemoteEndpointMessageProperty property = (RemoteEndpointMessageProperty)context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name]; string externalIP = property.Address;

Kevin Jensen at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Dns.GetHostName() is what i use the most.

Darren Kopp

Use Dns.GetHostName(), because it's much better to get the name of the computer than the host IP. Consider the case when your host computer have more than one IP address, has virtual network cards (VMWare).

Tomas Walek

Environment.MachineName does axactly that

VdesmedT

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.