卡修
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
posts - 35, comments - 51, trackbacks - 3
<
2008年6月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我参与的团队
重庆.NET俱乐部(0/309)
随笔分类
WPF学习日记(1)
非技术相关(2)
随笔档案
2008年8月 (1)
2008年6月 (5)
2008年5月 (3)
2008年1月 (3)
2007年12月 (1)
2007年11月 (2)
2007年9月 (1)
2007年8月 (6)
2007年7月 (4)
2007年6月 (3)
2007年5月 (2)
2007年4月 (2)
2007年3月 (2)
好友的blogs
键键
积分与排名
积分 - 9749
排名 - 3374
最新评论
1. re: 使用深度V8.1 系统后打开部分文件夹缓慢
换9.0了
--Boy Xie
2. re: 使用深度V8.1 系统后打开部分文件夹缓慢
我的也是
--刺吾伊
阅读排行榜
1. WCF 取得客户端IP(2136)
2. 完全卸载VS2005(1146)
3. C#中的集合类(662)
4. 由于使用SVN 造成的项目无法使用的问题,报错“项目刷新失败,无法从服务器中检索文件夹信息 ”(430)
5. xp_cmdshell 自己收藏(360)
评论排行榜
1. msil指令 收藏(9)
2. 在datatable中循环删除多条(8)
3. WCF 取得客户端IP(7)
4. 关于王石捐款(4)
5. 改死的迅雷(4)
WCF 取得客户端IP
在公司的一个项目里面,使用WCF做通讯,里面需要取得使用WCF做客户端的IP,在服务器上做进一步的处理,但是让人很失望的是WCF 3.0 里面并不能支持这个功能。
还好,微软在3.5的新版WCF中提供了这个方法。
不说废话,直接看如何实现。
简单定义一个服务:
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);
}
}
在建立通道之后按照可以取得:
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 [RemoteEndpointMessageProperty.Name]
as
RemoteEndpointMessageProperty;
return
string
.Format(
"
Hello {0}! Your IP address is {1} and your port is {2}
"
, value, endpointProperty.Address, endpointProperty.Port);
}
}
}
config:
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
system.web
>
<
compilation
debug
="true"
/>
</
system.web
>
<
system.serviceModel
>
<
services
>
<
service
name
="ClientInfoSample.MyService"
behaviorConfiguration
="ClientInfoSample.MyServiceBehavior"
>
<
host
>
<
baseAddresses
>
<
add
baseAddress
= "http://localhost:8731/Design_Time_Addresses/ClientInfoSample/MyService/"
/>
</
baseAddresses
>
</
host
>
<
endpoint
address
=""
binding
="wsHttpBinding"
contract
="ClientInfoSample.IService"
>
<
identity
>
<
dns
value
="localhost"
/>
</
identity
>
</
endpoint
>
<
endpoint
address
="mex"
binding
="mexHttpBinding"
contract
="IMetadataExchange"
/>
</
service
>
</
services
>
<
behaviors
>
<
serviceBehaviors
>
<
behavior
name
="ClientInfoSample.MyServiceBehavior"
>
<
serviceMetadata
httpGetEnabled
="True"
/>
<
serviceDebug
includeExceptionDetailInFaults
="False"
/>
</
behavior
>
</
serviceBehaviors
>
</
behaviors
>
</
system.serviceModel
>
</
configuration
>
例子内容缘自:
http://nayyeri.net/blog/detect-client-ip-in-wcf-3-5/
posted on 2008-06-03 14:10
Boy Xie
阅读(2136)
评论(7)
编辑
收藏
Feedback
#1楼
2008-06-03 15:17 |
jchdong
沙发
回复
引用
查看
#2楼
2008-06-03 15:26 |
姜敏
正在学习中.
回复
引用
查看
#3楼
2008-06-03 15:50 |
tansm [未注册用户]
获取客户端的IP反向操作并不被推荐,当客户端位于互联网的另一端的某个内网下时,他的IP毫无疑义。
所以不建议楼主这么干。
回复
引用
#4楼
2008-06-03 16:16 |
小Q
hehe 学习了.
回复
引用
查看
#5楼
2008-06-03 20:30 |
Train-8 [未注册用户]
不好
回复
引用
#6楼
2008-06-03 23:28 |
江南白衣
不要依赖于这个IP做安全认证,这是可以伪造的,一定要加上其他措施
回复
引用
查看
#7楼
2008-06-04 09:49 |
求知无傲
有没有更好的方式,辨别真伪呢?
回复
引用
查看
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
博客园首页
新闻频道
社区
小组
博问
网摘
闪存
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
成果网帮您增加网站收入
相关文章:
C#下如何实现服务器+客户端的聊天程序
把Membership,Role包装为WCF服务
[原创]我的WCF之旅(1):创建一个简单的WCF程序
【转】WCF 取得客户端IP
不要在WCF service中使用TypedDataset或DataTable作为数据传输载体
取得本机的IP地址函数
相关链接:
最新IT新闻:
Google 10周岁生日
祝Google 10周岁生日快乐
Google十年市值达1500亿美元 创造奇迹
GMail:回过头来支持IE6
Google十年大变样