确定服务器。转移(请求)被执行死刑

时间:2022-09-15 16:58:33

I was debugging two things and discovered that when an action executed:

我调试了两件事,发现当一个动作执行时:

Server.TransferRequest(url);
return new EmptyResult();

that my Application_Error would fire and catch the exception "The SessionStateTempDataProvider class requires session state to be enabled."

我的Application_Error将触发并捕获异常“SessionStateTempDataProvider类需要启用会话状态。”

(I do have session state enabled, specifically StateServer, and for the sake of discussion let's imagine I do not need to commit changes from this request back to the session)

(我确实启用了会话状态,特别是政治家,为了便于讨论,让我们假设我不需要将此请求提交回会话)

I'd like to control how this scenario gets logged. Obviously I can detect the exception type and message text, but if I wanted to not do special logging when the Request was .TransferRequest'd (or .Transfer'd), how can I do this?

我想控制这个场景是如何被记录的。显然,我可以检测异常类型和消息文本,但是如果我不想在请求为.TransferRequest d(或.Transfer d)时进行特殊的日志记录,我怎么做呢?

I have already examined Response.IsRequestBeingRedirected - it was false.

我已经检查过了。isrequestbeing重定向——它是错误的。

1 个解决方案

#1


1  

Each Server.Transfer will add an unsafe request completion call to the stack, therefore, if there are more than 1 of this, you may think that the request is internally redirected by one of the Server.Transfer or Server.TransferRequest methods

每个服务器。传输将向堆栈添加一个不安全的请求完成调用,因此,如果超过1个,您可能认为请求在内部由服务器重定向。转移或服务器。TransferRequest方法

Here is a boolean function which you can add to your global.asax file, which will return true if it finds more than one instances of request completion frames in the current stack trace.

这是一个布尔函数,可以添加到全局变量中。asax文件,如果在当前堆栈跟踪中发现请求完成帧的多个实例,它将返回true。

The new StackTrace() is an expensive operation, but, because this is executing in an Exceptional context here, it's overhead "may" be ignored.

新的StackTrace()是一个昂贵的操作,但是,由于它是在一个异常的上下文中执行的,所以它的开销“可能”会被忽略。

private bool IsRequestTransferred()
{
    System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
    int requestCompletionCount = 0;
    foreach (var stackFrame in stackTrace.GetFrames())
    {
        System.Reflection.MethodBase methodBase = stackFrame.GetMethod();
        if (methodBase.DeclaringType.Name == "UnsafeIISMethods" && methodBase.Name == "MgdIndicateCompletion")
        {
            if (++requestCompletionCount == 2)
            {
                return true;
            }
        }
    }
    return false;
}

void Application_Error(object sender, EventArgs e)
{
    bool isRequestTransferred = IsRequestTransferred();
}

#1


1  

Each Server.Transfer will add an unsafe request completion call to the stack, therefore, if there are more than 1 of this, you may think that the request is internally redirected by one of the Server.Transfer or Server.TransferRequest methods

每个服务器。传输将向堆栈添加一个不安全的请求完成调用,因此,如果超过1个,您可能认为请求在内部由服务器重定向。转移或服务器。TransferRequest方法

Here is a boolean function which you can add to your global.asax file, which will return true if it finds more than one instances of request completion frames in the current stack trace.

这是一个布尔函数,可以添加到全局变量中。asax文件,如果在当前堆栈跟踪中发现请求完成帧的多个实例,它将返回true。

The new StackTrace() is an expensive operation, but, because this is executing in an Exceptional context here, it's overhead "may" be ignored.

新的StackTrace()是一个昂贵的操作,但是,由于它是在一个异常的上下文中执行的,所以它的开销“可能”会被忽略。

private bool IsRequestTransferred()
{
    System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
    int requestCompletionCount = 0;
    foreach (var stackFrame in stackTrace.GetFrames())
    {
        System.Reflection.MethodBase methodBase = stackFrame.GetMethod();
        if (methodBase.DeclaringType.Name == "UnsafeIISMethods" && methodBase.Name == "MgdIndicateCompletion")
        {
            if (++requestCompletionCount == 2)
            {
                return true;
            }
        }
    }
    return false;
}

void Application_Error(object sender, EventArgs e)
{
    bool isRequestTransferred = IsRequestTransferred();
}