How to retrieve the client's machine name from within a WCF Operation Contract?

chilltemp picture chilltemp · Feb 17, 2011 · Viewed 9.8k times · Source

I'm currently looking at the OperationContect.Current properties. Is there a (nested) property the will always return the machine name of the client? I'm currently using net.tcp binding, but would like to support additional bindings in the future.

Using .NET 3.5 SP1

Answer

Chris Wenham picture Chris Wenham · Feb 17, 2011

You can get the remote endpoint's IP address from the current OperationContext's IncomingMessageProperties, eg:

RemoteEndpointMessageProperty messageProperty = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
Console.WriteLine("Remote address is: {0}", messageProperty.Address);