【发布时间】:2019-02-25 04:30:00
【问题描述】:
我在使用我的应用程序配置 Autofac 时遇到了一些问题。我将感谢您的所有帮助。下面是有例外的代码。如果您需要更多详细信息,请随时发表评论。谢谢!
-
API 配置
public static void Register(HttpConfiguration config) { // Web API configuration and services var setings = config.Formatters.JsonFormatter.SerializerSettings; setings.ContractResolver = new CamelCasePropertyNamesContractResolver(); setings.Formatting = Formatting.Indented; // Web API Cors var origin = "https://localhost:44381"; EnableCorsAttribute cors = new EnableCorsAttribute(origin, "*", "*"); config.EnableCors(cors); // DI var builder = new ContainerBuilder(); builder.RegisterType<AlertRepository>() .As<IAlertRepository>(); builder.RegisterType<UnitOfWork>() .As<IUnitOfWork>(); builder.RegisterGeneric(typeof(Repository<>)) .As(typeof(IRepository<>)) .InstancePerRequest(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } -
工作单元类
public class UnitOfWork : IUnitOfWork { private readonly CRNetContext _context; public UnitOfWork(CRNetContext context) { _context = context; Alerts = new AlertRepository(_context); } public IAlertRepository Alerts { get; private set; } public int Complete() { return _context.SaveChanges(); } public void Dispose() { _context.Dispose(); } } 异常
{ "message": "发生错误。", "exceptionMessage": "尝试创建 'AlertsController' 类型的控制器时发生错误。请确保控制器具有无参数的公共构造函数。", "exceptionType": "System.InvalidOperationException", "stackTrace": " 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,类型 controllerType)\r\n 在 System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage 请求)\r\ n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()", “内部异常”:{ "message": "发生错误。", "exceptionMessage": "在激活特定注册过程中发生错误。有关详细信息,请参阅内部异常。注册:Activator = AlertsController (ReflectionActivator),Services = [CRNET.API.Controllers.AlertsController],Lifetime = Autofac.Core。 Lifetime.CurrentScopeLifetime,Sharing = None, Ownership = OwnedByLifetimeScope ---> 激活特定注册时出错。详情见内部异常注册:Activator = UnitOfWork(ReflectionActivator), Services = [CRNET.DAL.Persistance .IUnitOfWork], Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> 在“CRNET.DAL.Persistance”类型上找不到使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数.UnitOfWork' 可以使用可用的服务和参数调用:\r\n无法解析构造函数 'Void .ctor(CRNET.DAL.Models.CRNet) 的参数 'CRNET.DAL.Models.CRNetContext context'语境)'。 (详情请参阅内部异常。)(详情请参阅内部异常。)", "exceptionType": "Autofac.Core.DependencyResolutionException", "stackTrace": " 在 Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 参数)\r\n 在 Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration 注册,IEnumerable1 parameters)\r\n at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 参数)\r \n 在 Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance)\r\n at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable1 参数)\r\n 在 Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)\r\n 在 Autofac.Integration.WebApi。 AutofacWebApiDependencyScope.GetService(Type serviceType) 在 C:\projects\autofac-webapi\src\Autofac.Integration.WebApi\AutofacWebApiDependencyScope.cs:line 76\r\n 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求, 类型 controllerType, Func1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)", "innerException": { "message": "An error has occurred.", "exceptionMessage": "An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = UnitOfWork (ReflectionActivator), Services = [CRNET.DAL.Persistance.IUnitOfWork], Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'CRNET.DAL.Persistance.UnitOfWork' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'CRNET.DAL.Models.CRNetContext context' of constructor 'Void .ctor(CRNET.DAL.Models.CRNetContext)'. (See inner exception for details.)", "exceptionType": "Autofac.Core.DependencyResolutionException", "stackTrace": " at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 参数)\r\n 在 Autofac.Core.Resolving.InstanceLookup.b__5_0()\r\n 在 Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func1 creator)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1参数)\r\n 在Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration 注册,IEnumerable1 parameters)\r\n at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass0_0.<CanSupplyValue>b__0()\r\n at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()\r\n at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 参数)\r\n 在 Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)", "innerException": { "message": "An error has occurred.", "exceptionMessage": "None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'CRNET.DAL.Persistance.UnitOfWork' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'CRNET.DAL.Models.CRNetContext context' of constructor 'Void .ctor(CRNET.DAL.Models.CRNetContext)'.", "exceptionType": "Autofac.Core.DependencyResolutionException", "stackTrace": " at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext context, IEnumerable1 参数)\r\n 在 Autofac.Core .Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext上下文,IEnumerable1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1参数)" } } } }
【问题讨论】:
标签: c# .net dependency-injection autofac