WCF service 获取 client 端的 IP 和 port (转)

 

转帖记录一下,以便日后使用。

主要使用是.NET3.5里的服务端上下文的消息实例的RemoteEndpointMessageProperty属性,获取客户端地址信息。但是限制 的绑定是HTTP、TCP相关的绑定协议。网络通信的底层机制来说,数据包如果经由TCP传输,IP数据包应该包含地址和端口信息,这个我们网络编程也可 以理解。但是WCF获取客户端地址信息早期却没提供相应的实现。其实按照道理来说没什么难度。只是多做个数据包的解析工作,然后把地址信息包装即可。

【3】示例代码:

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

【3.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);


        }
    }

【3.2】宿主

 

<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.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);
            }

 

【3.4】测试结果:

 

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

 

 

 


【4】总结:

 

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

 

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

 

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

 

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

 

     以上就是WCF获取客户端机制的介绍和代码实现的讲解部分,大家基本实际项目开发可以考虑结合自身情况使用。 最后给出实现代码,可以直接运行,版本是VS2008 RTM.NET3.5,大家调试的时候注意版本兼容问题。下载地址如下:

/Files/frank_xl/WCFServiceGetClientAddressFrankXuLei.rar。有问题大家可以留言交流,也可以到MSDN中文论坛交流:http://social.microsoft.com/Forums/zh-CN/wcfzhchs/threads

posted @ 2016-08-02 13:37  TonyZhang24  阅读(768)  评论(0编辑  收藏  举报