Unity Auto Registration specifying a custom lifetime manager with <T>

Sep 20, 2012 at 12:08 PM

I am  having challenges  using auto registration  with  a custom lifetime  manager with <T> . Below is  my  code 

            IUnityContainer myContainer= new UnityContainer()
                .AddExtension(new EnterpriseLibraryCoreExtension());

          myContainer
                .ConfigureAutoRegistration()
                .ExcludeSystemAssemblies()
                .Include(a => a.AssemblyQualifiedName != null && a.AssemblyQualifiedName.Contains("ShortLoans"),
                         Then.Register()
                             .UsingLifetime(a => new HttpContextLifetimeManager<>())) // Default registration action
                .ApplyAutoRegistration();
            return myContainer;

 

The limeftime manager class is  like  :

 

public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable

{

}

The challange I  have is  to pass  the  type on  the HttpContextLifetimeManager . I  have been  challanged  for the  past  2  days  and i will  really  appreciate  any  help  towards the  right  direction.

Coordinator
Sep 20, 2012 at 12:49 PM

Sorry, I didn't get the question. Could you please provide more detail on what you mean by 'pass  the  type on  the HttpContextLifetimeManager'?

Sep 20, 2012 at 12:59 PM

 

Thanks  for  your  response. As i  said  above  my UsingLifetime(  HttpContextLifetimeManager<T> )  requires a  type  at  runtime   so  i was  wondering  how  to  create a new instance of the lifetime manager making  use of the  UsingLifetime like  below. The line below  doesn't compile.

.Include(a => a.AssemblyQualifiedName != null && a.AssemblyQualifiedName.Contains("ShortLoans"),
                         Then.Register()
                             .UsingLifetime(a => new HttpContextLifetimeManager<a>())) // Default registration action

Coordinator
Sep 20, 2012 at 1:21 PM

This should help:

.Include(a => a.AssemblyQualifiedName != null && a.AssemblyQualifiedName.Contains("ShortLoans"),
                         Then.Register()
                             .UsingLifetime(a => (LifetimeManager)Activator.CreateInstance(typeof(HttpContextLifetimeManager<>).MakeGenericType(a)))

Doesn't have to do anything with auto registration though.

Sep 20, 2012 at 3:08 PM

Thanks  very  much for  your  assistance ,It  has  worked .I  really  appreaciate it  and you  have  saved  more  after  a long  struggle.