autofac

How to carry out custom initialisation with autofac

泄露秘密 提交于 2019-12-10 17:21:31
问题 I'm adding autofac to an existing project and some of the service implementations require their Initialize method to be called and passed configuration information. Currently I'm using the code: builder.Register(context => { var service = new SqlTaxRateProvider(context.Resolve<IUserProvider>()); service.Initialize(config); return service; } ).As<ITaxService>() .SingleInstance(); which works but I'm still creating the object myself which is what I'm trying to get away from this and allow

How to enable property injection on controllers using Autofac and ASP.NET Core?

浪尽此生 提交于 2019-12-10 17:14:43
问题 There doesn't really seem to be an easy way to make sure the controllers have property injection. A way around it would be to register all controllers individually, which seems to defeat the purpose a bit. The [FromServices] attribute is removed and they specifically mention it should be up to the individual IoC containers to make sure this happens. Am I missing something glaringly obvious? Looked for all available extension methods but didn't find anything, same for issues and the discussion

Autofac and Web API scopes

泪湿孤枕 提交于 2019-12-10 16:27:56
问题 Earlier i saw this: .InstancePerApiRequest(); Now i only got this: .InstancePerHttpRequest(); Did autofac removed the API-scope? I have the reference Autofac.Integration.WebApi but this extension is not available. What is the difference between InstancePerHttpRequest and InstancePerApiRequest? 回答1: InstancePerApiRequest is part of the Web API integration, and InstancePerHttpRequest is part of the MVC integration. These both actually apply the same tag to the lifetime scope. That was done

manually invoke property injection with AutoFac

穿精又带淫゛_ 提交于 2019-12-10 16:19:15
问题 Suppose all the dependencies have already been registered at the beginning of the program. At later points in the program how can you use AutoFac to create a new object with a parameterless constructor and inject the registered properties into the object? 回答1: You can register your object in the container with PropertiesAutowired then use resolve when you need an instace: ContainerBuilder builder = new ContainerBuilder(); // builder register other dependencies builder.RegisterType<MyObject>()

Autofac, MediatR & multiple DLL projects

前提是你 提交于 2019-12-10 16:13:17
问题 I have several (eventually 100+) small DLL projects all based on MediatR. This means that the interfaces in use are just the IMediatR interfaces ( IRequest<TResult>, IRequestHandler<IRequest<TResult>, TResult> ). Since a lot of these do not have a UI and are called via orchestration from another DLL I was thinking I could create an Autofac Container project (DLL), register all the micro-services, then resolve what I need at runtime in another app that consumes my container. So far, so good.

Autofac SingleInstance not working

孤人 提交于 2019-12-10 15:49:46
问题 I am trying to get a Singleton instance working with Autofac. I'm kind of doing a quasi-mvvm type thing with Winforms, just an experiment so don't get to hung up on that. But I am trying you have my model be a single instance with a reference in a command (ICommand here is not the WPF variety): I have the following setup of a container: var cb = new ContainerBuilder(); cb.RegisterType<CalculateCommissionCommand>().As<ICommand<TradeEntry>>().SingleInstance(); cb.RegisterType<CalculationsModel>

Cannot register a struct instance with autofac

南楼画角 提交于 2019-12-10 15:49:30
问题 I just started moving to Autofac from Unity and I have a problem when trying to register an instance. public static void Register(ContainerBuilder containerBuilder, CancellationToken shutDownCancellationToken) { containerBuilder.RegisterType<CancellationToken>(); containerBuilder.RegisterInstance(shutDownCancellationToken); } I get the following error: The type ' CancellationToken ' must be a reference type in order to use it as parameter ' T ' in the generic type or method '

autofac - dependency injection into IHttpModule

大兔子大兔子 提交于 2019-12-10 15:42:55
问题 New MVC 4 web application using autofac 3.0 on IIS 7.5. How do I inject a dependency into an IHttpModule? I tried constructor injection which resulted in: Constructor on type 'AnonymousIdentityModule' not found So it seems the internals require a parameterless constructor for http modules. I also tried property injection too but that resulted in no dependency actually being injected. Registration builder.RegisterType<AnonymousIdentityModule>().As<IHttpModule>().PropertiesAutowired()

Method-level attributed interception with Autofac

二次信任 提交于 2019-12-10 15:33:56
问题 (this is a related question to this one which is for SimpleInjector. I was recommended to create separate questions for each IoC container.) With Unity, I'm able to quickly add an attribute based interception like this public sealed class MyCacheAttribute : HandlerAttribute, ICallHandler { public override ICallHandler CreateHandler(IUnityContainer container) { return this; } public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { // grab from cache if I have it,

Autofac: Registering an Async Factory method

点点圈 提交于 2019-12-10 15:15:51
问题 TL;DR: Does Autofac support something like AutoFixture's fixture.Get() mechanism ? I'm using Autofac and need to invoke async factory methods which look like this: class AppModel { public static async Task<AppModel> CreateAsync(IDependency x, IDependency2 y) { ... } } What is the simplest way for me to execute such a method and have the arguments be supplied by Autofac ? i.e., I want to be able to do something like: Task<AppModel> creationTask = <some autofaccery>(AppModel.CreateAsync); var