问题
We are setting up a ASP.NET 4.6.2 Web Api host and noticing that some requests fail because of the following error:
HttpControllerContext.Configuration must not be null.
I've been unable to reproduce the issue and wanted to ask if anybody could steer me in the right direction?
This is the stack trace:
[0] System.InvalidOperationException "HttpControllerContext.Configuration must not be null." at System.Web.Http.Results.NegotiatedContentResult`1.ApiControllerDependencyProvider.EnsureResolved() at System.Web.Http.Results.NegotiatedContentResult`1.ApiControllerDependencyProvider.get_ContentNegotiator() at System.Web.Http.Results.BadRequestErrorMessageResult.Execute() at System.Web.Http.Results.BadRequestErrorMessageResult.ExecuteAsync(CancellationToken cancellationToken) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()
回答1:
A similar error occurred in my tests when adding headers to a BadRequestErrorMessageResult. The fix only required to initialize the controller's Configuration. ...and subsequently initialize the Request.
So my test's setup ended up looking something like
public class FooControllerTest
{
private FooController _sut;
[Setup]
public void Setup()
{
_sut = new FooController();
_sut.Configuration = new System.Web.Http.HttpConfiguration();
_sut.Request = new System.Net.Http.HttpRequestMessage();
}
}
Hope that helps.
来源:https://stackoverflow.com/questions/55480264/httpcontrollercontext-configuration-is-sometimes-null-during-content-negotiation