代码检查表明我需要处置对象。哪一个?
这是我的职责。我已经将客户端和消息包装到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是这一行:
初始化块
{ From = ... }
在构造对象之后和using
块的内部try/finally< 之前执行/code> 开始。
如果
MailAddress
构造函数(或其参数表达式,或者对From
的赋值(如果它是属性访问器))抛出异常,则MailMessage
将不被处置。更改为:
临时
<>g_initLocal0
变量是 MailMessage 在分配给msg
之前的名称。Your problem is this line:
The initializer block
{ From = ... }
is executed after the object is constructed and before theusing
block's internaltry/finally
begins.If the
MailAddress
constructor (or its argument expressions, or the assignment toFrom
if it is a property accessor) throws an exception, theMailMessage
will not be disposed.Change to:
The temporary
<>g_initLocal0
variable is the name of the MailMessage before it gets assigned tomsg
.