使用 IDispatchMessageInspector 获取请求的远程地址

发布于 2024-12-15 05:58:04 字数 994 浏览 0 评论 0原文

我正在尝试关注此博客文章: 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。我检查了参数 requestchannel。另外,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-12-22 05:58:04

使用 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"/>)

放血 2024-12-22 05:58:04

使用来自 IDispatchMessageInspector 实现的代码:

var remoteEndpoint = request.Properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
var ipAddress = remoteEndpoint.Address;

Use this from IDispatchMessageInspector implementation:

var remoteEndpoint = request.Properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
var ipAddress = remoteEndpoint.Address;
夏夜暖风 2024-12-22 05:58:04

该信息将位于请求标头中,可使用以下方式找到:

WebHeaderCollection headers = WebOperationContext.Current.IncomingRequest.Headers;

The information will be in the request headers, found using:

WebHeaderCollection headers = WebOperationContext.Current.IncomingRequest.Headers;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文