private static void CrossWireServiceScope(Container container, IApplicationBuilder builder)
        {
            if (container.Options.DefaultScopedLifestyle == null)
            {
                throw new InvalidOperationException(
                          "To be able to cross-wire a service with a transient or scoped lifestyle, " +
                          "please ensure that the container is configured with a default scoped lifestyle by " +
                          "setting the Container.Options.DefaultScopedLifestyle property with the required " +
                          "scoped lifestyle for your type of application. In ASP.NET Core, the typical " +
                          $"lifestyle to use is the {nameof(AsyncScopedLifestyle)}. " +
                          "See: https://simpleinjector.org/lifestyles#scoped");
            }

            if (container.GetItem(ServiceScopeKey) == null)
            {
                var scopeFactory = builder.ApplicationServices.GetRequiredService <IServiceScopeFactory>();

                // We use unregistered type resolution, to allow the user to register IServiceScope manually
                // if he needs.
                container.ResolveUnregisteredType += (s, e) =>
                {
                    if (e.UnregisteredServiceType == typeof(IServiceScope) && !e.Handled)
                    {
                        e.Register(Lifestyle.Scoped.CreateRegistration(scopeFactory.CreateScope, container));
                    }
                };

                container.SetItem(ServiceScopeKey, new object());
            }
        }
        /// <summary>
        /// Enables ASP.NET Core services to be cross-wired in the Container. This method should be called
        /// in the <b>ConfigureServices</b> method of the application's <b>Startup</b> class. When cross-wiring
        /// is enabled, individual cross-wire registrations can be made by calling
        /// <see cref="CrossWire{TService}(Container, IApplicationBuilder)"/>.
        /// </summary>
        /// <param name="services">The ASP.NET application builder instance that references all
        /// framework components.</param>
        /// <param name="container">The container.</param>
        public static void EnableSimpleInjectorCrossWiring(this IServiceCollection services, Container container)
        {
            Requires.IsNotNull(services, nameof(services));
            Requires.IsNotNull(container, nameof(container));

            if (container.GetItem(CrossWireContextKey) == null)
            {
                container.SetItem(CrossWireContextKey, services);
            }
        }
        /// <summary>Registers a <see cref="IFilterProvider"/> that allows filter attributes to go through the
        /// Simple Injector pipeline (https://simpleinjector.org/pipel). This allows any registered property to be
        /// injected if a custom <see cref="IPropertySelectionBehavior"/> in configured in the container, and
        /// allows any<see cref="Container.RegisterInitializer">initializers</see> to be called on those
        /// attributes.
        /// <b>Please note that attributes are cached by Web API, so only dependencies should be injected that
        /// have the singleton lifestyle.</b>
        /// </summary>
        /// <param name="container">The container that should be used.</param>
        /// <param name="configuration">The <see cref="HttpConfiguration"/>.</param>
        /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null
        /// reference (Nothing in VB).</exception>
        public static void RegisterWebApiFilterProvider(this Container container, HttpConfiguration configuration)
        {
            Requires.IsNotNull(container, "container");
            Requires.IsNotNull(configuration, "configuration");

            configuration.Services.RemoveAll(typeof(IFilterProvider),
                                             provider => provider is ActionDescriptorFilterProvider);

            var filterProvider = new SimpleInjectorActionDescriptorFilterProvider(container);

            configuration.Services.Add(typeof(IFilterProvider), filterProvider);

            container.SetItem(typeof(SimpleInjectorActionDescriptorFilterProvider), filterProvider);
        }
        /// <summary>
        /// Enables ASP.NET Core services to be cross-wired in the Container. This method should be called
        /// in the <b>ConfigureServices</b> method of the application's <b>Startup</b> class. When cross-wiring
        /// is enabled, individual cross-wire registrations can be made by calling
        /// <see cref="CrossWire{TService}(Container, IApplicationBuilder)"/>.
        /// </summary>
        /// <param name="services">The ASP.NET application builder instance that references all
        /// framework components.</param>
        /// <param name="container">The container.</param>
        public static void EnableSimpleInjectorCrossWiring(this IServiceCollection services, Container container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (container.GetItem(CrossWireContextKey) == null)
            {
                container.SetItem(CrossWireContextKey, services);
            }
        }
Esempio n. 5
0
        // This method will never return null.
        internal static LifetimeScopeManager GetLifetimeScopeManager(this Container container)
        {
            var manager = (LifetimeScopeManager)container.GetItem(ManagerKey);

            if (manager == null)
            {
                lock (ManagerKey)
                {
                    manager = (LifetimeScopeManager)container.GetItem(ManagerKey);

                    if (manager == null)
                    {
                        manager = new LifetimeScopeManager();
                        container.SetItem(ManagerKey, manager);
                    }
                }
            }

            return(manager);
        }