Ejemplo n.º 1
0
        public void FindAllDependenciesFindsAttributes()
        {
            IEnumerable <Assembly> thisAssembly = new[] { GetType().Assembly };
            var matches = DependencyInjectionRegistrar.FindAllDependencies(thisAssembly);

            Assert.IsTrue(matches.Count() == 2);
            Assert.AreEqual("TestDependency", matches.ElementAt(0).Type.Name);
            Assert.AreEqual("TestDependency2", matches.ElementAt(1).Type.Name);
        }
Ejemplo n.º 2
0
        public void AddDependenciesThrowsIfInvalidRegisterAsType()
        {
            IEnumerable <Assembly> thisAssembly = new[] { GetType().Assembly };

            System.Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");

            Assert.ThrowsException <NotSupportedException>(() => {
                DependencyInjectionRegistrar.AddDependencies(Mock.Of <IServiceCollection>(), new DependencyInjectionEntry[] {
                    new (typeof(TestDependency), new DependencyInjectionAttribute()
                    {
                        RegisterAs = typeof(string)
                    })
                }, new DependencyInjectionMiddleware[] { });
            });
        static int Main(string[] args)
        {
            var services = new ServiceCollection();

            services.AddSingleton <GreetingService>();
            // add extra services to the container here
            using var registrar = new DependencyInjectionRegistrar(services);
            var app = new CommandApp(registrar);

            app.SetDefaultCommand <InfoCommand>();
            app.Configure(config =>
            {
                config.AddCommand <InfoCommand>("greet");
                // configure your commands as per usual
                // commands are automatically added to the container
            });
            return(app.Run(args));
        }
Ejemplo n.º 4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Console.WriteLine($"Running in production: {environment.IsProduction()}");

            services.AddCors();

            DependencyInjectionRegistrar.Execute(
                services,
                AppDomain.CurrentDomain.GetAssemblies()
                );

            var authConfig = services.AddConfig <Auth0Config>(Configuration.GetSection("Auth0"));

            services.AddConfig <EmailConfig>(Configuration.GetSection("Email"));

            var stripeConfig = services.AddConfig <IBillingConfig, StripeConfig>(Configuration.GetSection("Stripe"));

            StripeConfiguration.ApiKey = stripeConfig.SecretKey;

            var dbConfig = services.AddConfig <DatabaseConfig, PostgresDatabaseConfig>(Configuration.GetSection("Database"));

            services.AddConfig <AdminConfig>(Configuration.GetSection("Admin"));

            services.AddAuthentication(opts => {
                opts.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                opts.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(opts => {
                opts.Authority = authConfig.Domain;
                opts.Audience  = authConfig.Identifier;
            });

            services.AddControllers(cfg => {
                cfg.Filters.Add(typeof(ValidationExceptionMiddlware));
                cfg.Filters.Add(typeof(AuthorizationExceptionMiddlware));
                cfg.Filters.Add(typeof(SpecificationExceptionMiddleware));
            }).AddJsonOptions(opts => {
                opts.JsonSerializerOptions.Converters.Add(new EnumSnakeCaseConverter());
                opts.JsonSerializerOptions.Converters.Add(new EitherConverter());
                opts.JsonSerializerOptions.IgnoreNullValues = true;
            });

            services.AddDatabaseMigrations(dbConfig.GetConnectionString(), typeof(MigrationsFlag).Assembly);
        }
Ejemplo n.º 5
0
        public void AddDependenciesCallsMiddleware()
        {
            IEnumerable <Assembly> thisAssembly = new[] { GetType().Assembly };

            System.Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");

            var mockMiddleware = new Mock <DependencyInjectionMiddleware>();

            var dependencies = new DependencyInjectionEntry[] {
                new (typeof(TestDependency), new DependencyInjectionAttribute()),
                new (typeof(TestDependency2), new DependencyInjectionAttribute()),
            };

            DependencyInjectionRegistrar.AddDependencies(
                Mock.Of <IServiceCollection>(),
                dependencies,
                new[] { mockMiddleware.Object as DependencyInjectionMiddleware }
                );

            mockMiddleware.Verify(m => m.BeforeEach(It.IsAny <DependencyInjectionEntry>()), Times.Exactly(dependencies.Count()));
        }
Ejemplo n.º 6
0
        public static Task <int> Main(string[] args)
        {
            var services = new ServiceCollection();

            services
            .AddHttpClient("airdrop")
            .ConfigurePrimaryHttpMessageHandler(
                () => new SocketsHttpHandler
            {
                // we using a self-signed certificate, so ignore it
                SslOptions = new SslClientAuthenticationOptions
                {
                    RemoteCertificateValidationCallback = delegate { return(true); }
                }
            });

            services
            .AddSingleton(AnsiConsole.Console)
            .AddLogging(
                builder =>
            {
                builder.ClearProviders();
                builder.AddProvider(new SpectreInlineLoggerProvider(AnsiConsole.Console));
            }
                );

            var typeRegistrar = new DependencyInjectionRegistrar(services);
            var app           = new CommandApp(typeRegistrar);

            app.Configure(
                c =>
            {
                c.AddCommand <ServerCommand>("server");
                c.AddCommand <ClientCommand>("client");
            }
                );

            AnsiConsole.WriteLine(ApplicationName);
            return(app.RunAsync(args));
        }
Ejemplo n.º 7
0
        static CommandApp Bootstrap()
        {
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json", true)
                                .Build()
            ;

            var serviceCollection = new ServiceCollection()
                                    .AddLogging
                                    (
                config =>
            {
                config.AddSimpleConsole();
            }
                                    )
                                    .Configure <JsonDbConfig>(configuration.GetSection("jsonDb"))
                                    .AddTransient <IActivitiesRepository, ActivitiesRepositoryJson>()
            ;


            using var registrar = new DependencyInjectionRegistrar(serviceCollection);
            var app = new CommandApp(registrar);

            app.Configure
            (
                config =>
            {
                config.Settings.ApplicationName = "Stats";
                config.ValidateExamples();

                config.AddCommand <AddCommand>("add")
                .WithDescription("Add an activity to the journal")
                .WithExample(new[] { "add" })
                ;
            }
            );

            return(app);
        }
Ejemplo n.º 8
0
        public void FindAllMiddlewaresFindsAll()
        {
            var mws = DependencyInjectionRegistrar.FindAllMiddlewares(new[] { GetType().Assembly });

            Assert.AreEqual(1, mws.Count());
        }