简体   繁体   English

从后台线程调用System.Web.UI.Control.RenderControl失败

[英]System.Web.UI.Control.RenderControl fails when called from background thread

I have a requirement to render a control in a background thread and send the output in mail. 我需要在后台线程中呈现控件,并通过邮件发送输出。 So I have this piece of code running in a background thread: 所以我将这段代码在后台线程中运行:

System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(sendmail), new Object());
public void sendmail(object o)
{
    UserControl cntrl = new Page().LoadControl("~/mycontrol.ascx") as UserControl;
    StringWriter stringwriter = new StringWriter();
    Html32TextWriter htmlwriter = new Html32TextWriter(stringwriter);
    cntrl.RenderControl(htmlwriter);
    SendEmailUsingNetMail(stringwriter);
}

At runtime I am getting this exception

System.NullReferenceException: Object reference NOT SET TO an instance of an object.
AT System.Web.UI.HtmlControls.HtmlForm.GetActionAttribute()
AT System.Web.UI.HtmlControls.HtmlForm.RenderAttributes(HtmlTextWriter writer)
AT System.Web.UI.HtmlControls.HtmlControl.RenderBeginTag(HtmlTextWriter writer)
AT System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
AT System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
AT System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
AT System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
AT System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
AT System.Web.UI.Control.Render(HtmlTextWriter writer)
AT System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
AT RedBus.RBMail.sendmail(object o)

The same code when run from asp.net request thread works perfectly fine. 从asp.net请求线程运行时,相同的代码运行良好。 Is it possible to get this thing to work in background thread itself? 是否有可能让此东西在后台线程本身中工作? If yes how? 如果是,怎么办?

Thanks, Pradhan 谢谢,Pradhan

Add a Form to your Page: 将表单添加到页面:

System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(sendmail), new Object());
public void sendmail(object o)
{

    System.Web.UI.Page() pageHolder = new System.Web.UI.Page() 
              {AppRelativeTemplateSourceDirectory = HttpRuntime.AppDomainAppVirtualPath };
    System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
    UserControl cntrl = new Page().LoadControl("~/mycontrol.ascx") as UserControl;
    form.Controls.Add(cntrl );
    pageHolder.Controls.Add(form);

    StringWriter stringwriter = new StringWriter();

    HttpContext.Current.Server.Execute(pageHolder, stringwriter, false);
    SendEmailUsingNetMail(stringwriter);
}

Also, I wrote an OSS project to do this called ucajax , the lib was intended to be consumed with web services, but there is no reason you can't call the methods directly to render controls. 另外,我编写了一个名为ucajax的OSS项目来完成此任务 ,该库旨在与Web服务一起使用,但是没有理由您不能直接调用方法来呈现控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM