gpt4 book ai didi

asp.net-mvc - ASP.NET WebApi + Autofac 找不到 ApiController

转载 作者:行者123 更新时间:2023-12-02 17:46:15 26 4
gpt4 key购买 nike

我正在尝试让 Autofac 与 WebApi 一起使用。我的 ApiControllers 位于与主 Web 项目不同的单独项目中。

库存系统.Web.UIInventorySystem.Web.Api.Controllers

每当我尝试浏览 api 路由时,我都会收到响应:

<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>

当我单步执行 Autofac 代码时,我看到我的 Controller 已注册,但 AutofacControllerActivator.Create() 返回 null。具体是线 返回(ResolutionExtensions.ResolveOptional(范围,controllerType)作为IHttpController);

autofac 代码:字符串 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));

我错过了什么吗?

谢谢

最佳答案

RegisterApiControllers 注册扩展会查找实现 IHttpController 接口(interface)且名称以“Controller”结尾的类型。您的 Controller 类名称是否有“Controller”后缀?

关于asp.net-mvc - ASP.NET WebApi + Autofac 找不到 ApiController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10343292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com