Luke Belbina
Luke Belbina

Reputation: 5859

WCF 4 Rest Getting IP of Request?

Hey, how do you get the IP address of the person making a request in something like the following:

    [ServiceContract]    
    [AspNetCompatibilityRequirements(RequirementsMode = 
    AspNetCompatibilityRequirementsMode.Required)]    
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]        
    public partial class UsersService
    {                          
        [WebInvoke(UriTemplate = "", Method = "PUT")]        
        public User AddNewUser(User newUser)
        {            
            // code goes here including GETTING AN IP??
        }

Thanks!

Upvotes: 21

Views: 14690

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

Inside AddNewUser use following snippet:

OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty =
  messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

RemoteEndpointMessageProperty instance offers Address and Port properties.

Upvotes: 39

Related Questions