繁体   English   中英

何时处置HttpResponseMessage或HttpContent

[英]When to dispose of a HttpResponseMessage or a HttpContent

我正在设置一个API来与数据库进行交互,当api收到一个帖子来创建一个已经存在于其各自表中的条目时,我想返回一个代码209与一条解释性消息冲突。 所以我尝试了这个:

调节器

public async Task<IHttpActionResult> PostOrganizationAsync(Organization organization)
{
    using (var context = new ContextHandler())
    {
        bool added = await context.AddOrganizationAsync(organization);
        if (added)
        {
            return Ok();
        }
        else
        {
            using (var httpContent = new StringContent("An organization with this Id already exists on the database"))
            {
                return new CustomHttpResponse(HttpStatusCode.Conflict, httpContent);
            }
        }
    }
}

ContextHandler中

class ContextHandler : IDisposable
{
    static AuditLogsEntities LogDatabaseContext;

    public ContextHandler()
    {
        LogDatabaseContext = new AuditLogsEntities();
        LogDatabaseContext.Configuration.LazyLoadingEnabled = false;
    }

    public void Dispose()
    {
        LogDatabaseContext.Dispose();
    }

    public async Task<bool> AddOrganizationAsync(Organization organization)
    {
        var findOrg = await LogDatabaseContext.OrganizationSet.FindAsync(organization.Id);
        if (findOrg != null)
        {
            return false;
        }
        else
        {
            LogDatabaseContext.OrganizationSet.Add(organization);
            await LogDatabaseContext.SaveChangesAsync();
            return true;
        }
    }
}

CustomHttpResponse

public class CustomHttpResponse : IHttpActionResult
    {
        public HttpStatusCode StatusCode;
        public HttpContent Content;

        public CustomHttpResponse(HttpStatusCode httpStatusCode, HttpContent httpContent)
        {
            StatusCode = httpStatusCode;
            Content = httpContent;
        }

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            HttpResponseMessage httpResponse = null;
            using (httpResponse = new HttpResponseMessage(StatusCode))
            {
                httpResponse.Content = Content;
                return httpResponse;
            }         
        }
    }

发布一个新的有效条目工作正常,但尝试发布一个已经存在的条目来测试209响应导致一个ObjectDisposedException ,我认为这是因为我的一个using块发生,但问题是我不确定是哪个阻止它是为什么会导致错误。

编辑:我不知何故现在才意识到,根据StackTrace,当在HttpResponseMessage中设置private HttpRequestMessage _request属性时抛出异常

不要使用块。 只需将它们存储在一个私有变量中,并通过控制器处理它们进行处理。 此外,您可以使用Task.FromResult完成已完成的任务。

调节器

private StringContent _httpContent
private CustomHttpResponse _customHttpResponse

public async Task<IHttpActionResult> PostOrganizationAsync(Organization organization)
{
    using (var context = new ContextHandler())
    {
        bool added = await context.AddOrganizationAsync(organization);
        if (added)
        {
            return Ok();
        }
        else
        {
            this._httpContent = new StringContent("An organization with this Id already exists on the database"))
            this._customHttpResponse = new CustomHttpResponse(HttpStatusCode.Conflict, httpContent);
            return this._customHttpResponse;
        }
    }
}

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        if (this._httpContent != null)
        {
            this._httpContent.Dispose();
        }
        if (this._customHttpResponse != null)
        {
            this._customHttpResponse.Dispose();
        }
    }
}

CustomHttpResponse

public class CustomHttpResponse : IHttpActionResult
{
    public HttpStatusCode StatusCode;
    public HttpContent Content;

    public CustomHttpResponse(HttpStatusCode httpStatusCode, HttpContent httpContent)
    {
        StatusCode = httpStatusCode;
        Content = httpContent;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        HttpResponseMessage httpResponse = new HttpResponseMessage(StatusCode);
        httpResponse.Content = Content;
        return Task.FromResult(httpResponse);
    }
}

暂无
暂无

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

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