新聞中心
WCF在實際應(yīng)用中,可以發(fā)現(xiàn)它能夠幫助我們實現(xiàn)許多功能需求。在這里我們就為大家詳細(xì)講解一下有關(guān)WCF獲取客戶端IP的實現(xiàn)方法。#t#

成都創(chuàng)新互聯(lián)長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為嵐皋企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、成都做網(wǎng)站,嵐皋網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
在公司的一個項目里面,使用WCF做通訊,里面需要取得使用WCF獲取客戶端IP,在服務(wù)器上做進(jìn)一步的處理,但是讓人很失望的是WCF 3.0 里面并不能支持這個功能。還好,微軟在3.5的新版WCF中提供了這個方法。
不說廢話,直接看如何實現(xiàn)。
簡單定義一個WCF獲取客戶端IP的服務(wù):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- namespace ClientInfoSample
- {
- [ServiceContract]
- public interface IService
- {
- [OperationContract]
- string GetData(string value);
- }
- }
在建立通道之后按照可以實現(xiàn)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" />
以上就是有關(guān)WCF獲取客戶端IP的實現(xiàn)方法。
分享文章:技巧掌握之WCF獲取客戶端IP
鏈接URL:http://fisionsoft.com.cn/article/dpssseh.html


咨詢
建站咨詢
