Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不等待的情况下发布httpclient?_C#_Httpclient - Fatal编程技术网

C# 如何在不等待的情况下发布httpclient?

C# 如何在不等待的情况下发布httpclient?,c#,httpclient,C#,Httpclient,我正在使用HttpClient将数据发布到webapi应用程序 此代码可以工作(web api接收post调用),但代码正在等待响应 public static async void Notify(List<string> txs,string url) { using (HttpClient client = new HttpClient() ) { string resourceAddress = url; client.Def

我正在使用HttpClient将数据发布到webapi应用程序

此代码可以工作(web api接收post调用),但代码正在等待响应

public static async void Notify(List<string> txs,string url) 
{
    using (HttpClient client = new HttpClient() )
    {
        string resourceAddress = url; 
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        await client.PostAsJsonAsync(resourceAddress, txs);
    }
}
公共静态异步void Notify(列表txs,字符串url)
{
使用(HttpClient=new HttpClient())
{
字符串resourceAddress=url;
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
等待client.postsjsonasync(resourceAddress,txs);
}
}
此函数不会等待web api的响应,但web api不会得到任何post调用:

public static void Notify(List<string> txs,string url) 
{
    using (HttpClient client = new HttpClient() )
    {
        string resourceAddress = url; 
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.PostAsJsonAsync(resourceAddress, txs);
    }
}
publicstaticvoidnotify(列表txs,字符串url)
{
使用(HttpClient=new HttpClient())
{
字符串resourceAddress=url;
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
client.postsjsonasync(resourceAddress,txs);
}
}
我需要调用web api并在不等待的情况下继续执行代码。 如何使用HttpClient实现这一点? 并且我要完成执行的方法(不包括内部的其他工作)

我需要调用web api并继续执行代码,而不需要 等待。如何使用HttpClient实现这一点

调用Notify时,它应该返回System.Threading.Task,此任务是管理Notify方法执行的包装器,反过来也是PostAsJsonAsync方法


wait告诉您的方法在此时暂停执行并等待任务完成。但如果有调用方法,则调用方法在等待时继续。如果调用方法也等待,那么这将等待任务完成,调用堆栈上的下一个方法将继续,依此类推,直到我们离开代码堆栈并在某种非阻塞层中等待代码完成(例如,异步ASP.NET MVC或异步Winforms UI)。或者我们有一个明确的阻止任务。等待呼叫。

如果有人仍在寻找答案

public void NotifyAsyncWrapper(IEnumerable<string> txs, string url)
{
    Notify(txs, url).ContinueWith(ContinuationAction, TaskContinuationOptions.OnlyOnFaulted);
}

public async Task Notify(IEnumerable<string> txs, string url)
{
    //async await code
}

private void ContinuationAction(Task task)
{
    if (task.Exception != null)
    {
        logger.LogError(ex, ex.Message);
    }
}
public void NotifyAsyncWrapper(IEnumerable txs,字符串url)
{
Notify(txs,url).ContinueWith(ContinuationAction,TaskContinuationOptions.OnlyOnFaulted);
}
公共异步任务通知(IEnumerable txs,字符串url)
{
//异步等待代码
}
私有void ContinuationAction(任务)
{
if(task.Exception!=null)
{
logger.LogError(例如,例如消息);
}
}

我认为您需要一个异步任务..我想退出函数Notify,并且仍然使POST作为父方法在任务中异步运行。尝试我上面提到的方法,从方法返回任务,它将继续运行Async。我很好奇,这段代码是否经过测试可以实际工作?在我看来,HttpClient应该在请求的中间得到处理。我会将
using
语句移动到外部范围,确保
在处理客户端之前等待
响应,但这可能没有任何区别。
  var task = Notify(someList, someUrl);

  // do a long running task / extra work here, 
  // we have not awaited Notify therefore this will execute whilst the post 
  // is running

  await task;
  // at this point the we have waited for the Notify method to complete, 
  // this will block for the time the post has left to complete (if any at all)
public void NotifyAsyncWrapper(IEnumerable<string> txs, string url)
{
    Notify(txs, url).ContinueWith(ContinuationAction, TaskContinuationOptions.OnlyOnFaulted);
}

public async Task Notify(IEnumerable<string> txs, string url)
{
    //async await code
}

private void ContinuationAction(Task task)
{
    if (task.Exception != null)
    {
        logger.LogError(ex, ex.Message);
    }
}