/// <summary> /// Allows components that are built by Simple Injector to depend on the (non-generic) /// <see cref="IStringLocalizer">Microsoft.Extensions.Localization.IStringLocalizer</see> abstraction. /// Components are injected with an contextual implementation. Using this method, application /// components can simply depend on <b>IStringLocalizer</b> instead of its generic counter part, /// <b>IStringLocalizer<T></b>, which simplifies development. /// </summary> /// <param name="options">The options.</param> /// <returns>The supplied <paramref name="options"/>.</returns> /// <exception cref="ArgumentNullException"> /// Thrown when <paramref name="options"/> is a null reference.</exception> /// <exception cref="InvalidOperationException">Thrown when no <see cref="IStringLocalizerFactory"/> /// entry can be found in the framework's list of services defined by <see cref="IServiceCollection"/>. /// </exception> /// <exception cref="ActivationException">Thrown when an <see cref="IStringLocalizer"/> is directly /// resolved from the container. Instead use <see cref="IStringLocalizer"/> within a constructor /// dependency.</exception> public static SimpleInjectorAddOptions AddLocalization(this SimpleInjectorAddOptions options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } EnsureMethodOnlyCalledOnce(options, nameof(AddLocalization), AddLocalizationKey); VerifyStringLocalizerFactoryAvailable(options.Services); // Cross-wire IStringLocalizerFactory explicitly, because auto cross-wiring might be disabled. options.Container.RegisterSingleton( () => options.GetRequiredFrameworkService <IStringLocalizerFactory>()); Type genericLocalizerType = GetGenericLocalizerType(); options.Container.RegisterConditional( typeof(IStringLocalizer), c => c.Consumer is null ? throw new ActivationException( "IStringLocalizer is being resolved directly from the container, but this is not " + "supported as string localizers need to be related to a consuming type. Instead, " + "make IStringLocalizer a constructor dependency of the type it is used in.") : genericLocalizerType.MakeGenericType(c.Consumer.ImplementationType), Lifestyle.Singleton, _ => true); return(options); }
/// <summary> /// Allows components that are built by Simple Injector to depend on the (non-generic) /// <see cref="ILogger">Microsoft.Extensions.Logging.ILogger</see> abstraction. Components are /// injected with an contextual implementation. Using this method, application components can simply /// depend on <b>ILogger</b> instead of its generic counter part, <b>ILogger<T></b>, which /// simplifies development. /// </summary> /// <param name="options">The options.</param> /// <returns>The supplied <paramref name="options"/>.</returns> /// <exception cref="ArgumentNullException"> /// Thrown when <paramref name="options"/> is a null reference.</exception> /// <exception cref="InvalidOperationException">Thrown when no <see cref="ILoggerFactory"/> entry /// can be found in the framework's list of services defined by <see cref="IServiceCollection"/>. /// </exception> public static SimpleInjectorAddOptions AddLogging(this SimpleInjectorAddOptions options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } EnsureMethodOnlyCalledOnce(options, nameof(AddLogging), AddLoggingKey); // Both RootLogger and Logger<T> depend on ILoggerFactory VerifyLoggerFactoryAvailable(options.Services); // Cross-wire ILoggerFactory explicitly, because auto cross-wiring might be disabled by the user. options.Container.RegisterSingleton(() => options.GetRequiredFrameworkService <ILoggerFactory>()); Type genericLoggerType = GetGenericLoggerType(); options.Container.RegisterConditional( typeof(ILogger), c => c.Consumer is null ? typeof(RootLogger) : genericLoggerType.MakeGenericType(c.Consumer.ImplementationType), Lifestyle.Singleton, _ => true); return(options); }