在asp.net核心中未在HttpRequestMessage和HttpResponseMessage上调用Dispose [英] Not calling Dispose on HttpRequestMessage and HttpResponseMessage in asp.net core

查看:192
本文介绍了在asp.net核心中未在HttpRequestMessage和HttpResponseMessage上调用Dispose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用asp.net核心在HttpRequestMessage和HttpResponseMessage上调用Dispose(或不调用)的最佳实践是什么?

What is the best practice for calling Dispose (or not) on HttpRequestMessage and HttpResponseMessage with asp.net core?

示例:

https://github.com/aspnet/Security/blob/1.0.0/src/Microsoft.AspNetCore.Authentication.Google/GoogleHandler.cs#L28-L34

protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
{
    // Get the Google user
    var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

    var response = await Backchannel.SendAsync(request, Context.RequestAborted);
    response.EnsureSuccessStatusCode();

    var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
    ...
 }

https://github.com/aspnet/Security/blob/1.0.0/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookHandler.cs#L37-L40
这两个示例都没有调用Dispose

and https://github.com/aspnet/Security/blob/1.0.0/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookHandler.cs#L37-L40
Both examples are not calling Dispose

这是一个遗漏吗?还是背后有正当的理由,也许是因为该方法是异步的?当然,CG会最终确定它们,但这是在这种情况下的最佳做法,为什么?请注意,以上示例是ASP.NET Core中间件组件的一部分.

Could this be an omission? or is there a valid reason behind it, maybe because the method is async? Of course the CG will eventually finalize them, but is this the best practice to do so under this circumstance and why? Notice that the above examples are part of ASP.NET Core Middleware components.

推荐答案

我在代码示例所属的github存储库上打开了一个问题.

I opened an issue on the github repository where the code examples belong to.

https://github.com/aspnet/Security/issues/886

在这些情况下并不重要.处置请求或响应仅在其内容"字段上调用处置".在各种HttpContent实现中,仅StreamContent需要处理任何事情. HttpClient的默认SendAsync可完全缓冲响应内容并处理流,因此调用者无需执行任何操作.

It's not important in these scenarios. Disposing a request or response only calls Dispose on their Content field. Of the various HttpContent implementations, only StreamContent needs to dispose anything. HttpClient's default SendAsync fully buffers the response content and disposes the stream, so there's nothing the caller needs to do.

但是为了避免产生奇怪的错误,我们最好处置这些对象. MemoryStream是另一个类,由于其当前的基础实现,它也经常不被处理.

But for the sake of not getting weird bugs down the line, we are better off disposing those object. MemoryStream is another class that is also often not dispose because of his current underlying implementation.

https://stackoverflow.com/a/234257/6524718

如果您完全确定自己从不希望从MemoryStream转移到另一种流,则不调用Dispose不会对您造成任何伤害.但是,这通常是一种很好的做法,部分原因是,如果您确实更改了使用其他Stream的方法,那么您就不想被难以发现的bug所困扰,因为您早早选择了简便的方法. (另一方面,有YAGNI参数...)

If you're absolutely sure that you never want to move from a MemoryStream to another kind of stream, it's not going to do you any harm to not call Dispose. However, it's generally good practice partly because if you ever do change to use a different Stream, you don't want to get bitten by a hard-to-find bug because you chose the easy way out early on. (On the other hand, there's the YAGNI argument...)

无论如何,这样做的另一个原因是,新的实现可能会引入可在Dispose上释放的资源.

The other reason to do it anyway is that a new implementation may introduce resources which would be freed on Dispose.

这篇关于在asp.net核心中未在HttpRequestMessage和HttpResponseMessage上调用Dispose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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