Exemplo n.º 1
0
        /// <summary>
        /// Builds the IServiceProvider for the application and registers available services.
        /// </summary>
        private static void BuildAndRegisterServices()
        {
            IConfigurationRoot config = Program.Configuration;

            if (config == null)
            {
                throw new InvalidOperationException("Application Configuration is null. You must call BuildConfiguration() before calling this method.");
            }
            System.Console.WriteLine($"Connection string: {config["Data:ptvdb:ConnectionString"]}");

            var srvcol = new ServiceCollection();

            srvcol.AddLogging();

            // register console apps db context
            srvcol.AddEntityFrameworkNpgsql().AddDbContext <SourceDbContext>(options => options.UseNpgsql(Program.Configuration[ConfigKeys.SourceConnectionString]));
            srvcol.AddDbContext <SourceDbContext>(options => options.UseNpgsql(Program.Configuration[ConfigKeys.SourceConnectionString]));
            // register ptv db context and PTV services
            RegisterServiceManager.RegisterFromAllAssemblies(srvcol);
            RegisterDataProviderServices.RegisterFromAssembly(srvcol);
            BaseEntityCloners.RegisterBaseEntityCloners(srvcol);
            FrameworksInitializer.RegisterEntityFramework(srvcol, Program.Configuration[ConfigKeys.PTVConnectionString]);

            // for now make it behave like this..
            srvcol.AddTransient <ISourceRepository, SourceRepository>();
            srvcol.AddTransient <IFakePtvRepository, FakePtvJsonFileRepository>();
            srvcol.AddTransient <IHttpContextAccessor, FakeHttpContext>();
            srvcol.AddTransient <IHostingEnvironment, FakeHostingEnv>();
            srvcol.AddSingleton(new ApplicationConfiguration(config));

            Program.ServiceProvider = srvcol.BuildServiceProvider();
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services">object responsible for registering of the assemblies</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddMvcCore().AddJsonFormatters();
            //services.AddMvcCore();
            //services.AddAuthorization();
            services.AddSingleton(PtvConfiguration);
            services.AddOptions();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("ui", new Info()
                {
                    Title = "PTV UI API", Version = "ui", Description = "PTV UI Web API",
                });
                var developmentPath = string.Format(Path.Combine(appEnv.ContentRootPath, "bin", "Debug", "netcoreapp1.1"));

                var xmlCommentPath = Path.Combine(appEnv.ContentRootPath, @"PTV.Application.Api.xml");
                var modelsXmlPath  = Path.Combine(appEnv.ContentRootPath, @"PTV.Domain.Model.xml");

                c.DescribeAllEnumsAsStrings();

                if (!File.Exists(xmlCommentPath) || !File.Exists(modelsXmlPath))
                {
                    xmlCommentPath = Path.Combine(developmentPath, @"PTV.Application.Api.xml");
                    modelsXmlPath  = Path.Combine(developmentPath, @"PTV.Domain.Model.xml");
                }

                if (!File.Exists(xmlCommentPath) || !File.Exists(modelsXmlPath))
                {
                    return;
                }

                c.IncludeXmlComments(xmlCommentPath);
                c.IncludeXmlComments(modelsXmlPath);
            });
            //services.AddCors();
            services.Configure <CookieAuthenticationOptions>(options => Configuration.GetSection("Data:CookieAuthenticationConfigurations").Bind(options));
            services.Configure <OpenIdConnectOptions>(options => Configuration.GetSection("Data:OpenIDConnectConfigurations").Bind(options));
            //services.Configure<OAuthOptions>(options => Configuration.GetSection("Data:OpenIDConnectConfigurations").Bind(options));
            services.Configure <IdentityServerAuthenticationOptions>(options => Configuration.GetSection("Data:IdentityServerAuthenticationConfigurations").Bind(options));
            services.Configure <ProxyServerSettings>(options => Configuration.GetSection("Data:ProxyServerSettings").Bind(options));
            services.Configure <DataContextOptions>(options => Configuration.GetSection("Data:DataContextOptions").Bind(options));
            services.Configure <AnnotationServiceConfiguration>(options => Configuration.GetSection("Data:AnnotationService").Bind(options));
            services.Configure <MapServiceConfiguration>(options => Configuration.GetSection("Data:MapService").Bind(options));
            services.Configure <TestAccessUrlConfiguration>(options => Configuration.GetSection("Data:TestInternetAccess").Bind(options));
            services.Configure <MapDNSes>(options => Configuration.GetSection("ApplicationConfiguration:MapDNSNames").Bind(options));
            services.Configure <RequestFilterAppSetting>(options => Configuration.GetSection("RequestFilter").Bind(options));
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            RegisterServiceManager.RegisterFromAllAssemblies(services);
            BaseEntityCloners.RegisterBaseEntityCloners(services);
            RegisterDataProviderServices.RegisterFromAssembly(services);
            // Add framework services.
            FrameworksInitializer.RegisterEntityFramework(services, Configuration["Data:DefaultConnection:ConnectionString"]);
        }
Exemplo n.º 3
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     services.AddMvc().AddJsonOptions(i => i.SerializerSettings.Error += JsonError);
     services.AddWebpack();
     services.AddSingleton(PtvConfiguration);
     services.AddOptions();
     services.Configure <CookieAuthenticationOptions>(options => Configuration.GetSection("Data:CookieAuthenticationConfigurations").Bind(options));
     services.Configure <OpenIdConnectOptions>(options => Configuration.GetSection("Data:OpenIDConnectConfigurations").Bind(options));
     services.Configure <IdentityServerAuthenticationOptions>(options => Configuration.GetSection("Data:IdentityServerAuthenticationConfigurations").Bind(options));
     services.Configure <ProxyServerSettings>(options => Configuration.GetSection("Data:ProxyServerSettings").Bind(options));
     services.Configure <DataContextOptions>(options => Configuration.GetSection("Data:DataContextOptions").Bind(options));
     services.Configure <AnnotationServiceConfiguration>(options => Configuration.GetSection("Data:AnnotationService").Bind(options));
     services.Configure <MapServiceConfiguration>(options => Configuration.GetSection("Data:MapService").Bind(options));
     services.Configure <MapDNSes>(options => Configuration.GetSection("ApplicationConfiguration:MapDNSNames").Bind(options));
     services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
     RegisterServiceManager.RegisterFromAllAssemblies(services);
     BaseEntityCloners.RegisterBaseEntityCloners(services);
     RegisterDataProviderServices.RegisterFromAssembly(services);
     FrameworksInitializer.RegisterEntityFramework(services, Configuration["Data:DefaultConnection:ConnectionString"]);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Configure and register all services needed for application
        /// </summary>
        /// <param name="services"></param>
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc(
                c => c.Conventions.Add(new ControllerModelRemoveVersionPrefixConvention())
                );
            services.AddMvcCore().AddJsonFormatters();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IConfigurationRoot>(Configuration);
            services.Configure <AppSettings>(options => Configuration.GetSection("AppSettings").Bind(options));

            services.AddSingleton(PtvConfiguration);
            services.Configure <CookieAuthenticationOptions>(options => Configuration.GetSection("Data:CookieAuthenticationConfigurations").Bind(options));
            services.Configure <OpenIdConnectOptions>(options => Configuration.GetSection("Data:OpenIDConnectConfigurations").Bind(options));
            services.Configure <IdentityServerAuthenticationOptions>(options => Configuration.GetSection("Data:IdentityServerAuthenticationConfigurations").Bind(options));
            services.Configure <ProxyServerSettings>(options => Configuration.GetSection("Data:ProxyServerSettings").Bind(options));
            services.Configure <DataContextOptions>(options => Configuration.GetSection("Data:DataContextOptions").Bind(options));
            services.Configure <AnnotationServiceConfiguration>(options => Configuration.GetSection("Data:AnnotationService").Bind(options));
            services.Configure <MapServiceConfiguration>(options => Configuration.GetSection("Data:MapService").Bind(options));
            services.Configure <TestAccessUrlConfiguration>(options => Configuration.GetSection("Data:TestInternetAccess").Bind(options));
            services.Configure <MapDNSes>(options => Configuration.GetSection("ApplicationConfiguration:MapDNSNames").Bind(options));
            services.Configure <RequestFilterAppSetting>(options => Configuration.GetSection("RequestFilter").Bind(options));
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            RegisterServiceManager.RegisterFromAllAssemblies(services);
            BaseEntityCloners.RegisterBaseEntityCloners(services);
            RegisterDataProviderServices.RegisterFromAssembly(services);
            FrameworksInitializer.RegisterEntityFramework(services, Configuration["Data:DefaultConnection:ConnectionString"]);
            services.AddSwaggerGen(
                c => {
                c.SwaggerDoc("v4", new Info
                {
                    Version     = "v4",
                    Title       = "PTV Open Api version 4",
                    Description = "Here you can see listed all the PTV Open Api methods."
                });
                c.SwaggerDoc("v5", new Info
                {
                    Version     = "v5",
                    Title       = "PTV Open Api version 5",
                    Description = "Here you can see listed all the PTV Open Api methods."
                });
                c.SwaggerDoc("v6", new Info
                {
                    Version     = "v6",
                    Title       = "PTV Open Api version 6",
                    Description = "Here you can see listed all the PTV Open Api methods."
                });
                c.SwaggerDoc("v7", new Info
                {
                    Version     = "v7",
                    Title       = "PTV Open Api version 7",
                    Description = "Here you can see listed all the PTV Open Api methods."
                });


                c.OperationFilter <SecurityRequirementsOperationFilter>();
                c.DocInclusionPredicate((version, apiDescription) => ResolveVersionSupportByRouteConstraint(apiDescription, version));
                c.SchemaFilter <SwaggerSchemaFilter>();

                // TODO: PTV-1556:
                //c.DocumentFilter<SwaggerDocumentFilter>();

                // Define the OAuth2.0 scheme that's in use (i.e. Implicit Flow)
                c.AddSecurityDefinition("oauth2", new OAuth2Scheme
                {
                    Type             = "oauth2",
                    Flow             = "implicit",
                    AuthorizationUrl = $"{Configuration["STS"]}/connect/authorize",
                    Scopes           = new Dictionary <string, string>
                    {
                        { "dataEventRecords", "Access event records" }
                    }
                });
            });

            var path           = appEnv.IsDevelopment() ? Path.Combine(appEnv.ContentRootPath, "bin", "Debug", @"netcoreapp1.1") : appEnv.ContentRootPath;
            var xmlCommentPath = Path.Combine(path, @"PTV.Application.OpenApi.xml");
            var modelsXmlPath  = Path.Combine(path, @"PTV.Domain.Model.xml");

            services.ConfigureSwaggerGen(options =>
            {
                options.DescribeAllEnumsAsStrings();
                options.IncludeXmlComments(xmlCommentPath);
                if (File.Exists(modelsXmlPath))
                {
                    options.IncludeXmlComments(modelsXmlPath);
                }
            });
        }