After updating SimpleInjector from version 2.8.3 to v3.0.1, it returns an error when I try to pass the connection string into a constructor. This was working fine in the past but after the update something is wrong. Error:
A first chance exception of type 'System.ArgumentException' occurred in SimpleInjector.dll
Additional information: The constructor of type MwmJobUpdateNotifier contains parameter 'connectionString' of type String which can not be used for constructor injection.
Here is where the SimpleInjector container is configured:
public static Container Configure(Container container)
{
// Force assembly reference so Interfaces load correctly.
if (typeof(IJobRepository) != null)
container.RegisterAllInterfacesForClassesInAssemblyContaining<JobRepository>();
// Force assembly reference so Interfaces load correctly.
if (typeof(IGmcService) != null)
container.RegisterAllInterfacesForClassesInAssemblyContaining<GmcService>();
container.Options.AllowOverridingRegistrations = true;
container.Register<IMwmJobUpdateNotifier>(() => new MwmJobUpdateNotifier(container.GetInstance<IJobRepository>(),
ConfigurationManager.ConnectionStrings["Coordinate_DatabaseEntities"].ConnectionString));
container.Options.AllowOverridingRegistrations = false;
MWM.Service.DAL.Config.AGI.AGIIocConfig.Configure(container);
return container;
}
It looks like doesn't like the way I pass the two parameters into the constructor.
Edited: The exception is triggered within my ContainerException
class where all interfaces are registered:
public static Container RegisterAllInterfacesForClassesInAssemblyContaining<T>(this Container container, Lifestyle lifestyle = null) where T : class
{
var assembly = typeof(T).Assembly;
var registrations = assembly.GetExportedTypes()
.Where(type => type.IsClass &&
type.GetInterfaces()
.Except(type.GetInterfaces().SelectMany(x => x.GetInterfaces()))
.Except(type.BaseType.GetInterfaces())
.Any())
.Select(type => new
{
Services = type.GetInterfaces()
.Except(type.GetInterfaces().SelectMany(x => x.GetInterfaces()))
.Except(type.BaseType.GetInterfaces()),
Implementation = type
});
foreach (var registration in registrations)
{
foreach (var service in registration.Services)
{
if (registration.Implementation.IsGenericTypeDefinition)
{
if (lifestyle == null)
{
container.Register(service.GetGenericTypeDefinition(), registration.Implementation.GetGenericTypeDefinition());
}
else
{
container.Register(service.GetGenericTypeDefinition(), registration.Implementation.GetGenericTypeDefinition(), lifestyle);
}
}
else
{
if (lifestyle == null)
container.Register(service, registration.Implementation);
else
container.Register(service, registration.Implementation, lifestyle);
}
}
}
return container;
}
The exception is captured in the penultimate container.Register(...)
call.