/// <summary>
        /// Registers the Web API <see cref="IHttpController"/> types that available for the application. This
        /// method uses the configured <see cref="IHttpControllerTypeResolver"/> to determine which controller
        /// types to register.
        /// </summary>
        /// <param name="container">The container the controllers should be registered in.</param>
        /// <param name="configuration">The <see cref="HttpConfiguration"/> to use to get the Controller
        /// types to register.</param>
        /// <param name="assemblies">The assemblies to search.</param>
        /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null
        /// reference (Nothing in VB).</exception>
        public static void RegisterWebApiControllers(this Container container, HttpConfiguration configuration,
                                                     IEnumerable <Assembly> assemblies)
        {
            Requires.IsNotNull(container, nameof(container));
            Requires.IsNotNull(configuration, nameof(configuration));
            Requires.IsNotNull(assemblies, nameof(assemblies));

            IAssembliesResolver assembliesResolver = new AssembliesResolver(assemblies);

            var controllerTypes = GetControllerTypesFromConfiguration(configuration, assembliesResolver);

            foreach (Type controllerType in controllerTypes)
            {
                Registration registration = Lifestyle.Transient.CreateRegistration(controllerType, container);

                if (typeof(ApiController).IsAssignableFrom(controllerType))
                {
                    // Suppress the Disposable Transient Component warning, because Web API's controller factory
                    // ensures correct disposal of controllers.
                    registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent,
                                                           justification:
                                                           "Web API registers controllers for disposal when the request ends during the " +
                                                           "call to ApiController.ExecuteAsync.");
                }

                container.AddRegistration(controllerType, registration);
            }
        }
Esempio n. 2
0
        private static void ConfigureIoc(Container container)
        {
            container.Register <IPeopleManagerService, PeopleManagerService>(Lifestyle.Transient);

            Registration registration = container.GetRegistration(typeof(IPeopleManagerService)).Registration;

            registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent,
                                                   "Presenters will call Dispose on these services.");

            container.Verify();
        }
        public static void RegisterMvcControllers(this Container container, params Assembly[] assemblies)
        {
            foreach (Type controllerType in GetControllerTypesToRegister(container, assemblies))
            {
                Registration registration = Lifestyle.Transient.CreateRegistration(controllerType, container);

                registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent,
                                                       justification:
                                                       "MVC's DefaultControllerFactory disposes the controller when the web request ends.");

                container.AddRegistration(controllerType, registration);
            }
        }
Esempio n. 4
0
        public static void RegisterMvcControllers(this Container container, params Assembly[] assemblies)
        {
            foreach (Type controllerType in GetControllerTypesToRegister(container, assemblies))
            {
                Registration registration = Lifestyle.Transient.CreateRegistration(controllerType, container);

                // Suppress the Disposable Transient Component warning, because MVC's controller factory
                // ensures correct disposal of controllers.
                registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent);

                container.AddRegistration(controllerType, registration);
            }
        }
        private static Registration CreateNonSingletonRegistration(
            Container container, Type serviceType, IApplicationBuilder app, Lifestyle lifestyle)
        {
            IHttpContextAccessor accessor = GetHttpContextAccessor(app);

            Registration registration = lifestyle.CreateRegistration(
                serviceType,
                () => GetServiceProvider(accessor, container, lifestyle).GetRequiredService(serviceType),
                container);

            if (lifestyle == Lifestyle.Transient && typeof(IDisposable).IsAssignableFrom(serviceType))
            {
                registration.SuppressDiagnosticWarning(
                    DiagnosticType.DisposableTransientComponent,
                    justification: "This is a cross-wired service. ASP.NET Core will ensure it gets disposed.");
            }

            return(registration);
        }
        /// <summary>
        /// Registers the Web API <see cref="IHttpController"/> types that available for the application. This
        /// method uses the configured <see cref="IHttpControllerTypeResolver"/> to determine which controller
        /// types to register.
        /// </summary>
        /// <param name="container">The container the controllers should be registered in.</param>
        /// <param name="configuration">The <see cref="HttpConfiguration"/> to use to get the Controller
        /// types to register.</param>
        /// <param name="assemblies">The assemblies to search.</param>
        /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null
        /// reference (Nothing in VB).</exception>
        public static void RegisterWebApiControllers(this Container container, HttpConfiguration configuration,
                                                     IEnumerable <Assembly> assemblies)
        {
            Requires.IsNotNull(container, "container");
            Requires.IsNotNull(configuration, "configuration");
            Requires.IsNotNull(assemblies, "assemblies");

            IAssembliesResolver assembliesResolver = new AssembliesResolver(assemblies);

            var controllerTypes = GetControllerTypesFromConfiguration(configuration, assembliesResolver);

            foreach (Type controllerType in controllerTypes)
            {
                Registration registration = Lifestyle.Transient.CreateRegistration(controllerType, container);

                // Suppress the Disposable Transient Component warning, because MVC's controller factory
                // ensures correct disposal of controllers.
                registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent);

                container.AddRegistration(controllerType, registration);
            }
        }
Esempio n. 7
0
        private static Registration CreateNonSingletonRegistration(
            Container container, Type serviceType, IServiceProvider appServices, Lifestyle lifestyle)
        {
            IHttpContextAccessor accessor = GetHttpContextAccessor(appServices);

            Registration registration = lifestyle.CreateRegistration(
                serviceType,
                () => GetServiceProvider(accessor, container, lifestyle).GetRequiredService(serviceType),
                container);

            // This registration is managed and disposed by IServiceProvider and should, therefore, not be
            // disposed (again) by Simple Injector.
            registration.SuppressDisposal = true;

            if (lifestyle == Lifestyle.Transient && typeof(IDisposable).IsAssignableFrom(serviceType))
            {
                registration.SuppressDiagnosticWarning(
                    DiagnosticType.DisposableTransientComponent,
                    justification:
                    "This is a cross-wired service. ASP.NET Core will ensure it gets disposed.");
            }

            return(registration);
        }
Esempio n. 8
0
        /// <summary>
        /// Allows registrations made using the <see cref="IServiceCollection"/> API to be resolved by Simple Injector.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
        public static void AutoCrossWireAspNetComponents(this Container container, IApplicationBuilder app)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

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

            var services = (IServiceCollection)container.GetItem(CrossWireContextKey);

            if (services == null)
            {
                throw new InvalidOperationException(
                          "To use this method, please make sure cross-wiring is enabled, by invoking " +
                          $"the {nameof(EnableSimpleInjectorCrossWiring)} extension method as part of the " +
                          "ConfigureServices method of the Startup class. " +
                          "See https://simpleinjector.org/aspnetcore for more information.");
            }

            if (container.Options.DefaultScopedLifestyle == null)
            {
                throw new InvalidOperationException(
                          "To be able to allow auto cross-wiring, 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");
            }

            CrossWireServiceScope(container, app);

            IHttpContextAccessor accessor = GetHttpContextAccessor(app);

            container.ResolveUnregisteredType += (s, e) =>
            {
                if (e.Handled)
                {
                    return;
                }

                Type serviceType = e.UnregisteredServiceType;

                ServiceDescriptor descriptor = FindServiceDescriptor(services, serviceType);

                if (descriptor != null)
                {
                    Lifestyle lifestyle = ToLifestyle(descriptor.Lifetime);

                    // Create a cross-wire registration that calls back into the .NET Core container.
                    Registration registration = lifestyle.CreateRegistration(serviceType,
                                                                             () => GetServiceProvider(accessor, container).GetRequiredService(serviceType),
                                                                             container);

                    // Apply the required suppressions.
                    if (lifestyle == Lifestyle.Transient && typeof(IDisposable).IsAssignableFrom(serviceType))
                    {
                        registration.SuppressDiagnosticWarning(
                            DiagnosticType.DisposableTransientComponent,
                            justification: "This is a cross-wired service. ASP.NET will ensure it gets disposed.");
                    }

                    e.Register(registration);
                }
            };
        }