无需等待调用异步方法 [英] Calling an async method without awaiting

查看:135
本文介绍了无需等待调用异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用异步方法(在ASP.NET Web API 2应用程序),而等待的结果。我的意思是我想主线程继续执行并得到完成无等待调用的方法。我想这个片段:

  //异步方法:
私有静态异步任务LogAsync(例外的例外,串IP,MethodBase方法,对象参数){
    //一些东西
}

//调用者的方法:
公共静态无效LOG1(例外的例外,对象参数){
    LogAsync(例外,IP,方法,参数);
}
公共静态无效LOG2(例外的例外,对象参数){
    Task.Factory.StartNew(()=> LogAsync(例外,IP,方法,参数));
}
公共静态异步无效LOG3(例外的例外,对象参数){
    等待LogAsync(例外,IP,方法,参数).ConfigureAwait(真正的);
}
公共静态异步无效LOG4(例外的例外,对象参数){
    //我tryied甚至这一个:
    等待LogAsync(例外,IP,方法,参数).ConfigureAwait(假);
}
 

正如你所看到的,我tryied不同的方式;但非他们给我我想要的。你有什么想法,有什么能帮助我吗?

解决方案
  

我想没有等待的结果调用异步方法(在ASP.NET Web API 2应用程序)

你确定这就是你想要做什么?在ASP.NET中,如果你开始像这样独立的工作(也就是工作的没有涉及到一个HTTP请求的),那么这个工作可能会不知情的情况下中止。在这种情况下,你的方法被称为日志,所以presumably他们登录异步。所以,你只需要的避免的的等待,如果它是完全可以接受偶尔失去日志。如果你希望你的日志是可靠的,你需要保持计谋。你的电话。

假设它的确定偶尔失去日志,那么你至少应该注册ASP.NET运行时的工作。如果你在.NET 4.5.2,您可以使用 HostingEnvironment.QueueBackgroundWorkItem ,因为这样的:

 公共静态无效的登录(例外的例外,对象参数){
  HostingEnvironment.QueueBackgroundWorkItem(_ =>
      LogAsync(例外,IP,方法,参数));
}
 

如果你在.NET的早期版本(4.5或4.5.1),你可以用我的 AspNetBackgroundTasks库

 公共静态无效的登录(例外的例外,对象参数){
  BackgroundTaskManager.Run(()=>
      LogAsync(例外,IP,方法,参数));
}
 

这两者之间的语义略有不同(我有一个书面记录的我的博客上),但他们都注册到ASP.NET运行时的工作,所以,至少它不是的完全的不可靠,比如 Task.Factory .StartNew Task.Run

I'm trying to call an async method (in an ASP.NET Web API 2 app) without awaiting for the result. I mean I want to main thread to continue executing and no-wait for called method to get completed. I'm trying this snippet:

// The async method:
private static async Task LogAsync(Exception exception, string ip, MethodBase method, object parameters) {
    // some stuff 
}

// The caller methods:
public static void Log1(Exception exception, object parameters) {
    LogAsync(exception, ip, method, parameters);
}
public static void Log2(Exception exception, object parameters) {
    Task.Factory.StartNew(() => LogAsync(exception, ip, method, parameters));
}
public static async void Log3(Exception exception, object parameters) {
    await LogAsync(exception, ip, method, parameters).ConfigureAwait(true);
}
public static async void Log4(Exception exception, object parameters) {
    // I've tryied even this one:
    await LogAsync(exception, ip, method, parameters).ConfigureAwait(false);
}

As you can see, I tryied different ways; but non of them give me what I want. Do you have any idea, what would help me?

解决方案

I'm trying to call an async method (in an ASP.NET Web API 2 app) without awaiting for the result.

Are you sure that's what you want to do? On ASP.NET, if you start up independent work like this (that is, work not related to an HTTP request), then that work may be aborted without your knowledge. In this scenario, your methods are called "log", so presumably they are logging asynchronously. So you only want to avoid the await if it's perfectly acceptable to occasionally lose logs. If you want your logs to be reliable, you'll need to keep the await. Your call.

Assuming that it's OK to occasionally lose logs, then you should at least register the work with the ASP.NET runtime. If you're on .NET 4.5.2, you can use HostingEnvironment.QueueBackgroundWorkItem, as such:

public static void Log(Exception exception, object parameters) {
  HostingEnvironment.QueueBackgroundWorkItem(_ =>
      LogAsync(exception, ip, method, parameters));
}

If you're on an older version of .NET (4.5 or 4.5.1), you can use my AspNetBackgroundTasks library:

public static void Log(Exception exception, object parameters) {
  BackgroundTaskManager.Run(() =>
      LogAsync(exception, ip, method, parameters));
}

The semantics between the two are slightly different (I have a writeup on my blog), but they both register the work with the ASP.NET runtime so at least it's not entirely unreliable like Task.Factory.StartNew or Task.Run is.

这篇关于无需等待调用异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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