简体   繁体   English

Response.WriteAsync和返回字符串之间的区别是什么

[英]What's the difference between Response.WriteAsync and returning a string

Hi I was wandering if someone can explain to me the difference between these two methods for returning data from a controller. 嗨,如果有人能向我解释这两种从控制器返回数据的方法之间的区别,我就会徘徊。 Is there an advantage or reason for using on method over the other? 使用方法优于另一方是否有优势或理由?

I am guessing that the returning function just calls Response.WriteAsync further down the line but I am not sure. 我猜测返回的函数只是进一步调用Response.WriteAsync ,但我不确定。

Using postman both methods return the exact same response, so I was just curious about the two options, is there a reason for using one over the other or just personal preference. 使用邮递员这两种方法都返回完全相同的响应,所以我只是对这两个选项感到好奇,是否有理由使用其中一个或仅仅是个人偏好。

Between Response.WriteAsync: 在Response.WriteAsync之间:

[HttpGet("Fetch_WriteAsync")]
public async Task Fetch_AsyncWrite()
{
    HttpContext.Response.ContentType = "application/json";
    await HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(new { data = "my_fetched_data" }));
}

and just returning: 然后回来:

[HttpGet("Fetch_Return")]
public async Task<string> Fetch_Return()
{
    HttpContext.Response.ContentType = "application/json";
    return await Task.Factory.StartNew(()=>JsonConvert.SerializeObject(new { data = "my_fetched_data" }));
}

Basically there is a difference regarding how your code is executed under the hood. 基本上,您的代码在引擎盖下的执行方式存在差异。 In the first case, in which you have this 在第一种情况下,你有这个

await HttpContext.Response
                 .WriteAsync(JsonConvert.SerializeObject(new 
                  { 
                      data = "my_fetched_data" 
                  }));

an ASP.NET thread would be used to execute your code. ASP.NET线程将用于执行您的代码。

While in the second case, in which you have this 而在第二种情况下,你有这个

return await Task.Factory
                 .StartNew(()=>JsonConvert.SerializeObject(new 
                 { 
                     data = "my_fetched_data" 
                 }));

a thread pool thread would be used. 将使用线程池线程。

As it is stated here : 正如这里所说:

You can use the ThreadPool in exactly the same way in ASP.NET and it works just as you would expect. 您可以在ASP.NET中以完全相同的方式使用ThreadPool,它可以像您期望的那样工作。 The problem is not in the ThreadPool itself but in what else ASP.NET uses it for at the same time. 问题不在于ThreadPool本身,而在于ASP.NET同时使用它的问题。 ASP.NET is multi-threaded by design and it uses the ThreadPool to serve pages and content mapped to the ASP.NET ISAPI filter. ASP.NET在设计上是多线程的,它使用ThreadPool来提供映射到ASP.NET ISAPI过滤器的页面和内容。

If you also use the ThreadPool, then ASP.NET has fewer threads to utilize and requests are put on hold until the pool returns a free thread . 如果您还使用ThreadPool,则ASP.NET使用的线程较少, 并且在池返回空闲 线程 之前请求被保留 This might not be a problem for a low traffic site, but more popular sites can get into trouble. 对于流量较低的站点,这可能不是问题,但更受欢迎的站点可能会遇到麻烦。 Low traffic sites can get into trouble if they use the ThreadPool a lot. 低流量站点如果使用ThreadPool很多就会遇到麻烦。

That being said, I would opt for the first solution. 话虽这么说,我会选择第一个解决方案。

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

相关问题 多个Response.writeAsync调用 - Multiple Response.writeAsync Calls 在自定义中间件中测试response.WriteAsync() - Testing response.WriteAsync() in custom middleware 返回void和返回任务有什么区别? - What's the difference between returning void and returning a Task? 在实现迭代器时,返回IEnumerator或IEnumerable之间有什么区别? - when implementing iterator, what's the difference between returning IEnumerator or IEnumerable? 使用 EnumeratorCancellation 返回 AsyncEnumerable 或循环 WithCancellation 有什么区别 - What's the difference between returning AsyncEnumerable with EnumeratorCancellation or looping WithCancellation String.Count和String.Length有什么区别? - What's the difference between String.Count and String.Length? “string”和“String”有什么区别? - What is the difference between “string” and “String”? 这些查询有什么区别? - What's the difference between these queries? string和StringBuilder有什么区别? - What is the difference between string and StringBuilder? 可空字符串(字符串?)和初始化为可原谅的字符串有什么区别 null(字符串 s = null!) - What is the difference between a nullable string (string?) and a string initiated to a forgiven null (string s = null!)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM