autofac

How to inject two instances of same object using Autofac?

岁酱吖の 提交于 2019-12-05 12:26:44
I'm using Autofac constructor injection. I need to figure out how to inject a single object instance into more than one constructor argument, without needing to explicitly resolve each argument during the container setup phase. I have a complex scenario which would be simplified by this behavior; the following example is just a simplified scenario so I can demonstrate the behavior I'm looking for. Example: Say I have these two interfaces, IOpenable and ICloseable: public interface IOpenable { void Open(); } public interface ICloseable { void Close(); } And I have this Door class which

Autofac scope lifetime issue

懵懂的女人 提交于 2019-12-05 12:02:18
I have ASP.NET MVC application where I registered a component with an InstancePerHttpRequest scope. builder.RegisterType<Adapter>().As<IAdapter>().InstancePerHttpRequest(); then I have an async piece of code where I'm resolving the Adapter component. The following code is simplified Task<HttpResponseMessage> t = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t => // IHandleCommand<T> takes an IAdapter as contructor argument var h = DependencyResolver.Current.GetServices<IHandleCommand<T>>(); ); The code above is throwing an exception: The request lifetime scope cannot be created

Registering async factory in Autofac

99封情书 提交于 2019-12-05 10:53:41
I have a Wallet class that I get from a repository. I'm trying to properly register both in Autofac so classes using the wallet could have a proper instance injected. The problem is that the repository uses an async method (returning Task). Does Autofac support such cases? This doesn't work: cb.RegisterType<WalletRepository>() .As<IWalletRepository>() .SingleInstance(); cb.Register(async c => await c.Resolve<IWalletRepository>().CreateAsync(App.WalletPath)); cb.RegisterType<ViewModel>() .AsSelf(). .SingleInstance(); Somewhere in the app I just have: class ViewModel { public ViewModel(Wallet

Register Autofac decorator for only one generic command handler

半城伤御伤魂 提交于 2019-12-05 10:43:22
We have a lot of generic command handlers that are registered by Autofac in open-generic fashion. We have couple decorators that decorate all handles. Now I need to register a decorator for only one command handler and not affect all other command handlers. Here is my attempt on that, but I don't seem to get the registration right. Here is simple test code that is similar to our code: We have hundreds of commands working like this: class NormalCommand : ICommand { } // This command handler should not be decorated class NormalCommandHandler : ICommandHandler<NormalCommand> { public void Handle

ASP.NET Core 2.2 系列【三】AutoFac 仓储接口的依赖注入

主宰稳场 提交于 2019-12-05 09:14:30
一、准备工作 通过 程序包管理器控制台 安装AutoFac: Install-Package Autofac.Extensions.DependencyInjection 创建新类库(.NetCore 2.2类库),存放接口跟实现类,命名为NetCoreWebApi.Repository。 创建用户仓储接口 在类库项目上右键->添加->新建文件夹,命名为Interface,存放接口类。在Interface文件夹下面新建类:IUserRepository,属性如下: using System.Collections.Generic; using NetCoreWebApi.Model.Models; namespace NetCoreWebApi.Repository.Interface { /// <summary> /// 用户接口 /// </summary> public interface IUserRepository { /// <summary> /// 添加用户 /// </summary> /// <param name="entity">实体对象</param> int Add(TbUser entity); /// <summary> /// 删除用户 /// </summary> /// <param name="entity">实体对象</param>

How do I inject a constructor dependency into a ViewModel using Xamarin and Autofac?

泄露秘密 提交于 2019-12-05 08:19:17
I have a ViewModel and I want to inject another Class into it. I am using Visual Studio with the latest version of Xamarin. I'm using Autofac for registering en resolving dependencies. But I'm new to it and I'm facing a problem which I can't find the solution to, even though it's probably simple. This is the Class in which I want to inject another Class: public IMessagingCenterWrapper MessagingCenterWrapper; public LoginViewModel(IMessagingCenterWrapper messagingCenterWrapper){ MessagingCenterWrapper = messagingCenterWrapper; } Then in entry point of the app I have a function which initializes

Can I access the full power of Autofac in UnitTests, using the Moq integration

為{幸葍}努か 提交于 2019-12-05 08:09:52
My project (which happens built on top of Orchard , though I don't think that's relevant) uses Autofac . I am writing unit tests in which I want to stub out any dependencies using Moq , and I'm using the Autofac/Moq integration to achieve this. This is fine for any simple dependencies that are being passed in as constructor arguments. (Autofac documentation provides details of how to achieve this here ). But because I don't ever have a containerBuilder, I don't see how to use a lot of Autofac's power - lamda registration , registering generics , marking properties to be auto-wired . etc. Am I

Autofac Dependency Injection in Azure Function

白昼怎懂夜的黑 提交于 2019-12-05 07:49:29
I am trying to implement DI using Autofac IOC in Azure function. I need to build the container, but not sure where to put the code to build the container I think for now you would need to do something ugly like: public static string MyAwesomeFunction(string message) { if (MyService == null) { var instantiator = Initialize(); MyService = instantiator.Resolve<IService>(); } return MyService.Hello(message); } private static IService MyService = null; private static IContainer Initialize() { // Do your IoC magic here } I did write a blog entry for doing dependency injection with Autofac in Azure

How to make Autofac perform property injection in Orchard CMS

我是研究僧i 提交于 2019-12-05 06:50:42
Is it possible to do property injection with the OrchardCMS? I know that Orchard uses Autofac and that Autofac does do property injection, but I need to know how to do property injection for the IOrchardServices interface. Our team is looking at Orchard but our code base is all in ASP.NET 4.0 WebForms and so we will continue to serve aspx pages and slowly migrate those said pages into Orchard as time permits. With that, we'll need a way to get access to the OrchardServices object. I'm thinking that this is something I'd have to come up on my own. Does any one have any good examples of

Handling errors/exceptions in a mediator pipeline using CQRS?

筅森魡賤 提交于 2019-12-05 06:17:46
I'm trying to follow this post by Jimmy Bogard to implement a mediator pipeline so I can use pre/post request handlers to do some work. From the comments on that article I come to this github gist . I don't quite understand how to hook all of this up yet, so here is my first go. FYI - I'm using Autofac for DI and Web Api 2. Following CQRS, here is a query. public class GetAccountRequest : IAsyncRequest<GetAccountResponse> { public int Id { get; set; } } //try using fluent validation public class GetAccountRequestValidationHandler : AbstractValidator<GetAccountRequest>, IAsyncPreRequestHandler