Here is another way to allow unregistered specific types from a container. Please note that all autosamplers that search and select logic, all registration event handlers remain valid.
First you define this method:
public static object ResolveUnregistered(this IComponentContext context, Type serviceType, IEnumerable<Parameter> parameters) { var scope = context.Resolve<ILifetimeScope>(); using (var innerScope = scope.BeginLifetimeScope(b => b.RegisterType(serviceType))) { IComponentRegistration reg; innerScope.ComponentRegistry.TryGetRegistration(new TypedService(serviceType), out reg); return context.ResolveComponent(reg, parameters); } }
The idea is that you get the component registration from the derived context and enable it in the current context. Then you can create some convenient overloads:
public static object ResolveUnregistered(this IComponentContext context, Type serviceType) { return ResolveUnregistered(context, serviceType, Enumerable.Empty<Parameter>()); } public static object ResolveUnregistered(this IComponentContext context, Type serviceType, params Parameter[] parameters) { return ResolveUnregistered(context, serviceType, (IEnumerable<Parameter>)parameters); } public static TService ResolveUnregistered<TService>(this IComponentContext context) { return (TService)ResolveUnregistered(context, typeof(TService), Enumerable.Empty<Parameter>()); } public static TService ResolveUnregistered<TService>(this IComponentContext context, params Parameter[] parameters) { return (TService)ResolveUnregistered(context, typeof(TService), (IEnumerable<Parameter>)parameters); }
source share