- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用我的应用程序配置 Autofac 时遇到了一些问题。我将不胜感激你的帮助。下面是有异常(exception)的代码。如果您需要更多详细信息,请随时发表评论。谢谢!
接口(interface)配置
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": "An error has occurred.", "exceptionMessage": "An error occurred when trying to create a controller of type 'AlertsController'. Make sure that the controller has a parameterless public constructor.", "exceptionType": "System.InvalidOperationException", "stackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()", "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 = AlertsController (ReflectionActivator), Services = [CRNET.API.Controllers.AlertsController], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> 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.) (See inner exception for details.)", "exceptionType": "Autofac.Core.DependencyResolutionException", "stackTrace": " at 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, IEnumerable
1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 parameters)\r\n at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable
1 parameters)\r\n at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance)\r\n at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable
1 parameters)\r\n at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)\r\n at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) in C:\projects\autofac-webapi\src\Autofac.Integration.WebApi\AutofacWebApiDependencyScope.cs:line 76\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)",
1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.b__5_0()\r\n at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func
"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 creator)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable
1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, 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, IEnumerable
1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)",
1 parameters)\r\n at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable
"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 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 parameters)" } } } }
最佳答案
Cannot resolve parameter 'CRNET.DAL.Models.CRNetContext context'...
您永远不会向 Autofac 注册类型 CRNetContext
。要修复它,请添加:
builder.RegisterType<CRNET.DAL.Models.CRNetContext>().AsSelf();
关于C# - IoC - WebAPI 2 上的 Autofac 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52432273/
我有一个泛型类( GenericClass ),它依赖于泛型类型( IGenericDependency )。这种依赖也是通用的。 public class GenericClass {
虽然我通常认为 Autofac 文档(在 wiki 上)很有帮助,但关于 XML 配置和模块的部分对我来说有点不清楚。现在,我有一个示例工作(我在下面介绍),但我不确定它是否代表了 Autofac 上
我是 autofac 的新手(使用 2.1.14.854),我仍在努力理解 我有一个接口(interface),这个接口(interface)有一个或多个实现,并且应该按特定顺序触发这些实现。 例如:
使用Autofac,可以做到这一点吗?我没有实例的类型。 var instance = container.Resolve("someStringKey"); 从一些谷歌搜索来看,这似乎不可能,但我想
我有一个进行数据处理的应用程序。有 class Pipeline { IEnumerable Filters {get; set;} 我将过滤器实现注册为 builder.RegisterType
我正在尝试将域事件引入项目中。 Udi Dahan 的帖子 - http://www.udidahan.com/2009/06/14/domain-events-salvation/ 中描述了这一概念
假设这个场景: public class B {}; public class C { public C(B b){} } 要从 Autofac 容器解析 C,我必须将 B 和 C 注册到容
我正在为第 3 方应用程序构建一个插件,我的插件使用 Autofac 来连接各种组件。容器是在应用程序启动时构建的,但主机应用程序稍后会调用我的命令。 调用命令时,主机应用程序会提供一些它定义的类型实
我有一个银行/集合,它在内存中缓存对象的实例,以便每个请求不需要返回到数据存储。我希望 Autofac 提供该银行的一个实例,但在 x 秒后使其过期,以便在下一个请求时创建一个新实例。我很难理解如何设
有人可以用简单的英语解释一下我放置问号的代码行的作用吗?或者给我指点一篇阐明这一点的文章。此代码用于在 autofac 容器中注册依赖项 var builder = new Autofac.Conta
我有以下类(class): public class Errors { private readonly string _connectionString; public Errors
假设所有依赖项都已在程序开始时注册。在程序的后面部分,如何使用 AutoFac 使用无参数构造函数创建一个新对象并将注册的属性注入(inject)到该对象中? 最佳答案 您可以使用 Propertie
我使用以下代码进行注册: builder.RegisterType().Named(".VCF"); builder.RegisterType().Named(".VCARD
我有一个工厂接口(interface)(连同具体实现): // foo.dll interface IFooProvider { T GetFoo() where T : Ba
我遇到的情况是,发布者和消费者位于同一个应用程序中。我正在使用 autofac。 据我了解,我需要 2 个具有 2 个端点的总线实例,一个用于发布者,一个用于订阅者。 我正在使用 autofac,但我
我正在使用 Autofac 构造函数注入(inject)。我需要弄清楚如何将单个对象实例注入(inject)多个构造函数参数,而不需要在容器设置阶段显式解析每个参数。 我有一个复杂的场景,可以通过这种
我们在我们的 PCL (Profile259) 中使用 Autofac 3.5.2,Nuget 告诉我们版本 4.1 可用,但当我们尝试更新它时,Nuget 失败并显示以下消息: “您正在尝试将此软件
我的情况是发布者和消费者坐在同一个应用程序中。我正在使用 autofac。 据我了解,我需要 2 个具有 2 个端点的总线实例,一个用于发布者,一个用于订阅者。 我正在使用 autofac,但我不知道
在 Autofac 2.1 的 beta 版本中,支持自动解析 Lazy如 Nicholas Blumhardt 的 Lazing Around with Autofac 中所述博客文章。 The c
我有这个类要在单元测试中实例化: public class Customer { internal Customer(Guid id) { // initialize prop
我是一名优秀的程序员,十分优秀!