我与Elmah有问题,我已经覆盖了Mailing方法。当我的应用程序未处于实时模式时,我希望它处理邮件(基本上不发送)。问题是当我这样做时,当我的ErrorHandler(在我的ASP.NET MVC应用程序中)尝试使用Elmah引发异常时,我得到错误:无法访问已处置的对象。对象名:'System.Net.Mail.MailMessage'。所以我需要想办法解决这个问题。代码如下:
通过电子邮件发送方式:
public static void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
if (!GlobalHelper.IsLiveMode)
{
//e.Mail.Dispose();
}
else
{
MailAddressCollection MAC = new MailAddressCollection();
foreach (string a in EmailRecipients.Split(','))
{
MAC.Add(a);
}
string b = "errors@" + GlobalHelper.AppName + ".com";
e.Mail.From = new MailAddress(b);
e.Mail.To.Clear(); // Clears existing mail addresses
e.Mail.To.Add(MAC.ToString());
e.Mail.Subject = GlobalHelper.AppName+ " Error: ("+e.Error.Type.Substring(0,10)+") Deployment Level:"+GlobalHelper.DeploymentMode;
}
错误处理程序方法:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CustomHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
// Bail if we can't do anything; app will crash.
if (context == null)
return;
var ex = context.Exception ?? new Exception("No further information exists.");
// Can show Data on view page /w this code
// var data = new ErrorPresentation
//{
// ErrorMessage = HttpUtility.HtmlEncode(ex.Message),
// TheException = ex,
// ShowMessage = !(filterContext.Exception == null),
// ShowLink = false
//};
//filterContext.Result = View("ErrorPage", data);
context.ExceptionHandled = true;
//Log to Elmah
ErrorSignal.FromCurrentContext().Raise(ex);
//Show Custom view page
context.Result = new ViewResult { ViewName = "Error" };
}
}
堆栈追踪:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Mail.MailMessage'.
at System.Net.Mail.MailMessage.get_AlternateViews()
at System.Net.Mail.MailMessage.SetContent()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Elmah.ErrorMailModule.SendMail(MailMessage mail)
at Elmah.ErrorMailModule.ReportError(Error error)
at Elmah.ErrorMailModule.OnError(Exception e, HttpContext context)
at Elmah.ErrorMailModule.OnErrorSignaled(Object sender, ErrorSignalEventArgs args)
at Elmah.ErrorSignalEventHandler.Invoke(Object sender, ErrorSignalEventArgs args)
at Elmah.ErrorSignal.Raise(Exception e, HttpContext context)
at Elmah.ErrorSignal.Raise(Exception e)
at MusingMonkey.Utilities.Filters.CustomHandleErrorAttribute.OnException(ExceptionContext context) in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Utilities\Filters\ErrorHandler.cs:line 34
at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__3(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<BeginProcessRequest>b__2()
at System.Web.Mvc.SecurityUtil.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a()
at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
答案 0 :(得分:2)
问题在于ELMAH正在尝试使用MailMessage
来发送它,但您已经处理了该对象。对您而言,最好的解决方案是不通过事件执行此操作,而是继承ErrorMailModule
并覆盖它的SendMail
方法,如下所示:
protected override void SendMail(MailMessage mail)
{
if (!GlobalHelper.IsLiveMode)
base.SendMail(mail);
}
然后,您可以继续在Mailing
事件处理程序中完成剩余工作。