MVC3传递ControllerContext线程? [英] MVC3 Passing ControllerContext to thread?

查看:88
本文介绍了MVC3传递ControllerContext线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

。这工作得很好,但速度很慢(5-7〜秒,只有1用户),所以我一直在试图把它变成一个独立的线程,以便用户不会被卡住了巨大的延迟。

I am using Rotativa in my MVC3 app to generate pdfs into a memory stream which is then emailed out as an email attachment. This works fine but it is quite slow (~5-7 seconds with just 1 user) so I've been trying to put it into a separate thread so the user doesn't get stuck with a huge delay.

我一直面临的问题是,Rotativa需要控制器上下文生成数据到内存流,这意味着,如果你试图把它放在一个单独的线程,并返回一个通知用户则上下文处置和PDF生成会失败。

The problem I've been facing is that Rotativa requires the Controller Context to generate the data into the memory stream, which means that if you try to put it in a separate thread and return a notification to the user then the context is disposed and the pdf generation will fail.

不幸的是我在服务器端的电子邮件验证检查,并返回真/假在适当情况下,假会提示用户修复它,然后再试一次。这意味着我不能只是假定电子邮件是总是有效的(我可以通过jQuery的做到这一点,但如果将其关闭,并尝试提交他们不会得到一个错误消息)。

Unfortunately I do an email validation check on the server-side and return a true/false where appropriate, false will prompt the user to fix it and try again. This means I can't just assume that the email is always valid (I could do it by jquery, but if they turn it off and try to submit they won't get an error message).

到目前为止,我曾尝试:

So far I have tried:


  • 创建一个新的线程,并传递上下文中

  • 将其复制到一个新的变量复制上下文

  • 序列化的背景下,流传递给新的线程和反序列化(不幸的是上下文不序列化)

有没有人有任何其他的想法?

Has anyone got any other ideas?

推荐答案

下面是我做的运行在与上下文背景一个漫长的过程。我使用的数据库所支持的自定义会话。你需要通过你需要进入后台的行动无论值。

Here is what I do to run a long process in the background with context. I'm using custom sessions backed by a database. You'll need to pass whatever values you need into the "background" action.

using (var client = new WebClient())
{
     var values = new NameValueCollection
     {
         { "sessionid", DataSession.Id.ExtractSid() }
     };
     client.UploadValuesAsync(new Uri(Url.AbsoluteAction("ResultsCallback", "Quote")), values);
}                

这篇关于MVC3传递ControllerContext线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆