autofac

How to use Property Injection with AutoFac?

為{幸葍}努か 提交于 2019-12-17 10:58:43
问题 In a Console application, I'm using Log4Net and in the Main method I'm getting the logger object. Now, I'd like to make this log object available in all my classes by letting all the classes inherit from a BaseClass which has a ILog property and is supposed to be set by Property Injection rather than Constructor Injection. I'm using AutoFac IoC container, how to inject my log Object to the Log property of my every class? What's the best/easiest way to achieve this? Is there any way to

How to resolve Autofac InstancePerHttpRequest

社会主义新天地 提交于 2019-12-17 10:46:33
问题 I have registered a component like this in my Global.asax.cs: ContainerBuilder builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerHttpRequest(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // This is where my error happens, not sure why? var workContext = container.Resolve<IWorkContext>(); WebWorkContext class:

how to resolve multiple dependencies of the same type in a System.Web.UI.Page where these dependencies implement a generic interface

三世轮回 提交于 2019-12-14 04:02:58
问题 In the BasePage.cs class I have the following IIndex property which should have two "or more" implementations of the INotificationService that can be accessed using a key. public class BasePage : Page { public IIndex<string, INotificationService<INotification>> NotificationServices { get; set; } public INotificationService<INotification> EmailService { get { return NotificationServices["emailService"]; } } public INotificationService<INotification> FaxService { get { return

Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'myContext' to type DataContext. error in Azure Function v2

牧云@^-^@ 提交于 2019-12-14 02:35:44
问题 Requirement: Create Azure Function which can inject Entity Framework context to Run method using dependency injection. Here is my Startup class [assembly: WebJobsStartup(typeof(Startup))] namespace MessagesToSqlDbFuncApp { internal class Startup : IWebJobsStartup { public void Configure(IWebJobsBuilder builder) => builder.AddDependencyInjection<ServiceProviderBuilder>(); } } Here is ServiceProviderBuilder class public class ServiceProviderBuilder : IServiceProviderBuilder { public

Autofac and DI for ValidationAttribute

假如想象 提交于 2019-12-13 22:01:07
问题 I have the following validation attribute class: public class ZipCodeValidationAttribute : ValidationAttribute { private readonly IValidationRepository _repository; public override bool IsValid(object value) { var repository = _repository; return repository.IsPostalCodeValid((string) value); } } To test I am trying to use Autofac as my IOC and use property injection. I've set up the test as follows: [TestMethod] public void When_PostalCodeAttribute_Given_ValidPostalCode_Then_SystemReturnsTrue

XAML designer “Could not load file or assembly Autofac 4.1” in App.xaml of UWP application using MVVMLight

只谈情不闲聊 提交于 2019-12-13 17:31:17
问题 Starting with a blank Universal Windows Application I added Autofac 4.1, Autofac.Extras.CommonServiceLocator 4.0 and MvvmLightLibs 5.3. I then created the following ViewModelLocator class. using Autofac; using Autofac.Extras.CommonServiceLocator; using GalaSoft.MvvmLight.Views; using Microsoft.Practices.ServiceLocation; namespace UwpTest { public class ViewModelLocator { public static IContainer Container { get; private set; } public static bool IsBuilt { get; set; } public ViewModelLocator()

Servicestack with Autofac not resolving IRequestContext

旧巷老猫 提交于 2019-12-13 17:17:29
问题 I am trying to use the Cache facilities of Service Stack. These are accessed through the RequestContext, which is injected by the IOC in your Service. This works as expected if you are using the default Funq IOC, it does not work when you hook AutoFac, RequestContext is null and I am not sure how to configure autofac to build it. Any clues here? My AutoFac configuration: var builder = new ContainerBuilder(); //Now register all dependencies to your custom IoC container builder

Autofac property injection with ValidationAttribute

主宰稳场 提交于 2019-12-13 14:02:43
问题 I've got a ValidationAttribute that looks like this: public class RegistrationUniqueNameAttribute : ValidationAttribute { public IRepository<User> UserRepository { get; set; } public override bool IsValid(object value) { //use UserRepository here.... } } In my container setup (in app start) I have this: builder.Register(c => new RegistrationUniqueEmailAttribute { UserRepository = c.Resolve<IRepository<User>>() }); However, when debugging, the value of UserRepository is always null, so the

Run-time registration with Autofac

北城余情 提交于 2019-12-13 11:52:37
问题 While discussing Autofac with a colleague, the issue of run-time registration of dependencies arose. In Prism, for instance, assemblies are frequently loaded at run time and their dependencies registered with the IoC container (usually Unity). How can this be accomplished with Autofac? From Autofac's documentation and what I've found on the web, it seems that registration is performed at application start. Even when "external" assemblies are used, the registrations are located in modules with

Autofac Registering Multiple Containers

梦想的初衷 提交于 2019-12-13 04:34:18
问题 I have a MVC application, and i am using Autofac to resolve dependencies. I have a situation where i have to create 2 containers and runtime should decide which container to use based on a condition. The condition is if the controller Home is called, i need to use container1, or else i have to use container2. Application_Start is the place where I register the container. I am not sure how to make this happen at runtime. Any help is highly appreciated. Thanks 回答1: One reason for letting