技术开发 频道

WCF 取得客户端IP


【IT168技术文档】

  简单定义一个服务:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace ClientInfoSample { [ServiceContract] public interface IService { [OperationContract] string GetData(string value); } }
  在建立通道之后按照可以取得:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Channels; namespace ClientInfoSample { public class MyService : IService { public string GetData(string value) { OperationContext context = OperationContext.Current; MessageProperties essageProperties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpointProperty = messageProperties [RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; return string.Format("Hello {0}! Your IP address is {1} and your port is {2}", value, endpointProperty.Address, endpointProperty.Port); } } }
0
相关文章