示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            if (this.env.EnvironmentName == "IntegrationTest")
            {
                var context = this.CreateInMemoryDbContextBuilder();
                services.AddSingleton(context);
                IntegrationSeed.Execute(context);
            }
            else
            {
                services.AddDbContext <PortalDbContext>(
                    options => options.UseSqlServer(this.configuration.GetConnectionString("PortalDbContext")));
            }

            services.AddScoped <DbContext, PortalDbContext>();
            services.AddScoped <ILinksService, LinksService>();
            services.AddScoped <ISettingsService, SettingsService>();
            services.AddScoped <IAvailableSettingsService, AvailableSettingsService>();
            services.AddSingleton <IConfigurationRoot>(res => this.configuration);
            services.AddEventManagementAuthentication(new DefaultEventManagementTokenValidationParameters(this.configuration));
            services.AddCors();
            services.AddMvc();
        }
示例#2
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var serviceFabricHostMarker = Environment.GetEnvironmentVariable("ServiceFabricHostMarker");

            if (this.CurrentEnvironment.EnvironmentName == "IntegrationTest")
            {
                var context = this.CreateInMemoryDbContextBuilder();
                services.AddSingleton(context);
                IntegrationSeed.Execute(context);
            }
            else
            {
                services.AddDbContext <RegistrationDbContext>(
                    options => options.UseSqlServer(this.Configuration.GetConnectionString("RegistrationDbContext")));
            }

            services.AddSingleton(res => this.Configuration);
            services.AddScoped <DbContext, RegistrationDbContext>();
            services.AddScoped <IEventService, EventService>();
            services.AddScoped <IRegistrationService, RegistrationService>();

            services.AddCors();
            services.AddMvc();
            services.AddEventManagementAuthentication(
                new DefaultEventManagementTokenValidationParameters(this.Configuration));

            var builder = new ContainerBuilder();

            builder.Populate(services);

            // Add NServiceBus Handlers
            EndpointConfiguration endpointConfiguration = null;

            if (!this.CurrentEnvironment.IsEnvironment("IntegrationTest"))
            {
                endpointConfiguration = EndpointConfigurationFactory.ConfigureEndpoint(RegistrationEndpoint.Name, true)
                                        .ConfigureSqlPersistence(
                    this.Configuration.GetConnectionString("RegistrationDbContext"),
                    1,
                    this.CurrentEnvironment.IsDevelopment() && serviceFabricHostMarker == null).ConfigureSqlTransport(
                    this.Configuration.GetConnectionString("TransportDbContext"),
                    (routing) =>
                {
                    routing.RegisterPublisher(typeof(AddEvent), ResouresEndpoint.Name);
                    routing.RegisterPublisher(typeof(DeleteEvent), ResouresEndpoint.Name);
                    routing.RegisterPublisher(typeof(CompletePayment), PaymentEndpoint.Name);
                    routing.RegisterPublisher(typeof(CancelPayment), PaymentEndpoint.Name);
                });
                endpointConfiguration.LicensePath("License.xml");
            }

            builder.Register(x => MessageSession).As <IMessageSession>();

            // Build container
            this.applicationContainer = builder.Build();

            // Add NServiceBus
            if (!this.CurrentEnvironment.IsEnvironment("IntegrationTest"))
            {
                endpointConfiguration.UseContainer <AutofacBuilder>(
                    customizations => { customizations.ExistingLifetimeScope(this.applicationContainer); });
                MessageSession = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
            }
            else
            {
                MessageSession = new TestableMessageSession();
            }

            var result = new AutofacServiceProvider(this.applicationContainer);

            ServiceProvider = result;

            return(result);
        }