SOfanatic
SOfanatic

Reputation: 5573

unity container class with different lifetimes

I'm currently doing the following, inside my UnityConfig.cs which get's called by the UnityMvcActivator at App_Start:

container.RegisterType<IUserDal, UserDal>(new PerRequestLifetimeManager());

I'm also doing this:

container.RegisterType<IEmailObserver, EmailObserver>(new ContainerControlledLifetimeManager());

I should've mentioned that IUserDal is in the constructor of IEmailObserver:

public EmailObserver(IUserDal userDal);

now I want to start the EmailObserver instance when the application starts, so I have this:

`DependencyResolver.SetResolver(new UnityDependencyResolver(container));

container.Resolve<IEmailObserver>();//this line fails

The error message I get is:

Exception is: InvalidOperationException - The PerRequestLifetimeManager can only be used in the context of an HTTP request. Possible causes for this error are using the lifetime manager on a non-ASP.NET application, or using it in a thread that is not associated with the appropriate synchronization context.

I'm trying to implement the Observer Pattern but I want to use Unity to register my observers and initialize them when the app starts.

Upvotes: 1

Views: 558

Answers (1)

tym32167
tym32167

Reputation: 4881

Check EmailObserver constructor - all types from there should be singletones too, I think.

Upvotes: 1

Related Questions