C# 使用IDispatchMessageInspector获取请求';远程地址
我正试图关注这篇博文: 我的目标是以某种方式获取传入请求的远程地址,但由于某种原因,该地址要么在任何参数中都看不到,要么为null 下面是我正在实现的接口:C# 使用IDispatchMessageInspector获取请求';远程地址,c#,wcf,idispatchmessageinspector,C#,Wcf,Idispatchmessageinspector,我正试图关注这篇博文: 我的目标是以某种方式获取传入请求的远程地址,但由于某种原因,该地址要么在任何参数中都看不到,要么为null 下面是我正在实现的接口: public interface IDispatchMessageInspector { object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext); void BeforeS
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
参数也是空的,但我猜这是因为我在做GET而不是POST
下面是我用来测试这个的方法的签名
[OperationContract, WebGet( UriTemplate = "{*path}", ResponseFormat = WebMessageFormat.Json)]
string[] GetList(string path);
使用
或
(OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name]作为RemoteEndpointMessageProperty)。地址
或
HttpContext.Current.Request.UserHostAddress
(注意此项需要设置)信息将在请求头中,使用以下方法找到:
WebHeaderCollection headers = WebOperationContext.Current.IncomingRequest.Headers;
从
IDispatchMessageInspector
实现中使用此选项:
var remoteEndpoint = request.Properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
var ipAddress = remoteEndpoint.Address;
我在某个时候也尝试过,但是头球没有我想要的。Yahia的解决方案似乎很有效。