ASP.NET WebApi + Autofac does not find ApiControllers

I am trying to get Autofac to work with WebApi. I have ApiControllers in a separate project from a main web project.

InventorySystem.Web.UI InventorySystem.Web.Api.Controllers

Whenever I try to switch to the api route, I get a response:

<Exception xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ExceptionType>System.ArgumentNullException</ExceptionType>
    <Message>Value cannot be null. Parameter name: value</Message>
    <StackTrace>
at System.Web.Http.Controllers.HttpControllerContext.set_Controller(IHttpController value)
at System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateInstance(HttpControllerContext controllerContext, HttpControllerDescriptor controllerDescriptor)
at System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateController(HttpControllerContext controllerContext, String controllerName)
at Autofac.Integration.WebApi.AutofacControllerFactory.CreateController(HttpControllerContext controllerContext, String controllerName)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    </StackTrace>
</Exception>

When I look through the Autofac code, I see that my controller is registered, but AutofacControllerActivator.Create () returns null. In particular, the return line (ResolutionExtensions.ResolveOptional (scope, controllerType) as IHttpController);

Autofocus code: string binDirectory = Path.Combine (HttpRuntime.AppDomainAppPath, "bin");

        var builder = new ContainerBuilder();

        builder.ConfigureWebApi(GlobalConfiguration.Configuration);

        builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
        builder.RegisterApiControllers(Assembly.LoadFile(Path.Combine(binDirectory, "InventorySystem.Web.Api.dll"))).PropertiesAutowired();
        builder.RegisterFilterProvider();            

        var container = builder.Build();

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new AutofacWebApiDependencyResolver(container));

Am I missing something?

thank

+5
source share
4 answers

, SO, , API-.

+2

RegisterApiControllers , IHttpController , "Controller". ""?

+1

Do you forget to register designer dependencies for your Api controllers with an assembly container?

HostingEnvironment.MapPath ("~ / bin") is another good way to get the file path in the bin directory.

0
source
var builder = new ContainerBuilder();   
builder.Update(container);
 var webApiResolver = new Autofac.Integration.WebApi.AutofacWebApiDependencyResolver(container);
    System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;
0
source

All Articles