【问题标题】:IIS session timeout occurs randomly - due to error in sending emailIIS 会话超时随机发生 - 由于发送电子邮件时出错
【发布时间】:2022-02-01 05:44:41
【问题描述】:

所以让我解释一下情况。我们的 asp.net mvc 应用程序在 Windows server 2012 r2IIS 8.5.9600.16384 上运行。现在随机用户会话每天丢失4/5 次,尽管超时设置为1000 minutes in process mode。现在检查event viewer,我发现当用户会话丢失时会发生4个错误(每次会话丢失时,以下错误都会重复)。以下是错误...

1.

An unhandled exception occurred and the process was terminated.

Application ID: /LM/W3SVC/7/ROOT

Process ID: 7680

Exception: System.Net.Mail.SmtpException

Message: The operation has timed out.

StackTrace:    at System.Net.Mail.SmtpClient.Send(MailMessage message)
at PDSO.Models.MailClient.<>c__DisplayClass2_1.<SendGMail>b__0(Object o) in 
D:\Development\Projects\YunuscoMRP\YunuscoMRP\Models\Repository\MailClient.cs:line 67
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, 
ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 
callback, Object state, Boolean preserveSyncCtx)
at 
System.Threading.QueueUserWorkItemCallback.System.Threading.
IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

`

Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Mail.SmtpException
   at System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)
   at PDSO.Models.MailClient+<>c__DisplayClass2_1.<SendGMail>b__0(System.Object)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

`

3。 `

Faulting application name: w3wp.exe, version: 8.5.9600.16384, time stamp: 0x52157ba0
Faulting module name: KERNELBASE.dll, version: 6.3.9600.19724, time stamp: 0x5ec50c3e
Exception code: 0xe0434352
Fault offset: 0x000156e8
Faulting process id: 0x1e00
Faulting application start time: 0x01d8165b6e64aacc
Faulting application path: C:\Windows\SysWOW64\inetsrv\w3wp.exe
Faulting module path: C:\Windows\SYSTEM32\KERNELBASE.dll
Report Id: 38d3cbc4-824f-11ec-8116-000c293f4b2c
Faulting package full name: 
Faulting package-relative application ID:

`

4.

  An unhandled Microsoft .NET Framework exception occurred in w3wp.exe [7680]. 
    Just-In-Time debugging this exception failed with the following error: Debugger could not be 
    started because no user is logged on.

    Check the documentation index for 'Just-in-time debugging, errors' for more information.

我有一堂课来处理传出的电子邮件。下面提供代码

public string SendGMail(string fromEmail, string toEmail, string mailSubject, string mailBody, 
string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
    MailMessage mail = new MailMessage();

    mail.From = new MailAddress(fromEmail);
    mail.To.Add(toEmail);
    mail.Subject = mailSubject;
    mail.Body = mailBody;
    mail.IsBodyHtml = true;
    //// mail.Attachments.Add(new Attachment(@attacmmentLocationPath));

    //---------------- Enable the block before publish ----------
    ThreadPool.QueueUserWorkItem(o =>
    {
        using (SmtpClient client = new SmtpClient("smtp.office365.com"))
        {
            client.Port = 587;
            client.Credentials = new System.Net.NetworkCredential(senderName, senderPass);
            client.EnableSsl = true;
            client.Send(mail);
        }
    });
    //-------------------------------------------------------------


    return "Success";
}
catch (Exception ex)
{
    bool error = new ErrorLogRepository().InsertErrorToDatabase("", "MailClient", "SendGmail", ex.ToString());
    return "Error";
}
}

谁能建议我发送电子邮件代码有什么问题或导致session timeout/iis restart/application pool restart 的原因?

【问题讨论】:

    标签: asp.net-mvc email smtp session-state


    【解决方案1】:

    您应该在工作项中添加一个 try-catch 块来处理 SMTP 客户端异常并优雅地记录错误。因为工作项在您的 SendMail 函数之外执行,所以 SendMail 函数中的 try-catch 块不处理异常。

    ThreadPool.QueueUserWorkItem(o =>
    {
        try
        {
            using (SmtpClient client = new SmtpClient("smtp.office365.com"))
            {
                client.Port = 587;
                client.Credentials = new System.Net.NetworkCredential(senderName, senderPass);
                client.EnableSsl = true;
                client.Send(mail);
            }
        }
        catch (Exception ex)
        {
            bool error = new ErrorLogRepository().InsertErrorToDatabase("", "MailClient", "SendGmail", ex.ToString());
        }
    });
    

    【讨论】:

    • 好的,我会尽力给你反馈。
    • 您的解决方案不起作用。还是同样的问题,这与Just-in-time Debugger 错误有关。
    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多