Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用IDispatchMessageInspector获取请求';远程地址_C#_Wcf_Idispatchmessageinspector - Fatal编程技术网

C# 使用IDispatchMessageInspector获取请求';远程地址

C# 使用IDispatchMessageInspector获取请求';远程地址,c#,wcf,idispatchmessageinspector,C#,Wcf,Idispatchmessageinspector,我正试图关注这篇博文: 我的目标是以某种方式获取传入请求的远程地址,但由于某种原因,该地址要么在任何参数中都看不到,要么为null 下面是我正在实现的接口: public interface IDispatchMessageInspector { object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext); void BeforeS

我正试图关注这篇博文:

我的目标是以某种方式获取传入请求的远程地址,但由于某种原因,该地址要么在任何参数中都看不到,要么为null

下面是我正在实现的接口:

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的解决方案似乎很有效。