WCF 获取客户端IP

时间:2022-05-03
本文章向大家介绍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;
        }
    }