Apparently you can easily obtain a client IP address in WCF 3.5 but not in WCF 3.0. Anyone know how?
+16
A:
This doesn't help you in 3.0, but I can just see people finding this question and being frustrated because they are trying to get the client IP address in 3.5. So, here's some code which 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;
Paul Mrozowski
2008-09-18 15:11:10
I couldn't edit the post, but it helped me a ton, thanks! Wanted to mention there are 2 errors. Should be "OperationContext" instead of "OperationContent" and should be "RemoteEndpointMessageProperty" instead of "RemoveEndpointMessageProperty".
Jeremy Mullin
2009-10-14 04:19:30
@Jeremy I corrected the errors
Jader Dias
2010-03-09 17:54:18
+3
A:
It turns out you can, so long as (a) your service is being hosted in a Web Service (obviously) and (b) you enable AspNetCompatibility mode, as follows:
<system.serviceModel>
!-- this enables WCF services to access ASP.Net http context -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
...
</system.serviceModel>
Gaz
2008-10-09 16:34:44
And then you get it by using `HttpContext.Current.Request.UserHostAddress`
Jader Dias
2010-03-09 18:16:54
+3
A:
You can if you are targeting .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
Ok, I'm looks like getting an IPv6 like "fe80::3dbc:a2ec". I was wandering how could I get the remote IP number
Junior Mayhé
2009-07-24 01:21:31