C# Autofac未解析ApicController的类型和命名参数
我有三个不同的应用程序,它们都使用相同的业务层/数据层构建。我正在向所有三个应用程序都使用的IUnitOfWork类添加一个IUserNameProvider。因为每个应用程序使用不同的方法获取用户名,所以我创建了IUserNameProvider,并使用Autofac注入适当的实现 这似乎应该是相当简单的代码,但我无法为Web API应用程序正确配置它。类似的代码在控制台应用程序中运行良好 My Global.asax.csC# Autofac未解析ApicController的类型和命名参数,c#,asp.net-web-api,autofac,C#,Asp.net Web Api,Autofac,我有三个不同的应用程序,它们都使用相同的业务层/数据层构建。我正在向所有三个应用程序都使用的IUnitOfWork类添加一个IUserNameProvider。因为每个应用程序使用不同的方法获取用户名,所以我创建了IUserNameProvider,并使用Autofac注入适当的实现 这似乎应该是相当简单的代码,但我无法为Web API应用程序正确配置它。类似的代码在控制台应用程序中运行良好 My Global.asax.cs protected void Application_Start()
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterType<WebAPIGetUser>().As<SSEMPA.DataAccess.Infrastructure.IUserNameProvider>();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>()
.WithParameter("connectionString", ConfigurationManager.ConnectionStrings["MyDataContex"].ConnectionString);
//other type registrations ...
DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
// other registrations ....
}
我的遥控器
public class AgencyApiController : ApiController
{
private readonly IUnitOfWork _unitOfWork;
public AgencyApiController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
...
}
当API被命中时,它会抛出以下错误:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>An error occurred when trying to create a controller of type 'AgencyApiController'. Make sure that the controller has a parameterless public constructor.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'SSEMPA.DataAccess.Infrastructure.UnitOfWork' can be invoked with the available services and parameters: Cannot resolve parameter 'SSEMPA.DataAccess.Infrastructure.IUserNameProvider userNameProvider' of constructor 'Void .ctor(SSEMPA.DataAccess.Infrastructure.IUserNameProvider , System.String)'.
</ExceptionMessage>
<ExceptionType>Autofac.Core.DependencyResolutionException</ExceptionType>
发生了一个错误。
尝试创建“AgencyApicController”类型的控制器时出错。确保控制器具有无参数公共构造函数。
System.InvalidOperationException异常
位于System.Web.Http.controllerDescriptor.HttpControllerDescriptor.CreateController(HttpRequestMessage请求,HttpControllerDescriptor,controllerDescriptor类型),位于System.Web.Http.Dispatcher.HttpControllerDescriptor.CreateController(HttpRequestMessage请求),位于System.Web.Http.Dispatcher.HttpControllerDispatcher.d_u1.MoveNext()
发生了一个错误。
在类型“SSEMPA.DataAccess.Infrastructure.UnitOfWork”上找到的具有“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数均不能使用可用服务和参数调用:无法解析构造函数“Void.ctor”的参数“SSEMPA.DataAccess.Infrastructure.IUserNameProvider userNameProvider”(SSEMPA.DataAccess.Infrastructure.IUserNameProvider,System.String)“”。
Autofac.Core.dependencResolutionException
我已经尝试为IUserNameProvider和connections字符串使用命名参数,但这并没有改变任何东西。看起来这应该可以工作,所以我肯定缺少一些小的东西
感谢您查看此信息。除了控制台应用程序之外,您还需要对WebAPI进行其他配置和设置。请查看此处的文档以获取说明:谢谢!其他人配置了此功能,并且不再在项目中。我不知道已经内置了另一个配置。answ是什么呃,这会导致一个有100个链接的网站吗???为什么不在这里粘贴google.com呢?
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>An error occurred when trying to create a controller of type 'AgencyApiController'. Make sure that the controller has a parameterless public constructor.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'SSEMPA.DataAccess.Infrastructure.UnitOfWork' can be invoked with the available services and parameters: Cannot resolve parameter 'SSEMPA.DataAccess.Infrastructure.IUserNameProvider userNameProvider' of constructor 'Void .ctor(SSEMPA.DataAccess.Infrastructure.IUserNameProvider , System.String)'.
</ExceptionMessage>
<ExceptionType>Autofac.Core.DependencyResolutionException</ExceptionType>