Getting client IP address in WCF 3.0

Obviously, you can easily get the client IP address in WCF 3.5, but not in WCF 3.0. Does anyone know how?

+80
wcf
Sep 18 '08 at 14:40
source share
3 answers

It turns out that you can, if (a) your service is hosted in a web service (obviously) and (b) you activate AspNetCompatibility mode as follows:

<system.serviceModel> <!-- this enables WCF services to access ASP.Net http context --> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> ... </system.serviceModel> 

And then you can get the IP address:

 HttpContext.Current.Request.UserHostAddress 
+35
09 Oct '08 at 16:34
source share

This will not help you in version 3.0, but I can just see how people find this question and get upset because they are trying to get the client IP address in 3.5. So here is some code that should work:

 using System.ServiceModel; using System.ServiceModel.Channels; OperationContext context = OperationContext.Current; MessageProperties prop = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; string ip = endpoint.Address; 
+151
Sep 18 '08 at 15:11
source share

You can target .NET 3.0 SP1.

 OperationContext context = OperationContext.Current; MessageProperties prop = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; string ip = endpoint.Address; 

Credits: http://blogs.msdn.com/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx

Link: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.remoteendpointmessageproperty.aspx

+15
Oct 10 '08 at 8:26
source share



All Articles