gpt4 book ai didi

c# - 错误 WebHostBuilder 只允许创建 WebHost 的单个实例

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:15 25 4
gpt4 key购买 nike

从 .NET Core 2.0 升级到 .NET Core 2.1

错误WebHostBuilder 只允许创建单个 WebHost 实例


程序.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

最佳答案

您在构建器上多次调用 .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;
}
}

并在构建 Web 主机后调用它。

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();
}

关于c# - 错误 WebHostBuilder 只允许创建 WebHost 的单个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com