代码如下:

public string GetClientIpAndPort()
        {
            OperationContext context = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return endpoint.Address + ":" + endpoint.Port.ToString();
        }

 

注:客户端是本机时,获取的 IP 为 ::1 (非本机无影响)。

posted on 2020-11-19 18:06  青叶煮酒  阅读(229)  评论(0编辑  收藏  举报