顯示具有 TimeOut 標籤的文章。 顯示所有文章
顯示具有 TimeOut 標籤的文章。 顯示所有文章

2025年7月10日 星期四

[研究]ASP.NET WebForm 執行過久跳轉 Exception 畫面之處理

[研究]ASP.NET WebForm 執行過久跳轉 Exception 畫面之處理

2025-07-10、2025-12-15

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20

********************************************************************************

ASP.NET WebForm 同一個 .aspx 網頁,因為計算的東西複雜,在效能好的機器上正常運作,在效能較差虛擬機上,執行一陣子自動跳轉到 Exception 畫面。

ChatGPT 回答的太拉雜囉嗦,整理數次詢問的結果。

全域設定,可改 Web.Config,預設 90秒, 最大值(上限):Int32.MaxValue 秒,2,147,483,647 秒 ≒ 68 年

<configuration>
  <system.web>
    <httpRuntime executionTimeout="300" />
  </system.web>
</configuration>

只改單一網頁,注意,Server.ScriptTimeout  設定超過 executionTimeout 是無用的。

protected void Button1_Click(object sender, EventArgs e)
{
    Server.ScriptTimeout = 300;

    try
    {
        // 假設是複雜計算
        DoHeavyCalculation();
        lblResult.Text = "計算完成。";
    }
    catch (TimeoutException ex)
    {
        lblResult.Text = "計算逾時,請簡化條件或稍後再試。";
        // 可以記錄詳細例外訊息
        LogException(ex);
    }
    catch (Exception ex)
    {
        lblResult.Text = "發生錯誤:" + ex.Message;
        LogException(ex);
    }
}


另外考慮是否因為去 DB 中抓資料時間超過,CommandTimeout 預設 30 秒,下面可以改為 180 秒。

SqlCommand command = new SqlCommand("your SQL here", connection);
command.CommandTimeout = 180; // 改為 3 分鐘
ㄌ

捕捉例外

try
{
    using (SqlConnection connection = new SqlConnection("your connection string"))
    {
        connection.Open();
        SqlCommand command = new SqlCommand("your SQL here", connection);
        command.CommandTimeout = 30;

        using (SqlDataReader reader = command.ExecuteReader())
        {
            // 處理資料
        }
    }
}
catch (SqlException ex)
{
    if (ex.Number == -2) // timeout
    {
        // 處理查詢逾時
        Response.Write("資料庫查詢逾時,請稍後再試。");
    }
    else
    {
        // 處理其他 SQL 錯誤
        Response.Write("資料庫錯誤:" + ex.Message);
    }
}
catch (Exception ex)
{
    // 處理非 SQL 的其他例外
    Response.Write("系統發生錯誤:" + ex.Message);
}

另外也可能是記憶體不足,或 IIS Request Timeout 設定過短 (可在 IIS 裡 Application Pool → Limits → Request Timeout 設定 ),或 Proxy、CDN、、、等因素。

**********

使用 GrdView1 和 SqlDataSource1 發生 "等候操作已逾時。增加 DB 連線時間

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            // System.ComponentModel.Win32Exception: 等候操作已逾時。
            // System.Data.SqlClient.SqlException (0x80131904): 執行逾時到期。
            e.Command.CommandTimeout = 60;
        }

**********

敝人最後是靠改成下面解決問題。

<httpRuntime executionTimeout="300" />

command.CommandTimeout = 300; // 改為 5 分鐘

(完)

相關

2022年4月25日 星期一

[研究][ASP.NET]Session TimeOut是固定時間 or 閒置時間測試

[研究][ASP.NET]Session TimeOut是固定時間 or 閒置時間測試

2022-04-25

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

參考這篇

[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(一)aspnet_regsql.exe
http://shaurong.blogspot.com/2022/04/aspnet-sql-server-session-state.html

資料庫會新增了2個資料表
dbo.ASPStateTempApplications
dbo.ASPStateTempSessions 

其中 dbo.ASPStateTempSessions 的 [Expires] 欄位可看過期時間。

登入後,先檢視一次時間;

放若干秒或分鐘後,隨便動一下滑鼠,再重新 SELECT 該值 ( 要找相同 SessionId 的,在自己開發電腦上測試,只有一個使用者登入,最好認),發現時間變更了,所以是 Session Time Out 是 閒置時間。

注意,[Created], [Expires], [LockDate], [LockDateLocal] 都是 UTC 時間,要加上 8 小時才是台灣時間。

(完)


2022年3月30日 星期三

[研究][ASP.NET]顯示IIS Session Time Out預設值設定時間

[研究][ASP.NET]顯示IIS Session Time Out預設值設定時間

2022-03-20

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

目前為了安全性,各 Web Server 預設都有 Session TimeOut 機制,並且有各自的預設時間,依據Web Server 種類、版本可能都有所不同。

ASP.NET WebForm 網站 Web.config 可以設定 Session TimeOut 值 (不設也會有預設值)。

Session Time Out 在設定後是一個定值,Remaining Session Time 剩餘會話時間是指 Session 在 Time Out 前剩下的時間,是動態值。

Web.Config,例如設定為20分鐘。

<configuration>
   <system.web>
      <sessionState timeout="20"></sessionState>
   </system.web>
</configuration>

如果不知道,可以用程式測試看看

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server"></asp:Label><br />
            <asp:Label ID="Label2" runat="server"></asp:Label><br />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Configuration;
using System.Web.Configuration;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Configuration conf = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
            SessionStateSection section = (SessionStateSection)conf.GetSection("system.web/sessionState");
            int timeout = (int)section.Timeout.TotalMinutes;
            Label1.Text = timeout.ToString();

            System.Int64 timeout2 = System.Web.HttpContext.Current.Session.Timeout; // The time-out period, in minutes
            Label2.Text = timeout2.ToString();
        }
    }
}

測試出來 Windows Server 2019 + IIS Web Server + ASP.NET WebForm Web Application 網站的 Session TimeOut 預設值為 20 分鐘。

(完)

相關

sessionState 項目 (ASP.NET 設定結構描述) | Microsoft Docs
https://docs.microsoft.com/zh-tw/previous-versions/dotnet/netframework-3.0/h6bb9cz9(v=vs.85)

對談 (電腦科學) - 維基百科,自由的百科全書https://zh.wikipedia.org/wiki/%E4%BC%9A%E8%AF%9D_(%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6)

HttpSessionState.Timeout 屬性 (System.Web.SessionState) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.web.sessionstate.httpsessionstate.timeout?view=netframework-4.8