WCF在实际应用中,可以发现它能够帮助我们实现许多功能需求。在这里我们就为大家详细讲解一下有关WCF获取客户端IP的实现方法。#t#

成都创新互联长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为岚皋企业提供专业的网站设计制作、成都做网站,岚皋网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
在公司的一个项目里面,使用WCF做通讯,里面需要取得使用WCF获取客户端IP,在服务器上做进一步的处理,但是让人很失望的是WCF 3.0 里面并不能支持这个功能。还好,微软在3.5的新版WCF中提供了这个方法。
 不说废话,直接看如何实现。
 简单定义一个WCF获取客户端IP的服务:
在建立通道之后按照可以实现WCF获取客户端IP:
- 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 [RemoteEndpoint
 
MessageProperty.Name]- as RemoteEndpointMessageProperty;
 - return string.Format("Hello {0}!
 
Your IP address is {1} and your
port is {2}", value, endpointProperty.
Address, endpointProperty.Port);- }
 - }
 - }
 - config:
 behaviorConfiguration="ClientInfoSample. 
MyServiceBehavior">Design_Time_Addresses/ClientInfoSample/MyService/" /> contract="ClientInfoSample.IService"> "mexHttpBinding" contract="IMetadataExchange"/> .MyServiceBehavior"> DetailInFaults="False" /> 
以上就是有关WCF获取客户端IP的实现方法。
                文章标题:技巧掌握之WCF获取客户端IP
                
                文章来源:http://www.csdahua.cn/qtweb/news33/266583.html
            
成都网站优化推广公司_创新互联,为您提供网站营销、App设计、Google、网站导航、品牌网站建设、微信小程序
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网