public static IServiceCollection AddEntityFramework(this IServiceCollection services, bool configurationIsLocalOrDev, IOptions <ReservationsConfiguration> config)
        {
            return(services.AddScoped(p =>
            {
                var unitOfWorkContext = p.GetService <IUnitOfWorkContext>();
                var azureServiceTokenProvider = new AzureServiceTokenProvider();
                ReservationsDataContext dbContext;
                try
                {
                    var synchronizedStorageSession = unitOfWorkContext.Get <SynchronizedStorageSession>();
                    var sqlStorageSession = synchronizedStorageSession.GetSqlStorageSession();
                    var optionsBuilder = new DbContextOptionsBuilder <ReservationsDataContext>().UseSqlServer(sqlStorageSession.Connection);
                    dbContext = new ReservationsDataContext(sqlStorageSession.Connection, config, optionsBuilder.Options, azureServiceTokenProvider, configurationIsLocalOrDev);
                    dbContext.Database.UseTransaction(sqlStorageSession.Transaction);
                }
                catch (KeyNotFoundException)
                {
                    var connection = AddDatabaseExtension.GetSqlConnection(configurationIsLocalOrDev, config.Value.ConnectionString);
                    var optionsBuilder = new DbContextOptionsBuilder <ReservationsDataContext>().UseSqlServer(connection);
                    dbContext = new ReservationsDataContext(optionsBuilder.Options);
                }

                return dbContext;
            }));
        }
Пример #2
0
        public static void StartNServiceBus(this UpdateableServiceProvider serviceProvider,
                                            IConfiguration configuration, bool configurationIsLocalOrDev)
        {
            var endpointConfiguration = new EndpointConfiguration(EndPointName)
                                        .UseInstallers()
                                        .UseErrorQueue($"{EndPointName}-errors")
                                        .UseMessageConventions()
                                        .UseNewtonsoftJsonSerializer()
                                        .UseOutbox(true)
                                        .UseServicesBuilder(serviceProvider)
                                        .UseSqlServerPersistence(() => AddDatabaseExtension.GetSqlConnection(configurationIsLocalOrDev, configuration["Reservations:ConnectionString"]))
                                        .UseUnitOfWork();

            if (configurationIsLocalOrDev)
            {
                endpointConfiguration.UseLearningTransport();
            }
            else
            {
                endpointConfiguration.UseAzureServiceBusTransport(
                    configuration["Reservations:NServiceBusConnectionString"], r => { });
            }

            if (!string.IsNullOrEmpty(configuration["Reservations:NServiceBusLicense"]))
            {
                endpointConfiguration.License(configuration["Reservations:NServiceBusLicense"]);
            }

            var endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

            serviceProvider.AddSingleton(p => endpoint)
            .AddSingleton <IMessageSession>(p => p.GetService <IEndpointInstance>())
            .AddHostedService <NServiceBusHostedService>();
        }