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

主要使用是.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

WCF service 获取 client 端的 IP 和 port (转)的更多相关文章

  1. 用C#基于WCF创建TCP的Service供Client端调用

    本文将详细讲解用C#基于WCF创建TCP的Service供Client端调用的详细过程 1):首先创建一个Windows Service的工程 2):生成的代码工程结构如下所示 3):我们将Servi ...

  2. server端获得到client端的IP地址的格式

    使用telnet,ping或其他client连接server端时,server端获得的client端的ip地址取决于client端使用的时ipv4还是ipv6地址. 例: client IPv4地址: ...

  3. python - socket - client端指定ip和端口

    问题描述: 在设备中有3个NI, ip分别为192.168.1.5/6/7.其中本端192.168.1.6同对端192.168.1.10建立了一个tunnel. 我希望测试tunnel连通性, 对端起 ...

  4. WCF客户端获取服务端异常[自定义异常]

    引言 经过不断的摸索,询问/调试,终于学会了关于WCF客户端与服务端之间异常的处理机制,在此来记录自己的成果,用于记录与分享给需要的伙伴们. 首先感谢[.NET技术群]里群主[轩]的大力帮助,如有需要 ...

  5. [Python Study Notes]CS架构远程访问获取信息--Client端v2.0

    更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 效果图: '''''''''''''''''''''' ...

  6. [Python Study Notes]CS架构远程访问获取信息--Client端v1.0

    更新内容: 1.添加entry栏默认ip和port口 2.修正退出功能 3.添加退出自动关闭窗口功能 4.优化cpu显示为固定保留两位小数 '''''''''''''''''''''''''''''' ...

  7. [Python Study Notes]CS架构远程访问获取信息--Client端

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  8. 通过网络socket获取对方 ip 和port

    int getpeername(int s, struct sockaddr *name, socklen_t *namelen);描述获取socket的对方地址struct sockaddr_in ...

  9. If WCF Service side and Client side config is different?!

    from stackoverflow http://stackoverflow.com/questions/4879310/when-setting-up-a-wcf-client-and-serve ...

随机推荐

  1. 十分钟轻松让你认识Entity Framework 7

    EF7不是在EF6上更新,而是完全重写了.它完全支持.NET Core runtime,也就是说以后你可以在Mac或者是Linux机器上使用EF了. https://github.com/nichol ...

  2. 解决win8 下 eclipse 中文字体太小的问题

    一.把字体设置为Courier New  操作步骤:打开Elcipse,点击菜单栏上的“Windows”——点击“Preferences”——点击“Genneral”——点击“Appearance”— ...

  3. 关于今天很热的--FizzBuzzWhizz

    今天早上到现在看到了3篇关于FizzBuzzWhizz的问题,第一篇是@程序媛想事儿(Alexia)[最难面试的IT公司之ThoughtWorks代码挑战--FizzBuzzWhizz游戏]其实题目不 ...

  4. .eww

    http://sourceforge.net/projects/ezwinports/files/ 下载libxml2的文件. 再下载的bin里复制libiconv-2.dll和libxml2-2.d ...

  5. jquery 实现类似于弹幕效果

    在别人网站中看到一个类似于弹幕的效果,闲来无事用jquery写了个备用~~ <!DOCTYPE html> <meta charset="utf-8"> & ...

  6. Git的使用

    1.从Git服务器上获取项目   2.提交我的修改项目 3.发布新版本 4.修复bug

  7. Leetcode 254. Factor Combinations

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  8. ubuntu14.04 下安装有道词典

    安装步骤 1.ubuntu14.04.1版本下是不能直接安装有道词典的,首先需要把14.04.版升级为14.04.2版. 在终端窗口中输入以下命令: sudo apt-get update sudo ...

  9. C++ 11 中的右值引用

    C++ 11 中的右值引用 右值引用的功能 首先,我并不介绍什么是右值引用,而是以一个例子里来介绍一下右值引用的功能: #include <iostream>    #include &l ...

  10. windows7下修改hosts文件无效解决办法(转)

    通常会为了开发方便.或者屏蔽掉一些恶意网站,我们会在hosts(c:\windows\system32\drivers\etc\hosts)文件中进行相应的域名指向,例: