Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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# at UriTemplate的可选参数导致AfterReceiveRequest-WCF-IDispatchMessageInspector的两次发票_C#_Wcf_Http Headers_Url Redirection_Uritemplate - Fatal编程技术网

C# at UriTemplate的可选参数导致AfterReceiveRequest-WCF-IDispatchMessageInspector的两次发票

C# at UriTemplate的可选参数导致AfterReceiveRequest-WCF-IDispatchMessageInspector的两次发票,c#,wcf,http-headers,url-redirection,uritemplate,C#,Wcf,Http Headers,Url Redirection,Uritemplate,我只是深入一点,发现了新的细节 通常不是“UriTemplate”导致第二次调用“AfterReceiveRequest”,而是其中的可选参数 如果我用 http://myserver/result/val1 AfterReceiveRequest将被调用两次 如果我传递所有可能的参数,比如 http://myserver/result/val1/val2/val3 不会有无用的调用。这种行为是故意的吗 UriTemplate = "result/{para1=null}/{para2=null

我只是深入一点,发现了新的细节

通常不是“UriTemplate”导致第二次调用“AfterReceiveRequest”,而是其中的可选参数

如果我用

http://myserver/result/val1

AfterReceiveRequest将被调用两次

如果我传递所有可能的参数,比如

http://myserver/result/val1/val2/val3

不会有无用的调用。这种行为是故意的吗

UriTemplate = "result/{para1=null}/{para2=null}/{para3=null}"

---以下是最初的帖子,仅供参考---

在实现WCF REST服务系统时,我遇到了http头的一个问题

在我的
ServiceContract
中,有一个方法在
WebGet
属性处具有
UriTemplate
定义,因此可以通过

http://server/resource/val1/val2 ...
而不是

http://server/resource?para1=val1&para2=val2 ...
(出于兼容性原因,我需要这个。)

另外,在http headers集合中有一个重要的值,我需要阅读。因此,我实现了
IDispatchMessageInspector
,并将此检查器添加到
EndpointDispatchers
MessageInspectors
集合中。此时,WCF将调用
AfterReceiveRequest
,我可以访问
WebOperationContext.Current.IncomingRequest.Headers
来读取所需的值

问题:WCF通过生成到目标方法的第二个请求来解决
UriTemplate
-映射,但不会将原始调用的头条目传递到生成的第二个调用。因此,
AfterReceiveRequest
(当然也包括
BeforeSendReply
)将被调用两次,但是来自真实客户端调用的头值仅包括在第一次调用中

此外,我还没有找到关联第一个和第二个
AfterReceiveRequest
调用的方法,以实现将头值从第一个调用传递到第二个调用的“特殊方式”

有没有办法告诉WCF将头路由到
UriTemplate
-重定向的第二次呼叫

下面是一些代码片段,以明确说明:

[ServiceContract]
public interface IMyService
{
    [WebGet(UriTemplate = "result/{para1=null}/{para2=null}/{para3=null}")]
    bool MyMethod(string para1, string para2, string para3);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[MyServiceInspectorBeavior]
public class MyService : IMyService
{
    public bool MyMethod(string para1, string para2, string para3)
    {
        return DoTheWork();
    }
    //...
}

public class MyServiceInspectorBeavior : Attribute, IServiceBehavior
{

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (EndpointDispatcher epDispatcher in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>().SelectMany(cDispatcher => cDispatcher.Endpoints))
        {
            epDispatcher.DispatchRuntime.MessageInspectors.Add(new MyInspector());
        }
    }

    //...
}

public class MyInspector : IDispatchMessageInspector
{
    //this is invoked twice for each client request, 
    //but only at the first call the header is present...
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        WebHeaderCollection webOpContext = 
                      WebOperationContext.Current.IncomingRequest.Headers;
        string xForwardedIp = webOpContext["X-FORWARDED-IP"];
        WriteLog(xForwardedIp);

        return OperationContext.Current.IncomingMessageProperties["ActivityId"];
    }
    //...
}
[服务合同]
公共接口IMyService
{
[WebGet(UriTemplate=“result/{para1=null}/{para2=null}/{para3=null}”)]
bool-MyMethod(字符串para1、字符串para2、字符串para3);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
[MyServiceInspectorBeavior]
公共类MyService:IMyService
{
public bool MyMethod(字符串para1、字符串para2、字符串para3)
{
返回DoTheWork();
}
//...
}
公共类MyServiceInspectorBeavior:属性,IServiceBehavior
{
公共无效ApplyDispatchBehavior(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase)
{
foreach(serviceHostBase.ChannelDispatchers.OfType()中的EndpointDispatcher epDispatcher.SelectMany(CDDispatcher=>CDDispatcher.Endpoints))
{
epDispatcher.dispatcheruntime.MessageInspectors.Add(新的MyInspector());
}
}
//...
}
公共类MyInspector:IDispatchMessageInspector
{
//对于每个客户端请求,会调用两次,
//但只有在第一次调用时,标题才出现。。。
接收请求后的公共对象(ref消息请求、IClientChannel通道、InstanceContext InstanceContext)
{
WebHeaderCollection webOpContext=
WebOperationContext.Current.IncomingRequest.Header;
字符串xForwardedIp=webOpContext[“X-FORWARDED-IP”];
WriteLog(xForwardedIp);
返回OperationContext.Current.IncomingMessageProperties[“ActivityId”];
}
//...
}

不知道如何解决这个问题?采取这种做法是否完全是错误的?