WCF 4休息获取请求的IP?

时间:2010-08-28 01:40:39

标签: c# wcf rest tcp

嘿,你如何获得发出请求的人的IP地址如下:

    [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??
        }

谢谢!

1 个答案:

答案 0 :(得分:38)

在AddNewUser内部使用以下代码段:

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

RemoteEndpointMessageProperty实例提供地址和端口属性。