autofac

Autofac attribute injection failing on attributes

坚强是说给别人听的谎言 提交于 2019-12-19 05:19:26
问题 I've found a few questions on this, but they tend to point to the exact documentation I'm following... but it's still not working. I'm building a fairly simple ASP.NET MVC 4 site, and the plan is to use ActionFilterAttribute -based logging. I have a DataAccessProvider class which opens transactions with the database and provides unit-of-work instances, and I'm trying to inject it into the filter attribute. The documentation says that it's enough to just call RegisterFilterProvider() , and

Autofac attribute injection failing on attributes

久未见 提交于 2019-12-19 05:18:12
问题 I've found a few questions on this, but they tend to point to the exact documentation I'm following... but it's still not working. I'm building a fairly simple ASP.NET MVC 4 site, and the plan is to use ActionFilterAttribute -based logging. I have a DataAccessProvider class which opens transactions with the database and provides unit-of-work instances, and I'm trying to inject it into the filter attribute. The documentation says that it's enough to just call RegisterFilterProvider() , and

.net core 3.1 Autofac自动注入

℡╲_俬逩灬. 提交于 2019-12-19 04:25:40
.Net core 3.+ 使用 Autofac 完成自动注册 public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddControllers() .AddControllersAsServices(); //属性注入必须加上这个 } public void ConfigureContainer(ContainerBuilder builder) { //获取所有控制器类型并使用属性注入 var controllerBaseType = typeof(ControllerBase); builder.RegisterAssemblyTypes(typeof(Program).Assembly) .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType) .PropertiesAutowired(); } Controller 中使用 public IAccount account { get; set; } 搞定 来源: CSDN 作者: qq_32753255 链接: https://blog.csdn.net/qq

Passing in the type of the declaring class for NLog using Autofac

青春壹個敷衍的年華 提交于 2019-12-19 03:37:10
问题 Following on from this question I would like autofac to inject the type of the declaring object into the constructor of my NLog service, so that it can correctly log which type is logging entries. My NLogService class looks like this... public class NLogService : ILogService { private readonly Logger _logger; public NLogService(Type t) { var consumerType = t.DeclaringType.FullName; _logger = LogManager.GetLogger(consumerType); } However it fails on app startup because it obviously cannot work

Managing AutoFac lifetime scopes per session and request in asp.net mvc 3

耗尽温柔 提交于 2019-12-19 03:22:35
问题 I want to use AutoFac in a web application. I have the root container, a child container per session and a child container per request. I'm trying to figure out what the/a best way is to manage these lifetime scopes. In the Global.asax.cs I have added the following: protected void Application_Start(object sender, EventArgs e) { var container = ...; } protected void Session_Start(object sender, EventArgs e) { var sessionScope = container.BeginLifetimeScope("session"); Session["Autofac

Instance per matching lifetime scope, with default?

折月煮酒 提交于 2019-12-18 16:53:15
问题 I'd like to have an instance per matching lifetime scoped registration in Autofac, but occasionally need to request an instance from a global container (where there is no matching lifetime scope). In scenarios where no matching lifetime scope exists, I want to give a top-level instance instead of throwing an exception. Is this possible? 回答1: I think you'd better extend Autofac by introducing a new lifetime option. I took the Autofac sources and modified them a bit: public static class

Inject DbContext with Autofac

橙三吉。 提交于 2019-12-18 15:16:16
问题 I have the following EntityFramework context: public class Context : DbContext, IDbContext { } Where IDbContext is the following: public interface IDbContext { DbEntityEntry Entry(Object entity); IEnumerable<DbEntityValidationResult> GetValidationErrors(); Int32 SaveChanges(); Task<Int32> SaveChangesAsync(); Task<Int32> SaveChangesAsync(CancellationToken cancellationToken); DbSet Set(Type entityType); DbSet<TEntity> Set<TEntity>() where TEntity : class; } // IDbContext What is the correct way

Autofac - resolving runtime parameters without having to pass container around

微笑、不失礼 提交于 2019-12-18 12:10:09
问题 I have a simpler "ServiceHelper" class that takes two parameters in the constructor: public ServiceHelper(ILogger<ServiceHelper> log, string serviceName) (ILogger generic wrapper for NLog that Autofac is providing just fine, and the serviceName is the name of a Windows service to control that I need to provide at runtime.) I'm having trouble wrapping my head around how to create new instances of this class at runtime passing in different service names, using Autofac. Something like this doesn

Managing NHibernate ISession with Autofac

此生再无相见时 提交于 2019-12-18 10:36:32
问题 Does anyone have any tips or best practices regarding how Autofac can help manage the NHibernate ISession Instance (in the case of an ASP.NET MVC application)? 回答1: I'm not overly familiar with how NHibernate sessions should be handled. That said, Autofac have excellent instance lifetime handling (scoping and deterministic disposal). Some related resources are this article and this question. Since you're in ASP.Net MVC land make sure you also look into the MVC integration stuff. To illustrate

Binding autofac with webapi using generic repository

蹲街弑〆低调 提交于 2019-12-18 09:03:20
问题 I am trying to use autofac with a repository and I am trying to add a little generics to try reducing the amount of duplicate code I am writing.However I am going round in circles trying to get autofac to work for me So I created a domainservice and interface that handles our the standard crud operations public class DomainService<T>:IDomainService<T> { protected readonly IDomainService<T> Repository; public DomainService(IDomainService<T> repository) { Repository = repository; } public