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";

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";

1 comment:

  1. Nice example. I used it. You do not need the ToString() here:
    ComputerName = computer_name[0].ToString();


Search Brian Hehir's sites