代码如下:
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 (非本机无影响)。