Getting the IP address of a client consumer of a WCF service
When developing those network hosting services (over HTTP or TCP), it is useful to get the IP address of the client-side callers/consumers so as to perform some counting or analysis work. Started from .NET Framework 3.5, WCF also provides built-in interfaces for service developers to conveniently get the IP address of client consumers of certain WCF services/endpoints. We can get the IP address of a client consumer as long as the underlying transport protocol is HTTP or TCP based (such as WSHttpBinding, NetTcpBinding, BasicHttpBinding, and so on).
How to do it...
Get a reference to the message properties from
OperationContext
.The IP address information is embedded in the message properties transferred from client to server. We can use the
OperationContext.Current
property to access those message properties in the service operation code. The following code snippet shows how to obtain the reference to message properties within service...