我正在设置一个API来与数据库交互,当api接收到一个post来创建一个已经出现在其各自表上的条目时,我希望返回一个代码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,当private HttpRequestMessage _request属性在HttpResponseMessage中设置时,会抛出异常
发布于 2018-04-17 19:28:21
不要用积木。只需将它们存储在私有变量中,并使用控制器处理来处理它们。此外,您还可以对已经完成的任务使用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);
}
}https://stackoverflow.com/questions/49882323
复制相似问题