有许多方法可以解决这种问题,包括为客户端上的每个调用构建一个新的HttpContent,或者将内容加载到流中并更改指针.
我想知道为什么调用此方法会自动调用其IDisposable参数的处理?据我所知,这不是.NET中的常见行为
值得注意的是,在PUT请求中也观察到这种行为是幂等的,因此这种行为是为了防止再次发送信息的前提似乎不正确.
我无法立即在referencesource上找到实现,但WCF源代码也包含它.您正在寻找的方法是DisposeRequestContent(HttpRequestMessage)
,随附的评论说明了这一点:
When a request completes,
This also ensures that aHttpClient
disposes the request content so the user doesn’t have to.HttpContent
object is only sent once usingHttpClient
(similar toHttpRequestMessages
that can also be sent only once).
HttpContent content = request.Content; if (content != null) { content.Dispose(); }
基本上,这是一个安全措施,以确保您不会发送两次相同的响应,他们认为这是一个糟糕/不常见/不鼓励的用例.
精彩评论