WCF 获取客户端IP

  public class Service2 : IService2
    {
        public User  DoWork()
        {
            Console.WriteLine(ClientIpAndPort());
            return new User { Name = "sdf", Age = 12 };
        }
        public string ClientIpAndPort()
        {
            OperationContext context = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

            return endpoint.Address+"port"+endpoint.Port;
        }
    }

 

你可能感兴趣的:(WCF)