Monday, December 6, 2010

Get HostName from client in WCF

What a pain! but this works. Add references to:

using System.ServiceModel.Channels;
using System.Net;

.....


Then this will get the client's IP address from the incoming message header on your IIS hosted WCF Web Service and resolve to the host:

string ComputerName = "NOTFOUND";

try
{
var remp = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string[] computer_name = Dns.GetHostEntry(remp.Address).HostName.Split(new Char[] { '.' });
ComputerName = computer_name[0].ToString();
}
catch (Exception e)
{
ComputerName = "NOTFOUND";
}

Search Brian Hehir's sites

Loading