autofac

asp.net core3.0 mvc 用 autofac

穿精又带淫゛_ 提交于 2019-11-30 15:08:38
原文: asp.net core3.0 mvc 用 autofac 好久没有写文章了,最近在用.net core3.0,一些开发中问题顺便记录; 1.首先nuget引入 Autofac Autofac.Extensions.DependencyInjection 2.修改Program.cs 添加.UseServiceProviderFactory(new AutofacServiceProviderFactory()) public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } 3.修改Startup.cs 添加 services

asp.net core3.0 mvc 用 autofac

旧城冷巷雨未停 提交于 2019-11-30 15:08:10
好久没有写文章了,最近在用.net core3.0,一些开发中问题顺便记录; 1.首先nuget引入 Autofac Autofac.Extensions.DependencyInjection 2.修改Program.cs 添加.UseServiceProviderFactory(new AutofacServiceProviderFactory()) public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } 3.修改Startup.cs 添加 services.AddControllersWithViews()

ASP.NET MVC 3, Action Filters, and Autofac Dependency Injection

◇◆丶佛笑我妖孽 提交于 2019-11-30 14:33:14
On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate transaction before executing the action and commits or rolls it back afterward, depending on whether or not an exception was thrown. The ISession instance is HttpRequestScoped() and injected by Autofac . It looks like this and works great: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public sealed class TransactionAttribute : ActionFilterAttribute { private ITransaction transaction; public TransactionAttribute() { this.Order = 0; } public ISession Session { get; set; } public

Autofac composite pattern

核能气质少年 提交于 2019-11-30 14:07:43
I noticed I quite often need to implement composite pattern. For example: interface IService { ... } class Service1 : IService { ... } class Service2 : IService { ... } class CompositeService : IService { public CompositeService(IEnumerable<IService> services) { ... } ... } I want to register CompositeService as IService in container and have dependencies injected. (looks somewhat similar to Decorator but decorating set of services instead of only one) What's the best way to do it in autofac? How would ideal solution look like (for C#)? Update: My current registration is: builder.RegisterType

DI/IoC Container Performance Benchmark Comparison?

萝らか妹 提交于 2019-11-30 13:49:57
问题 I've found some 2008 benchmark results for testing the performance of several of the top .NET DI/IoC containers here. But I haven't been able to find any updated results. Are there any benchmarks out there that compare some of the big IoC containers (StructureMap, Unity, Ninject, Autofac, Castle Windsor, etc.)? 回答1: I would not recommend using performance benchmarks to pick an IoC container. There are many, many more important factors, such as feature set, development roadmap and

Getting autofac to work with mvc6 beta5

烂漫一生 提交于 2019-11-30 13:49:11
I am trying to get autofac working with an mvc6 application I am working on. I found this blog article however it seems to be a little dated. It looks like its using the beta3 bits I am using this clr version 1.0.0-beta5-11911 My project has these 2 references "Autofac": "4.0.0-alpha2", "Autofac.Dnx": "4.0.0-alpha2", Within the article is talks about how to modify the startup.cs // Create the Autofac container builder. var builder = new Autofac.ContainerBuilder(); // Add any Autofac modules or registrations. builder.RegisterModule(new AutofacModule()); // Populate the services. builder

Caliburn.Micro. Automatically call eventaggregator.Subscribe() for IHandle implementors with Autofac

纵饮孤独 提交于 2019-11-30 13:49:00
In Caliburn.Micro documentation the authors mention such possibility: documentation link IHandle inherits from a marker interface IHandle. This allows the use of casting to determine if an object instance subscribes to any events. This enables simple auto-subscribing if you integrate with an IoC container. Most IoC containers (including the SimpleContainer) provide a hook for being called when a new instance is created. Simply wire for your container’s callback, inspect the instance being created to see if it implement IHandle, and if it does, call Subscribe on the event aggregator. How is it

Autofac runtime parameters

馋奶兔 提交于 2019-11-30 13:25:51
问题 I'm new to autofac and looking to see the best practices on passing runtime values to constructor. I've read a bunch of stackoverflow questions where this is asked but none are fully fleshed out. Should we be using delegates, factory to create service etc. I know passing the container around is not the best way to accomplish this. In my particular case I have a service that access multiple dependencies, say logging, dataprovider, etc. Along with the few services being passed I also have run

How do I resolve Web API controllers using Autofac in a mixed Web API and MVC application?

删除回忆录丶 提交于 2019-11-30 13:11:33
问题 Hi I have an MVC application where I have defined some dependencies to my Web API. public class AutofacWebApiDependenceResolver : IDependencyResolver { private readonly IComponentContext container; public AutofacWebApiDependenceResolver(IContainer container) { if (container == null) { throw new ArgumentNullException("container"); } this.container = container; } public object GetService(Type serviceType) { if (serviceType == null) { throw new ArgumentNullException("serviceType"); } var ret =

Customizing Autofac's component resolution / Issue with generic co-/contravariance

こ雲淡風輕ζ 提交于 2019-11-30 12:00:28
问题 First, sorry for the vague question title. I couldn't come up with a more precise one. Given these types: { TCommand : ICommand } «interface» «interface» / +-----------+ +----------------------/----+ | ICommand | | ICommandHandler<TCommand> | +-----------+ +---------------------------+ ^ | Handle(command: TCommand) | | +---------------------------+ | ^ | | +------------+ +-------------------+ | FooCommand | | FooCommandHandler | +------------+ +-------------------+ ^ | +-------------------+ |