简体   繁体   English

如何在Web Api方法中处理HttpResponseMessage?

[英]How do I Dispose a HttpResponseMessage in my Web Api Method?

I'm getting the CA2000 error on all my Wep Api 2 controllers for the Options() verb. 我在所有Wep Api 2控制器上都遇到了Option2000()动词的CA2000错误。

In method 'PromotionController.Options()', object '<>g__initLocal0' is not disposed along all exception paths. 在方法'PromotionController.Options()'中,未沿所有异常路径放置对象'<> g__initLocal0'。

I need to support pre-flight requests made for cross domain ajax calls so all I'm doing is returning a HttpStatusCode.Ok so that the subsequent post/put/delete will fire off. 我需要支持针对跨域Ajax调用发出的飞行前请求,因此我正在做的只是返回HttpStatusCode.Ok以便随后的发布/发布/删除操作将被触发。

public HttpResponseMessage Options()
{
    return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}

I have tried the using statement like this: 我已经尝试过using语句,例如:

public HttpResponseMessage Options()
{
    using(var response = new HttpResponseMessage())
    {
        response.StatusCode = HttpStatusCode.OK;
        return response;
    }
}

but when I hit it with an Options request from Postman I get an exception: 但是当我用邮递员的选项请求击中它时,我得到了一个例外:

Cannot access a disposed object.\\r\\nObject name: 'System.Net.Http.HttpResponseMessage'. 无法访问已处置的对象。\\ r \\ n对象名称:'System.Net.Http.HttpResponseMessage'。

How do I return the HttpStatusCode.Ok in a way that it won't throw Code Analysis errors? 如何以不会引发代码分析错误的方式返回HttpStatusCode.Ok

Thanks! 谢谢!

Exception about accessing a disposed object is obvious because when execution leaves the using block, response is disposed, but you're returning it out of the using block and out of the method. 关于访问已处置对象的异常很明显,因为当执行离开using块时, response已处置,但是您将其从using块和方法中返回。 You can't access that object outside of the method, because it simply doesn't exist anymore. 您无法在方法之外访问该对象,因为它不再存在。

In general, to solve the CA2000 warning simply dispose the instance of HttpResponseMessage class explicitly when you don't need it anymore. 通常,要解决CA2000警告,只需在不再需要HttpResponseMessage类的实例时就对其进行显式处理。

In your case solution could be to override the Dispose method of your controller (implements the ApiController method) and explicitly dispose your instances there . 在您的情况下,解决方案可能是重写控制器Dispose方法 (实现ApiController方法),并在那里明确地 ApiController 您的实例 Check for example this question . 例如检查这个问题

You can also just suppress the warning and instance will be disposed later by the garbage collector anyway. 您还可以仅禁止显示警告,并且无论如何该实例将在以后由垃圾收集器处理。 But it's not a proper solution. 但这不是一个适当的解决方案。

You can register the response object for disposing using RegisterForDispose method of HttpRequestMessage class. 您可以使用HttpRequestMessage类的RegisterForDispose方法RegisterForDispose要处理的响应对象。 Infrastructure of .Net will disposes all registered object after disposing the request. .Net基础结构将在处理请求后处理所有注册的对象。

I can't guaranty that you won't get the error after that, but the object will be disposed so that you can suppress the error. 我不能保证在那之后您不会收到错误,但是该对象将被处置,以便您可以抑制错误。

Example : 范例

HttpResponseMessage response = Request.CreateResponse();
// Do what you need with response
// ...
// ...
request.RegisterForDispose(response);// response will be disposed by host

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM