代码检查表明我需要处置对象。哪一个?

发布于 2024-11-17 05:30:37 字数 2086 浏览 3 评论 0原文

这是我的职责。我已经将客户端和消息包装到 using 子句中,但在运行代码检查时仍然出现错误。错误指向第一个使用行:

public static void Send(MailItem mail)
        {
            var sender = Membership.GetUser(mail.CreatedBy);
            if (sender == null)
            {
                return;
            }

            using (var msg = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]) })
            {
                foreach (var recipient in mail.MailRecipients)
                {
                    var recipientX = Membership.GetUser(recipient.UserKey);
                    if (recipientX == null)
                    {
                        continue;
                    }

                    msg.To.Add(new MailAddress(recipientX.Email, recipientX.UserName));
                }

                msg.Subject = "[From: " + sender.UserName + "]" + mail.Subject;
                msg.Body = mail.Body;

                if (HttpContext.Current != null)
                {
                    msg.Body += Environment.NewLine + Environment.NewLine + "To reply via Web click link below:" +
                                Environment.NewLine;
                    msg.Body += ConfigurationManager.AppSettings["MailPagePath"] + "?AID=" +
                                ContextManager.CurrentAccount.AccountId + "&RUN=" + sender.UserName;
                }

                try
                {
                    using (var emailClient = new SmtpClient())
                    {
                        emailClient.Send(msg);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex);
                }
            }
        }

这是我收到的警告:

警告 1 CA2000: Microsoft.Reliability:在方法中 'Email.Send(MailItem)',对象 '<>g_initLocal0' 未释放 沿着所有异常路径。称呼 对象上的 System.IDisposable.Dispose '<>g_initLocal0' 在所有之前 对它的引用是出自 范围。 C:\CodeWorkspace\Code\Utility\Email.cs 41

This is my function. I already wrapped both client and message into using clause and still get error when run code inspection. Error points to first using line:

public static void Send(MailItem mail)
        {
            var sender = Membership.GetUser(mail.CreatedBy);
            if (sender == null)
            {
                return;
            }

            using (var msg = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]) })
            {
                foreach (var recipient in mail.MailRecipients)
                {
                    var recipientX = Membership.GetUser(recipient.UserKey);
                    if (recipientX == null)
                    {
                        continue;
                    }

                    msg.To.Add(new MailAddress(recipientX.Email, recipientX.UserName));
                }

                msg.Subject = "[From: " + sender.UserName + "]" + mail.Subject;
                msg.Body = mail.Body;

                if (HttpContext.Current != null)
                {
                    msg.Body += Environment.NewLine + Environment.NewLine + "To reply via Web click link below:" +
                                Environment.NewLine;
                    msg.Body += ConfigurationManager.AppSettings["MailPagePath"] + "?AID=" +
                                ContextManager.CurrentAccount.AccountId + "&RUN=" + sender.UserName;
                }

                try
                {
                    using (var emailClient = new SmtpClient())
                    {
                        emailClient.Send(msg);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex);
                }
            }
        }

This is warning I get:

Warning 1 CA2000 :
Microsoft.Reliability : In method
'Email.Send(MailItem)', object
'<>g_initLocal0' is not disposed
along all exception paths. Call
System.IDisposable.Dispose on object
'<>g
_initLocal0' before all
references to it are out of
scope. C:\CodeWorkspace\Code\Utility\Email.cs 41

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶ ̄淡然 2024-11-24 05:30:37

您的问题是这一行:

using (var msg = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]) }) 

初始化块 { From = ... } 在构造对象之后和 using 块的内部 try/finally< 之前执行/code> 开始。

如果 MailAddress 构造函数(或其参数表达式,或者对 From 的赋值(如果它是属性访问器))抛出异常,则 MailMessage 将不被处置。

更改为:

using (var msg = new MailMessage()) 
{
    msg.From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]);
    ...
}

临时 <>g_initLocal0 变量是 MailMessage 在分配给 msg 之前的名称。

Your problem is this line:

using (var msg = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]) }) 

The initializer block { From = ... } is executed after the object is constructed and before the using block's internal try/finally begins.

If the MailAddress constructor (or its argument expressions, or the assignment to From if it is a property accessor) throws an exception, the MailMessage will not be disposed.

Change to:

using (var msg = new MailMessage()) 
{
    msg.From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]);
    ...
}

The temporary <>g_initLocal0 variable is the name of the MailMessage before it gets assigned to msg.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文