WCF: How to get Host IP or Server Name

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


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


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;

Tags:

Wcf