tags:

views:

3990

answers:

5

Apparently you can easily obtain a client IP address in WCF 3.5 but not in WCF 3.0. Anyone know how?

A: 

Simply put you can't.

blowdart
+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
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
@Jeremy I corrected the errors
Jader Dias
+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
And then you get it by using `HttpContext.Current.Request.UserHostAddress`
Jader Dias
+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

Reference: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.remoteendpointmessageproperty.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é
This is a real good way to dot it
Cédric Boivin
A: 

i don't know sure why for me it is giving 127.0.0.1, what i wanted was the private and public ip address of machine. it is network

shabby
forget it, i figured it out
shabby