WCF服务如何获得客户端IP地址信息

这里给出服务端获取客户端IP地址信息的示例代码分析和实现过程,这里的测试主要是针对HTTP、TCP相关的协议做了4个测试。NamePipeBinding等协议不做测试了,本地协议不需要IP和端口。我们主要测试的是几个主要的协议,来验证以上的结论。

1服务端:

   主要是对RemoteEndpointMessageProperty属性的使用来获取地址、端口信息。具体代码如下:

 //1.服务契约

[ServiceContract(Namespace = "http://www.cnblogs.com/frank_xl/")]
    public interface IWCFService
    {
        //操作契约
        [OperationContract]
        string SayHelloToUser(string name);

    }
    //2.服务类,继承接口。实现服务契约定义的操作
    public class WCFService : IWCFService
    {

        //实现接口定义的方法
        public string SayHelloToUser(string name)
        {
            //提供方法执行的上下文环境
            OperationContext context = OperationContext.Current;
            //获取传进的消息属性
            MessageProperties properties = context.IncomingMessageProperties;
            //获取消息发送的远程终结点IP和端口
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            Console.WriteLine(string.Format("Hello {0},You are  from {1}:{2}", name, endpoint.Address, endpoint.Port));
            return string.Format("Hello {0},You are  from {1}:{2}", name, endpoint.Address, endpoint.Port);


        }
    }

2宿主:使用的是控制台托管宿主方式,延续上一个些列的特色。大家很应该很容易就上手了。简单介绍一下配置过程。    这里配置了HTPP和TCP相关的4个服务终结点进行测试:

<service behaviorConfiguration="WCFService.WCFServiceBehavior"
        name="WCFService.WCFService">
        <endpoint 
          address="http://localhost:8001/WCFService" 
          binding="wsHttpBinding" contract="WCFService.IWCFService">
        </endpoint>
        <endpoint 
          address="net.tcp://localhost:8002/WCFService" 
          binding="netTcpBinding" contract="WCFService.IWCFService">
        </endpoint>
        <endpoint
          address="http://localhost:8003/WCFService"
          binding="basicHttpBinding" contract="WCFService.IWCFService">
        </endpoint>
        <endpoint
          address="http://localhost:8004/WCFService"
          binding="wsDualHttpBinding" contract="WCFService.IWCFService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8001/"/>
          </baseAddresses>
        </host>
      </service>
    </services>

3客户端:启动宿主和添加服务引用的过程就详细介绍了。我们分别针对每个不同的服务终结点,实例化代理进行测试。宿主会输出信息,而且客户端打印返回的消息。代码如下:

 //1.WSHttpBinding_IWCFService
            using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
            {
                string strUserName = "Frank Xu Lei WSHttpBinding ";
                string strMessage = "";
                //通过代理调用SayHelloToUser服务
                strMessage =wcfServiceProxyHttp.SayHelloToUser(strUserName);
                Console.WriteLine(strMessage);
            }
            //2.NetTcpBinding_IWCFService
            using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
            {
                string strUserName = "Frank Xu Lei NetTcpBinding ";
                string strMessage = "";
                //通过代理调用SayHelloToUser服务
                strMessage = wcfServiceProxyHttp.SayHelloToUser(strUserName);
                Console.WriteLine(strMessage);
            }
            //3.BasicHttpBinding_IWCFService
            using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
            {
                string strUserName = "Frank Xu Lei BasicHttpBinding ";
                string strMessage = "";
                //通过代理调用SayHelloToUser服务
                strMessage = wcfServiceProxyHttp.SayHelloToUser(strUserName);
                Console.WriteLine(strMessage);
            }
            //4.WSDualHttpBinding_IWCFService,
            using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
            {
                string strUserName = "Frank Xu Lei WSDualHttpBinding ";
                string strMessage = "";
                //通过代理调用SayHelloToUser服务
                strMessage = wcfServiceProxyHttp.SayHelloToUser(strUserName);
                Console.WriteLine(strMessage);
            }
4测试结果:

   启动宿主、客户端,进行测试测试。结果如图:



总结:

    1)以上测试可以看出,客户端的每次请求,服务端都可以获取响应的地址信息:IP+Port.获得以后大家可以再进行别的处理,比如系统日志LOG、安全限制等等。

    2)此特性只是对http和tcp相关的绑定器作用,其它绑定WCF没有做支持,和实际绑定的应用有关系。比如NamePipe等。

    3)双向通信,这个属性可以获取服务端的信息。

    4)这个属性没做欺骗检测,如果使用作为安全标准,要慎重考虑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值