.NET MVC Fire and Forget方法也会立即返回View [英] .NET MVC Fire and Forget Method also return View immediately

查看:63
本文介绍了.NET MVC Fire and Forget方法也会立即返回View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Fire& amp;的最佳解决方案在Action上忘记方法,然后立即返回View.据我所知,如果我将Action的返回类型设为 Task< ActionResult> await 异步方法,它将起作用,但是Action还在等待该异步方法完成之前以预期的形式返回查看".

I'm looking on best solution for Fire & Forget a method at the Action and return View immediately. So far as i can see if i make the Action's return type Task<ActionResult> and await the asynchronous method it will work but Action is also waiting that asynchronous method to done before return View as expected.

另一方面,如果我不等待异步方法,则在 View 返回后将中断该方法的执行.

In the other hand if i don't await the asynchronous method, method's execution will be cut after the View returns.

我要问这样的情况最好的解决方案是什么?我的代码是这样的:

I'm asking for what's the best solution for situation like that ? My code is like that :

public class DefaultController : Controller
{
    // GET: Default
    public async Task<ActionResult> Index()
    {
        await Asynchronous();
        return View();
    }
    public async Task Asynchronous()
    {
        var FilePath = ControllerContext.HttpContext.Server.MapPath("~/HelloWorld.txt");
        for(int i = 0; i <= 100; i++)
        {
            await Task.Delay(15000); // Wait 15 Seconds.
            System.IO.File.AppendAllLines(FilePath, new string[] { i.ToString() });
        }
    }
}

推荐答案

我正在寻找Fire& amp;的最佳解决方案在Action处忘记方法并立即返回View

I'm looking on best solution for Fire & Forget a method at the Action and return View immediately

这取决于最佳"的含义.在ASP.NET上执行忘我"操作本质上是不安全的,因此您希望代码的安全性在不同程度上都有不同程度.

That depends on what "best" means. Fire and Forget on ASP.NET is inherently unsafe, so there are varying degrees of how safe you want your code to be.

如果您的应用程序必须继续执行,那么唯一安全的系统是让您的操作处理程序将其要执行的操作写入安全存储机制(例如,Azure Queue,MSMQ或SQL Server)).一旦安全地存储了它,您的操作方法就可以返回.然后,您还将拥有一个独立的后台进程(例如,Azure Function,Win32 Service或ASP.NET进程中的一个线程,除非您非常谨慎地托管它).该后台进程将从安全存储中读取并进行实际工作.

If your app must continue execution, then the only safe system is to have your action handler write what it wants to do into a safe storage mechanism (e.g., Azure Queue, MSMQ, or SQL Server). Once it has been safely stored, then your action method can return. Then you'll also have an independent background process (e.g., Azure Function, Win32 Service, or possibly a thread in your ASP.NET process only if you're very careful about how it's hosted). That background process will read from the safe storage and do the actual work.

如果您的应用程序偶尔会出现丢失"的情况(将成功返回给客户端之后)很好,那么您可以使用不太安全的机制,例如.NET 4.5.2的 HostingEnvironment.QueueBackgroundWorkItem ,或者我的 AspNetBackgroundTasks库(对于较早的版本).

If your app is fine with occasionally "losing" work (after returning success to the client), then you can use a less-safe mechanism such as HostingEnvironment.QueueBackgroundWorkItem for .NET 4.5.2, or my AspNetBackgroundTasks library for earlier versions.

我的博客中列出了其他替代方法发表关于这个主题的.

这篇关于.NET MVC Fire and Forget方法也会立即返回View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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