使用 IDispatchMessageInspector 获取请求的远程地址
我正在尝试关注此博客文章: http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx
我的目标是以某种方式获取远程地址传入请求的地址,但由于某种原因,该地址要么在任何参数中都看不到,要么为空。
这是我正在实现的接口:
public interface IDispatchMessageInspector
{
object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext);
void BeforeSendReply(ref Message reply, object correlationState);
}
我尝试获取远程地址的方法是 AfterReceiveRequest
。我检查了参数 request
和 channel
。另外,channel.RemoteAddress
似乎是它应该在的位置,但由于某种原因该属性为 null。 request
参数也为 null,但我猜这是因为我正在执行 GET 而不是 POST。
下面是我调用来测试这一点的方法的签名。
[OperationContract, WebGet( UriTemplate = "{*path}", ResponseFormat = WebMessageFormat.Json)]
string[] GetList(string path);
I'm trying to follow this blog post: http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx
My aim is to somehow get the remote address of the incoming request, but for some reason the address either is nowhere to be seen in any of the parameters, or is null.
Here's the interface I'm implementing:
public interface IDispatchMessageInspector
{
object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext);
void BeforeSendReply(ref Message reply, object correlationState);
}
The method I'm trying to get the remote address in is AfterReceiveRequest
. I've checked both parameters request
and channel
. Also, it seems that channel.RemoteAddress
is where it should be, but that property is null for some reason. The request
parameter is also null, but I'm guessing that's because I'm doing a GET and not a POST.
Below is the signature of the method I'm invoking to test this out.
[OperationContract, WebGet( UriTemplate = "{*path}", ResponseFormat = WebMessageFormat.Json)]
string[] GetList(string path);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
OperationContext.Current.IncomingMessageHeaders.From
< /a>或
(OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty).Address
或
HttpContext.Current.Request.UserHostAddress
(请注意,这需要设置
)Use
OperationContext.Current.IncomingMessageHeaders.From
OR
(OperationContext.Current. IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty).Address
OR
HttpContext.Current.Request.UserHostAddress
(BEWARE this one requires setting<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
)使用来自
IDispatchMessageInspector
实现的代码:Use this from
IDispatchMessageInspector
implementation:该信息将位于请求标头中,可使用以下方式找到:
The information will be in the request headers, found using: