WCF服务如何获得客户端IP地址信息
来源:互联网 发布:linux su认证失败 编辑:程序博客网 时间:2024/05/24 04:30
这里给出服务端获取客户端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); } }
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
- WCF服务如何获得客户端IP地址信息
- WCF热门问题编程示例(1):WCF服务如何获得客户端IP地址信息
- WCF热门问题编程示例(1):WCF服务如何获得客户端地址信息
- 如何获得客户端的ip地址
- 如何获得客户端的ip地址
- 如何获得客户端的ip地址?
- 如何获得客户端的IP地址
- WCF服务怎么获得宿主的ip地址
- jQuery获得客户端/浏览器的IP地址信息
- tornado获得客户端设备信息和ip地址
- 如何在ASP.NET中获得客户端IP地址
- 获得客户端真实ip地址
- 获得客户端实际Ip地址
- 在WCF服务中获取客户端的IP地址和端口号
- 在WCF服务中获取客户端的IP地址和端口号
- Nodejs如何获得客户端IP
- 服务器如何获取客户端用户的(城市)地址[获得真实IP再获得城市]
- php获得客户端ip地址范例
- Ruby Get Started
- 【Bugly干货分享】iOS内存管理:从MRC到ARC实践
- Onekey Ghost找不到硬盘分区怎么办
- 给定日期-判断是星期几
- Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
- WCF服务如何获得客户端IP地址信息
- 数据库中char varchar nchar nvarchar的区别
- Struts---拦截器
- 微商城开发(2)
- 输入与输出
- LIME:一种解释机器学习模型的方法
- git基本操作
- 欧几里得辗转相除求最大公约数最小公倍数
- final和static final之间的区别