Ошибка WebHostBuilder позволяет создавать только один экземпляр WebHost - PullRequest
0 голосов
/ 27 июня 2018

Обновлен с .NET Core 2.0 до .NET Core 2.1

ОШИБКА WebHostBuilder позволяет создавать только один экземпляр WebHost


Program.cs

namespace WebApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {

            CreateWebHostBuilder(args)
                .MigrateDbContext<ApplicationDbContext>((context, services) =>
                {
                    var env = services.GetService<IHostingEnvironment>();
                    var logger = services.GetService<ILogger<ApplicationDbContextSeed>>();

                    new ApplicationDbContextSeed()
                        .SeedAsync(context, env, logger)
                        .Wait();
                }).Build().Run();
        }
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .ConfigureAppConfiguration((builderContext, config) =>
                {
                    config.AddEnvironmentVariables();
                });
    }
}

IWebHostExtensions.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

namespace Microsoft.AspNetCore.Hosting
{
    public static class IWebHostExtensions
    {
        public static IWebHostBuilder MigrateDbContext<TContext>(this IWebHostBuilder webHost, Action<TContext, IServiceProvider> seeder) where TContext : DbContext
        {
            using (var scope = webHost.Build().Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                var logger = services.GetRequiredService<ILogger<TContext>>();

                var context = services.GetService<TContext>();

                try
                {
                    logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");

                    context.Database
                        .Migrate();

                    seeder(context, services);

                    logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
                }
            }

            return webHost;
        }
    }
}

Кредит:

https://github.com/dotnet-architecture/eShopOnContainers/blob/c83c6842f593aaaae59adefa78a412a4a6c87faa/src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs

https://github.com/dotnet-architecture/eShopOnContainers/blob/a9676b4b5667eeebe0aa8ff5f64c604e9bd3971a/src/Services/Identity/Identity.API/Program.cs

https://github.com/dotnet-architecture/eShopOnContainers/blob/a9676b4b5667eeebe0aa8ff5f64c604e9bd3971a/src/Services/Identity/Identity.API/Data/ApplicationDbContextSeed.cs

1 Ответ

0 голосов
/ 27 июня 2018

Вы звоните .Build() более одного раза на застройщика.

Первый в расширении

public static class IWebHostExtensions
{
    public static IWebHostBuilder MigrateDbContext<TContext>(this IWebHostBuilder webHost, Action<TContext, IServiceProvider> seeder) where TContext : DbContext
    {
        using (var scope = webHost.Build().Services.CreateScope()) //<-- HERE
        {
            //...

И снова в Main

CreateWebHostBuilder(args)
    .MigrateDbContext<ApplicationDbContext>((context, services) =>
    {
        var env = services.GetService<IHostingEnvironment>();
        var logger = services.GetService<ILogger<ApplicationDbContextSeed>>();

        new ApplicationDbContextSeed()
            .SeedAsync(context, env, logger)
            .Wait();
    }).Build().Run(); //<-- HERE

В связанных примерах для переноса используется хост, а не компоновщик.

Выполните рефакторинг вашего расширения аналогично расширению

public static class IWebHostExtensions {
    public static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<TContext, IServiceProvider> seeder) 
        where TContext : DbContext {
        using (var scope = webHost.Services.CreateScope()) {
            var services = scope.ServiceProvider;
            var logger = services.GetRequiredService<ILogger<TContext>>();
            var context = services.GetService<TContext>();

            try {
                logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");

                context.Database
                    .Migrate();

                seeder(context, services);

                logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
            } catch (Exception ex) {
                logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
            }
        }
        return webHost;
    }
}

И вызывать его после того, как веб-хост уже построен.

public class Program {
    public static void Main(string[] args) {

        BuildWebHost(args)
            .MigrateDbContext<ApplicationDbContext>((context, services) => {
                var env = services.GetService<IHostingEnvironment>();
                var logger = services.GetService<ILogger<ApplicationDbContextSeed>>();

                new ApplicationDbContextSeed()
                    .SeedAsync(context, env, logger)
                    .Wait();
            })
            .Run();
    }
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((builderContext, config) => {
                config.AddEnvironmentVariables();
            })
            .Build();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...