Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity DI Container RegisterType method breaking changes from v5.8.x to v5.9.x

I was using Unity DI Container v5.8.4 on my .NET Core 2.1 project and I needed to register Mediator object and I was using the configuration suggested here.

Now I have updated to v5.9.4 and I have an error about RegisterType method arguments:

Cannot convert from 'Unity.Lifetime.LifetimeManager' to 'Unity.Injection.InjectionMember'

This is my actual code:

public static IUnityContainer RegisterMediator(this IUnityContainer container, LifetimeManager lifetimeManager)
{
    return container.RegisterType<IMediator, Mediator>(lifetimeManager)
        .RegisterInstance<ServiceFactory>(type =>
        {
            var enumerableType = type
                .GetInterfaces()
                .Concat(new[] { type })
                .FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));

            return enumerableType != null
                ? container.ResolveAll(enumerableType.GetGenericArguments()[0])
                : container.IsRegistered(type)
                    ? container.Resolve(type)
                    : null;
        });
}

What have I to do to update the registration code?

like image 883
Cheshire Cat Avatar asked Mar 04 '23 12:03

Cheshire Cat


1 Answers

They have changed the signature of the RegisterType in this PR and it is now taking a ITypeLifetimeManager instead of a LifetimeManager.

The HierarchicalLifetimeManager is now implementing the ITypeLifetimeManager interface so you just need to update the lifetimeManager parameter in your RegisterMediator method:

public static IUnityContainer RegisterMediator(this IUnityContainer container, 
                                                    ITypeLifetimeManager lifetimeManager)
{
    return container.RegisterType<IMediator, Mediator>(lifetimeManager)
        ...
}
like image 144
nemesv Avatar answered May 05 '23 05:05

nemesv