HttpControllerContext.Configuration is sometimes null during Content negotiation

删除回忆录丶 提交于 2021-02-08 10:44:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!