autofac

Can Autofac inject dependencies into layout view files?

家住魔仙堡 提交于 2020-01-21 02:50:12
问题 I'm trying to inject a dependency into the shared layout view page to avoid having to do it in every view that uses the layout. I've followed the guidance in the wiki for injecting dependencies into views, but the property is always null. Can Autofac inject properties into a custom view page that is a layout file? Here's my setup. CustomViewPage namespace MyApp { using System.Web.Mvc; public abstract class CustomViewPage : WebViewPage { public IHelper Helper { get; set; } } } ~/Views/Shared/

How to configure fluent nHibernate with MySQL

时光怂恿深爱的人放手 提交于 2020-01-20 17:08:27
问题 I'm trying to configure nHibernate to use a MySql database. I found examples for mssql and sqlite but none for mysql. So, how do I change this so it uses mysql: Fluently.Configure().Database( MsSqlConfiguration.MsSql2005.ConnectionString( c => c.FromConnectionStringWithKey("ConnectionString") ) ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>()) .BuildSessionFactory()) 回答1: Change MsSqlConfiguration.MsSql2005 , to MySqlConfiguration.Standard , it was the one thing I

从零开始在.NetCore3.1 Web项目中使用IOC

≡放荡痞女 提交于 2020-01-19 14:05:35
前言   现在的编程世界中,IOC已经成为了标配,毕竟解耦对于代码开发和维护都提升了很大的效率;而.NetCore中就自带了IOC功能,下面记录一下.NetCore Web项目中IOC的使用及如何集成AutoFac; 环境    开发环境: .NetCore3.1;注:需要安装对应的SDK版本; 项目创建   1. 选择模板 -- ASP.NET Core Web应用程序;   2. 添加两个项目DotNetCoreBll 和 DotNetCoreDal ,选模板的时候选择 类库(.Net Core);   最终项目结构   加这两个程序集的目的就是为了模拟业务处理,DotNetCoreBll 业务处理;DotNetCoreDal 获取数据; 设置DotNetCoreIoc 为启动项目运行,如下图   出现以上界面项目创建成功; 传统方式--自己New   1. 分别在DotNetCoreBll 和 DotNetCoreDal中增加 类 UserBll 和 UserDal , 里面代码如下:   类UserDal :      类UserBll:      2. 在HomeController中增加一个Api       然后,运行项目,访问 http://localhost:5000/Home/GetUserName 地址,运行如下:   综上, 可以看出

Resolve component in both tagged scope and untagged scope

假装没事ソ 提交于 2020-01-15 17:19:12
问题 I'm try to supply a different service to some tagged lifetime scopes in AutoFac, but can't seem to get the hang of it. I've tried using the custom lifetime from Instance per matching lifetime scope, with default? but that didn't work. I've written a test that illustrates what I'm trying to do: [TestMethod] public void NamedLifetimeTests() { var builder = new ContainerBuilder(); builder.Register(c => new ChildA()).As<Parent>().InstancePerLifetimeScope(); builder.Register(c => new ChildB()).As

Can an Autofac Decorator be registered to override existing registrations?

北慕城南 提交于 2020-01-15 11:21:48
问题 Given multiple types are registered with the same interface, Autofac uses the last type registered when resolving a component. An exception to this behavior seems to be with Autofac's decorator feature. Given a type is registered as IDoSomething either before or after the type is decorated using the standard process of registering a keyed type and then registering a decorator with the same key, it seems the instance returned is the non-decorated type. I'm running into this situation with a

netcore 2.2 使用 Autofac 实现自动注入

强颜欢笑 提交于 2020-01-15 09:24:03
Autofac自动注入 是通过名称约定来实现依赖注入 ps:本demo接口层都以“I”开头,以“Service”结尾。服务层实现都以“Service”结尾。 为什么要实现自动注入 大多时候,我们都是 以下方式进行依赖注入 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //初始化容器 var builder = new ContainerBuilder(); //管道寄居 builder.Populate(services); builder.RegisterType<TestService>().As<ITestService>();//UserService注入到IUserService //构造 ApplicationContainer = builder.Build(); //将AutoFac反馈到管道中 return new AutofacServiceProvider(ApplicationContainer); } 随着业务的增长,接口跟实现类会越来越多,还需要手动一个个的注册依赖项,有时候会出现忘了写配置,导致程序报错

using modules and config files in autofac

余生颓废 提交于 2020-01-15 07:23:06
问题 While I'm generally finding the Autofac documentation (on the wiki) to be helpful, the sections on XML configuration and modules is a bit unclear to me. Now, I have a sample working (which I present below), but I'm unsure whether it represents a sort of bastardized approach to configuration within the context of Autofac. In particular, I'm not sure if I have more or less of what I really need in the config files and the code files. Here's the code: using System; using System.IO; using Autofac

Asp.net Core AutoFac register generic using factory

家住魔仙堡 提交于 2020-01-15 04:58:07
问题 I'm using Asp.net Core with AutoFac and following the accepted answer here: Validation: How to inject A Model State wrapper with Ninject? This uses ninject. I don't understand how to do the equivalent of this ninject part in autoFac, specifically the kernel.Get : Func<Type, IValidator> validatorFactory = type => { var valType = typeof(Validator<>).MakeGenericType(type); return (IValidator)kernel.Get(valType); }; kernel.Bind<IValidationProvider>() .ToConstant(new ValidationProvider

Asp.net Core AutoFac register generic using factory

99封情书 提交于 2020-01-15 04:58:05
问题 I'm using Asp.net Core with AutoFac and following the accepted answer here: Validation: How to inject A Model State wrapper with Ninject? This uses ninject. I don't understand how to do the equivalent of this ninject part in autoFac, specifically the kernel.Get : Func<Type, IValidator> validatorFactory = type => { var valType = typeof(Validator<>).MakeGenericType(type); return (IValidator)kernel.Get(valType); }; kernel.Bind<IValidationProvider>() .ToConstant(new ValidationProvider

Mediatr: reducing number of DI'ed objects

亡梦爱人 提交于 2020-01-15 03:53:08
问题 I have a lot of commands and queries and most of them need same interfaces DI'ed to do different things. Is it possible to some how reduce this clutter that each and every one of my handler needs and it is repeated over and over? public class GetCoinByIdQueryHandler : IRequestHandler<GetCoinByIdQuery, CoinModel> { private readonly EventsContext context; private readonly ICacheClient cache; private readonly ILogger logger; private readonly IMapper mapper; private readonly Settings settings;