ServiceCollectionExtensions

We found 10 examples in language CSharp for this search. You will see 37 fragments of code.
        [Test, CustomAutoMoqData]
        public void ServiceCollection_is_returned(IServiceCollection services, Action<INybusConfigurator> configuratorDelegate)
        {
            var result = ServiceCollectionExtensions.AddNybus(services, configuratorDelegate);

            Assert.That(result, Is.SameAs(services));
        }

        [Test, CustomAutoMoqData]
        public void AddNybus_invokes_configuratorDelegate(IServiceCollection services, Action<INybusConfigurator> configuratorDelegate)
        {
            ServiceCollectionExtensions.AddNybus(services, configuratorDelegate);

            Mock.Get(configuratorDelegate).Verify(p => p(It.IsAny<INybusConfigurator>()));
        }

        [Test, CustomAutoMoqData]
        public void AddNybus_registers_NybusHostOptions(IServiceCollection services, IConfigurationSection configuration, Action<INybusConfigurator> configuratorDelegate)
        {
            ServiceCollectionExtensions.AddNybus(services, configuratorDelegate);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(NybusHostOptions))));
        }

        [Test, AutoMoqData]
        public void AddCommandHandler_registers_handler_generic_syntax(IServiceCollection services)
        {
            ServiceCollectionExtensions.AddCommandHandler<FirstTestCommand, FirstTestCommandHandler>(services);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(ICommandHandler<FirstTestCommand>) && sd.ImplementationType == typeof(FirstTestCommandHandler))));
        }

        [Test, AutoMoqData]
        public void AddCommandHandler_registers_handler_by_its_type(IServiceCollection services)
        {
            ServiceCollectionExtensions.AddCommandHandler(services, typeof(FirstTestCommandHandler));

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(ICommandHandler<FirstTestCommand>) && sd.ImplementationType == typeof(FirstTestCommandHandler))));
        }

        [Test, AutoMoqData]
        public void AddCommandHandler_registers_handler_by_its_type_generic_syntax(IServiceCollection services)
        {
            ServiceCollectionExtensions.AddCommandHandler<FirstTestCommandHandler>(services);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(ICommandHandler<FirstTestCommand>) && sd.ImplementationType == typeof(FirstTestCommandHandler))));
        }


        [Fact]
        public void AddGraphQL_ServicesSchema_ServiceNull()
        {
            // arrange
            var schema = Schema.Create(c => c.Options.StrictValidation = false);

            // act
            Action action = () => ServiceCollectionExtensions.AddGraphQL(
                null, schema);

            // assert
            Assert.Throws<ArgumentNullException>(action);
        }

        [Fact]
        public void AddGraphQL_ServicesSchema_SchemaNull()
        {
            // arrange
            // act
            Action action = () => ServiceCollectionExtensions.AddGraphQL(
                new ServiceCollection(), default(Schema));

            // assert
            Assert.Throws<ArgumentNullException>(action);
        }

        [Fact]
        public void AddGraphQL_ServicesSchemaConfigure_ServiceNull()
        {
            // arrange
            var schema = Schema.Create(c => c.Options.StrictValidation = false);

            // act
            Action action = () => ServiceCollectionExtensions.AddGraphQL(
                null,
                schema,
                new Action<IQueryExecutionBuilder>(c => { }));

            // assert
            Assert.Throws<ArgumentNullException>(action);
        }

        [Fact]
        public void AddGraphQL_ServicesSchemaConfigure_SchemaNull()
        {
            // arrange
            // act
            Action action = () => ServiceCollectionExtensions.AddGraphQL(
                new ServiceCollection(),
                default(Schema),
                new Action<IQueryExecutionBuilder>(c => { }));

            // assert
            Assert.Throws<ArgumentNullException>(action);
        }

        [Fact]
        public void AddGraphQL_ServicesSchemaFactory_ServiceNull()
        {
            // arrange
            var schema = Schema.Create(c => c.Options.StrictValidation = false);

            // act
            Action action = () => ServiceCollectionExtensions.AddGraphQL(
                null, new Func<IServiceProvider, ISchema>(s => schema));

            // assert
            Assert.Throws<ArgumentNullException>(action);
        }

        [Fact]
        public void AddGraphQL_ServicesSchemaFactory_SchemaFactoryNull()
        {
            // arrange
            // act
            Action action = () => ServiceCollectionExtensions.AddGraphQL(
                new ServiceCollection(),
                default(Func<IServiceProvider, ISchema>));

            // assert
            Assert.Throws<ArgumentNullException>(action);
        }

        /// <summary>
        /// Add core project to the service collection.
        /// </summary>
        /// <param name="service">IServiceCollection.</param>
        /// <returns>Updated IServiceCollection.</returns>
        public static IServiceCollection AddAppApplication(this IServiceCollection service)
        {
            service.AddMediatR(typeof(ServiceCollectionExtensions));
            return service;
        }


        internal override void Register(IServiceCollection services, Type type) {
            addServiceMethod.MakeGenericMethod(ServiceType, type).Invoke(null, new object[] { services });
        }

        internal override void Register(IServiceCollection services, Type type, Action<Decorators.DecoratorOptions> decoratorSetupAction) {
            addDecoratedServiceMethod.MakeGenericMethod(ServiceType, type).Invoke(null, new object[] { services, decoratorSetupAction });
        }


        internal override void Register(IServiceCollection services, Type type) {
            addServiceMethod.MakeGenericMethod(ServiceType, type).Invoke(null, new object[] { services });
        }

        internal override void Register(IServiceCollection services, Type type, Action<Decorators.DecoratorOptions> decoratorSetupAction) {
            addDecoratedServiceMethod.MakeGenericMethod(ServiceType, type).Invoke(null, new object[] { services, decoratorSetupAction });
        }

        [Test, CustomAutoData]
        public void AddWcfService_returns_same_ServiceCollection(IServiceCollection services, WcfServiceConfigurator<TestService> configurator, ServiceLifetime lifetime)
        {
            var result = ServiceCollectionExtensions.AddWcfService<TestService>(services, configurator, lifetime);

            Assert.That(result, Is.SameAs(services));
        }

        [Test, CustomAutoData]
        public void AddWcfService_registers_host_configuration(IServiceCollection services, WcfServiceConfigurator<TestService> configurator, ServiceLifetime lifetime)
        {
            ServiceCollectionExtensions.AddWcfService<TestService>(services, configurator, lifetime);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(d => d.ServiceType == typeof(IServiceHostConfiguration<TestService>) && d.Lifetime == ServiceLifetime.Singleton)));
        }

        [Test, CustomAutoData]
        public void AddWcfService_registers_host_builder(IServiceCollection services, WcfServiceConfigurator<TestService> configurator, ServiceLifetime lifetime)
        {
            ServiceCollectionExtensions.AddWcfService<TestService>(services, configurator, lifetime);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(d => d.ServiceType == typeof(IServiceHostBuilder) && d.ImplementationType == typeof(ServiceHostBuilder) && d.Lifetime == ServiceLifetime.Singleton)));
        }

        [Test, CustomAutoData]
        public void AddWcfService_registers_hosted_service_configuration(IServiceCollection services, WcfServiceConfigurator<TestService> configurator, ServiceLifetime lifetime)
        {
            ServiceCollectionExtensions.AddWcfService<TestService>(services, configurator, lifetime);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(d => d.ServiceType == typeof(WcfServiceHostedServiceConfiguration<TestService>) && d.Lifetime == ServiceLifetime.Singleton)));
        }

        [Test, CustomAutoData]
        public void AddWcfService_registers_hosted_service(IServiceCollection services, WcfServiceConfigurator<TestService> configurator, ServiceLifetime lifetime)
        {
            ServiceCollectionExtensions.AddWcfService<TestService>(services, configurator, lifetime);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(d => d.ServiceType == typeof(IHostedService) && d.Lifetime == ServiceLifetime.Singleton)));
        }

        [Test, CustomAutoData]
        public void AddWcfService_registers_announcement_service(IServiceCollection services, WcfServiceConfigurator<TestService> configurator, ServiceLifetime lifetime)
        {
            ServiceCollectionExtensions.AddWcfService<TestService>(services, configurator, lifetime);

            Mock.Get(services).Verify(p => p.Add(It.Is<ServiceDescriptor>(d => d.ServiceType == typeof(IAnnouncementService) && d.Lifetime == ServiceLifetime.Singleton)));
        }


        [Theory]
        [InlineData(false, "name", true, typeof(ArgumentNullException))]
        [InlineData(true, null, true, typeof(ArgumentNullException))]
        [InlineData(true, "", true, typeof(ArgumentNullException))]
        [InlineData(true, "name", false, typeof(ArgumentNullException))]
        [InlineData(true, " ", true, null)]
        [InlineData(true, "name", true, null)]
        [InlineData(true, "duplicated", true, typeof(InvalidOperationException))]
        public void AddNamedScoped_Parameters_ExpectedTheExceptionOrNot(bool servicesIsValid, string name, bool factoryIsValid, Type exceptionType)
        {
            // Arranje
            var (services, factory) = GetArguments(servicesIsValid, factoryIsValid, ServiceLifetime.Scoped);

            // Act
            void act() => ServiceCollectionExtensions.AddNamedScoped(services, name, factory);

            // Assert
            if (exceptionType == null)
            {
                act();
            }
            else
            {
                Assert.Throws(exceptionType, act);
            }
        }

        [Theory]
        [InlineData(false, "name", true, typeof(ArgumentNullException))]
        [InlineData(true, null, true, typeof(ArgumentNullException))]
        [InlineData(true, "", true, typeof(ArgumentOutOfRangeException))]
        [InlineData(true, "name", false, typeof(ArgumentOutOfRangeException))]
        [InlineData(true, " ", true, null)]
        [InlineData(true, "name", true, null)]
        [InlineData(true, "duplicated", true, typeof(InvalidOperationException))]
        public void AddNamedScoped_NamedServiceDictionary_Parameters_ExpectedTheExceptionOrNot(bool servicesIsValid, string name, bool factoryIsValid, Type exceptionType)
        {
            // Arranje
            var (services, factory) = GetArguments(servicesIsValid, factoryIsValid, ServiceLifetime.Scoped);

            // Act
            void act() => ServiceCollectionExtensions.AddNamedScoped(services, new NamedServiceDictionary<object> { { name, factory } });

            // Assert
            if (exceptionType == null)
            {
                act();
            }
            else
            {
                Assert.Throws(exceptionType, act);
            }
        }

        [Theory]
        [InlineData(false, "name", true, typeof(ArgumentNullException))]
        [InlineData(true, null, true, typeof(ArgumentNullException))]
        [InlineData(true, "", true, typeof(ArgumentNullException))]
        [InlineData(true, "name", false, typeof(ArgumentNullException))]
        [InlineData(true, " ", true, null)]
        [InlineData(true, "name", true, null)]
        [InlineData(true, "duplicated", true, typeof(InvalidOperationException))]
        public void AddNamedTransient_Parameters_ExpectedTheExceptionOrNot(bool servicesIsValid, string name, bool factoryIsValid, Type exceptionType)
        {
            // Arranje
            var (services, factory) = GetArguments(servicesIsValid, factoryIsValid, ServiceLifetime.Transient);

            // Act
            void act() => ServiceCollectionExtensions.AddNamedTransient(services, name, factory);

            // Assert
            if (exceptionType == null)
            {
                act();
            }
            else
            {
                Assert.Throws(exceptionType, act);
            }
        }

        [Theory]
        [InlineData(false, "name", true, typeof(ArgumentNullException))]
        [InlineData(true, null, true, typeof(ArgumentNullException))]
        [InlineData(true, "", true, typeof(ArgumentOutOfRangeException))]
        [InlineData(true, "name", false, typeof(ArgumentOutOfRangeException))]
        [InlineData(true, " ", true, null)]
        [InlineData(true, "name", true, null)]
        [InlineData(true, "duplicated", true, typeof(InvalidOperationException))]
        public void AddNamedTransient_NamedServiceDictionary_Parameters_ExpectedTheExceptionOrNot(bool servicesIsValid, string name, bool factoryIsValid, Type exceptionType)
        {
            // Arranje
            var (services, factory) = GetArguments(servicesIsValid, factoryIsValid, ServiceLifetime.Transient);

            // Act
            void act() => ServiceCollectionExtensions.AddNamedTransient(services, new NamedServiceDictionary<object> { { name, factory } });

            // Assert
            if (exceptionType == null)
            {
                act();
            }
            else
            {
                Assert.Throws(exceptionType, act);
            }
        }

        [Theory]
        [InlineData(false, "name", true, typeof(ArgumentNullException))]
        [InlineData(true, null, true, typeof(ArgumentNullException))]
        [InlineData(true, "", true, typeof(ArgumentNullException))]
        [InlineData(true, "name", false, typeof(ArgumentNullException))]
        [InlineData(true, " ", true, null)]
        [InlineData(true, "name", true, null)]
        [InlineData(true, "duplicated", true, typeof(InvalidOperationException))]
        public void AddNamedSingleton_Parameters_ExpectedTheExceptionOrNot(bool servicesIsValid, string name, bool factoryIsValid, Type exceptionType)
        {
            // Arranje
            var (services, factory) = GetArguments(servicesIsValid, factoryIsValid, ServiceLifetime.Singleton);

            // Act
            void act() => ServiceCollectionExtensions.AddNamedSingleton(services, name, factory);

            // Assert
            if (exceptionType == null)
            {
                act();
            }
            else
            {
                Assert.Throws(exceptionType, act);
            }
        }

        [Theory]
        [InlineData(false, "name", true, typeof(ArgumentNullException))]
        [InlineData(true, null, true, typeof(ArgumentNullException))]
        [InlineData(true, "", true, typeof(ArgumentOutOfRangeException))]
        [InlineData(true, "name", false, typeof(ArgumentOutOfRangeException))]
        [InlineData(true, " ", true, null)]
        [InlineData(true, "name", true, null)]
        [InlineData(true, "duplicated", true, typeof(InvalidOperationException))]
        public void AddNamedSingleton_NamedServiceDictionary_Parameters_ExpectedTheExceptionOrNot(bool servicesIsValid, string name, bool factoryIsValid, Type exceptionType)
        {
            // Arranje
            var (services, factory) = GetArguments(servicesIsValid, factoryIsValid, ServiceLifetime.Singleton);

            // Act
            void act() => ServiceCollectionExtensions.AddNamedSingleton(services, new NamedServiceDictionary<object> { { name, factory } });

            // Assert
            if (exceptionType == null)
            {
                act();
            }
            else
            {
                Assert.Throws(exceptionType, act);
            }
        }

        /// <summary>
        /// Add application project to the service collection.
        /// </summary>
        /// <param name="service">IServiceCollection.</param>
        /// <returns>Updated IServiceCollection.</returns>
        public static IServiceCollection AddApplication(this IServiceCollection service)
        {
            service.AddMediatR(typeof(ServiceCollectionExtensions));
            return service;
        }

        public static IServiceCollection AddCelebrityMvc(this IServiceCollection services, IConfiguration configuration)
        {
            return services
                .AddOptions()
                .Configure<Global>(configuration.GetSection(nameof(Global)))
                .AddAuthentication(ServiceCollectionExtensions.Authentication)
                .AddOpenIdConnect(ServiceCollectionExtensions.OpenId(configuration))
                .AddCookie()
                .Services
                .AddMvc(ServiceCollectionExtensions.Mvc)
                .AddJsonOptions(ServiceCollectionExtensions.Json)
                .Services;
        }

        private static void Json(MvcJsonOptions options)
        {
            options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
            options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
            options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            options.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
        }

        private static void Mvc(MvcOptions options)
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();

            options.Filters.Add(new AuthorizeFilter(policy));
#if DEBUG
            options.Filters.Add(new DisableCorsAuthorizationFilter());
#endif
        }

        private static void Authentication(AuthenticationOptions options)
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        }

        private static Action<OpenIdConnectOptions> OpenId(IConfiguration configuration)
        {
            return options =>
            {
                var global = configuration.Get<Global>();
                configuration.GetSection(nameof(Global)).Bind(global);

                options.Authority = new UriBuilder(Uri.UriSchemeHttps, global.AuthOpenId.Domain).Uri.AbsoluteUri;
                options.CallbackPath = new PathString(global.AuthOpenId.CallbackUrl);
                options.ClaimsIssuer = global.AuthOpenId.ClaimsIssuer;
                options.ClientId = global.AuthOpenId.ClientId;
                options.ClientSecret = global.AuthOpenId.ClientSecret;
                options.ResponseType = global.AuthOpenId.ResponseType;

                options.Scope.Clear();
                options.Scope.Add(global.AuthOpenId.Scope);

                options.Events = new OpenIdConnectEvents
                {
                    OnRedirectToIdentityProviderForSignOut = ServiceCollectionExtensions.SignOut(global),
                    OnUserInformationReceived = ServiceCollectionExtensions.User()
                };
            };
        }

        private static Func<UserInformationReceivedContext, Task> User()
        {
            return context =>
            {
                Trace.WriteLine(context.User);
                return Task.CompletedTask;
            };
        }

        /// <summary>
        /// Add Infrastructure project to the service collection.
        /// </summary>
        /// <param name="service">IServiceCollection.</param>
        /// <returns>Updated IServiceCollection.</returns>
        public static IServiceCollection AddInfrastructure(this IServiceCollection service)
        {
            service.AddMediatR(typeof(ServiceCollectionExtensions));
            service.AddHttpClient();
            return service;
        }

Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions : Object

Methods :

public static IServiceCollection AddOpenTracing(IServiceCollection services = , Action<IOpenTracingBuilder> builder = null)
public static IServiceCollection AddOpenTracingCoreServices(IServiceCollection services = , Action<IOpenTracingBuilder> builder = null)
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods