.net core 2.1 Web 应用程序在 visual studio 中工作,但在 windows 10 中部署到 IIS 时不起作用
.net core 2.1 web app works in visual studio but does not work when deployed to IIS in windows 10
我是 .net 核心开发的新手,我正在尝试将 Web 应用程序 .net 核心 2.1 部署到 windows 10 中的 IIS。我已经完成了所有步骤,包括创建应用程序池 'No Managed Code'一切正常。 2 天后它停止工作,然后我使用发布类型将我的项目重新部署到调试,在这里我在浏览器中显示了这个异常,这与日志文件中的相同。
但是,同一个应用程序在 visual studio 中运行良好。我的机器安装了以下 .net 包。
.Net Core Runtme 2.1.7(x64)
.Net Core 2.1.7 - Windows 服务器托管
.net 核心运行时 2.1.7(x86)
.Net 核心 SDK 2.1.503 (x86)
.Net 核心 SDK 2.1.503(x64)
微软网络部署 4.0
在浏览了所有可用的文章并调整和更改应用程序后,该应用程序终于可以运行了,但后来它停止运行并出现上述错误。
我的 Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.Configure<DataProtectionTokenProviderOptions>(o =>
{
o.Name = "Default";
o.TokenLifespan = TimeSpan.FromHours(1);
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
mysqloptions => {
mysqloptions.ServerVersion(new Version(8, 0, 13), ServerType.MySql);
}));
services.AddTransient<IProductRepository, EFProductRepository>();
services.AddScoped<Cart>(sp => SessionCart.GetCart(sp));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddIdentity<ApplicationUser, IdentityRole>(
options =>
{
options.Stores.MaxLengthForKeys = 128;
options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
options.SignIn.RequireConfirmedEmail = false;
options.Password.RequireDigit = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
}
)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddRoles<IdentityRole>()
//.AddDefaultUI();
.AddDefaultTokenProviders();
//Authentication
services.AddDbContext<MainContext>(options =>
options.UseMySql(Configuration.GetConnectionString("ModelConnectionString"),
mysqloptions => {
mysqloptions.ServerVersion(new Version(8, 0, 13), ServerType.MySql);
mysqloptions.MigrationsAssembly("GasStationApp");
}));
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();
services.AddMvc().AddNToastNotifyToastr(new ToastrOptions()
{
ProgressBar = false,
PositionClass = ToastPositions.TopFullWidth
}
);
services.Configure<IISOptions>(options => {
options.AutomaticAuthentication = false;
options.ForwardClientCertificate = false;
});
我的Program.cs
public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.RollingFile("logs/log-{Date}.txt")
.CreateLogger();
try
{
Log.Information("Starting web host");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false);
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseSerilog() // <-- Add this line
.Build();
}
该应用程序在 VS2017 中运行良好,但在 Windows 中部署到 IIS 时无法运行 10 请帮助我解决此问题。任何意见将是有益的。提前致谢。
这有点奇怪,部署的文件有三个appsettings.json、appsettings.development.json、appsettings.production.json。我没有对此进行调查,因为我认为默认的 appsettings.json 文件应该具有原始配置,但事实证明部署文件夹中的 appsettings.json 和 appsettings.development.json 只有默认设置在 VS 2017 中创建 Web 应用程序项目时可用。appsettings.production.json 文件具有原始配置。
解决方案。
复制 appsettings.production.json 文件并将其重命名为 appsettings.json,网络应用程序现在可以正常工作了。
文件是 appsettings.json
和 appsettings.{environment}.json
。 ASP.NET 核心依靠环境变量 (ASPNETCORE_ENVIRONMENT
) 来确定要加载的配置。默认情况下,这在 Visual Studio 中设置为 Development
,这当然会导致使用 appsettings.Development.json
。当您发布您的应用程序时,您应该将目标上的 ASPNETCORE_ENVIRONMENT
环境变量设置为 Production
,这将导致使用 appsettings.Production.json
。 (我不记得大小写是否重要,尽管它可能很重要,尤其是对于 Linux 和 Mac OS 使用的区分大小写的文件系统。最好只确保文件是命名为 appsettings.Production.json
,以防万一。)
此外,特定于环境的 JSON 文件会覆盖非特定的文件。换句话说,首先读入 appsettings.json
,然后读入 appsettings.{environment}.json
。在环境特定版本中设置的 appsettings.json
中的任何内容都将被环境中的该值覆盖-具体版本。
总之,模式应该是这样的。任何不特定于特定环境的配置都应该进入 appsettings.json
。任何特定于环境的配置都应该进入相应的特定于环境的配置文件。我发现在 appsettings.json
中放置特定于环境和秘密配置值的占位符也是一种很好的做法。例如:
"ConnectionStrings": {
"DefaultConnection": "[CONNECTION STRING]"
}
由于配置自身分层的方式,并且由于 appsettings.json
是第一个加载的东西,您可以以任何其他形式的配置提供实际值(特定于环境的 JSON、环境变量、用户机密、Azure Key Vault 等)。然后,这会将您应用程序的所有配置记录在一个地方,并明确指示实际需要提供的内容。
我是 .net 核心开发的新手,我正在尝试将 Web 应用程序 .net 核心 2.1 部署到 windows 10 中的 IIS。我已经完成了所有步骤,包括创建应用程序池 'No Managed Code'一切正常。 2 天后它停止工作,然后我使用发布类型将我的项目重新部署到调试,在这里我在浏览器中显示了这个异常,这与日志文件中的相同。
但是,同一个应用程序在 visual studio 中运行良好。我的机器安装了以下 .net 包。 .Net Core Runtme 2.1.7(x64) .Net Core 2.1.7 - Windows 服务器托管 .net 核心运行时 2.1.7(x86) .Net 核心 SDK 2.1.503 (x86) .Net 核心 SDK 2.1.503(x64) 微软网络部署 4.0
在浏览了所有可用的文章并调整和更改应用程序后,该应用程序终于可以运行了,但后来它停止运行并出现上述错误。 我的 Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.Configure<DataProtectionTokenProviderOptions>(o =>
{
o.Name = "Default";
o.TokenLifespan = TimeSpan.FromHours(1);
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
mysqloptions => {
mysqloptions.ServerVersion(new Version(8, 0, 13), ServerType.MySql);
}));
services.AddTransient<IProductRepository, EFProductRepository>();
services.AddScoped<Cart>(sp => SessionCart.GetCart(sp));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddIdentity<ApplicationUser, IdentityRole>(
options =>
{
options.Stores.MaxLengthForKeys = 128;
options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
options.SignIn.RequireConfirmedEmail = false;
options.Password.RequireDigit = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
}
)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddRoles<IdentityRole>()
//.AddDefaultUI();
.AddDefaultTokenProviders();
//Authentication
services.AddDbContext<MainContext>(options =>
options.UseMySql(Configuration.GetConnectionString("ModelConnectionString"),
mysqloptions => {
mysqloptions.ServerVersion(new Version(8, 0, 13), ServerType.MySql);
mysqloptions.MigrationsAssembly("GasStationApp");
}));
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();
services.AddMvc().AddNToastNotifyToastr(new ToastrOptions()
{
ProgressBar = false,
PositionClass = ToastPositions.TopFullWidth
}
);
services.Configure<IISOptions>(options => {
options.AutomaticAuthentication = false;
options.ForwardClientCertificate = false;
});
我的Program.cs
public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.RollingFile("logs/log-{Date}.txt")
.CreateLogger();
try
{
Log.Information("Starting web host");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false);
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseSerilog() // <-- Add this line
.Build();
}
该应用程序在 VS2017 中运行良好,但在 Windows 中部署到 IIS 时无法运行 10 请帮助我解决此问题。任何意见将是有益的。提前致谢。
这有点奇怪,部署的文件有三个appsettings.json、appsettings.development.json、appsettings.production.json。我没有对此进行调查,因为我认为默认的 appsettings.json 文件应该具有原始配置,但事实证明部署文件夹中的 appsettings.json 和 appsettings.development.json 只有默认设置在 VS 2017 中创建 Web 应用程序项目时可用。appsettings.production.json 文件具有原始配置。
解决方案。
复制 appsettings.production.json 文件并将其重命名为 appsettings.json,网络应用程序现在可以正常工作了。
文件是 appsettings.json
和 appsettings.{environment}.json
。 ASP.NET 核心依靠环境变量 (ASPNETCORE_ENVIRONMENT
) 来确定要加载的配置。默认情况下,这在 Visual Studio 中设置为 Development
,这当然会导致使用 appsettings.Development.json
。当您发布您的应用程序时,您应该将目标上的 ASPNETCORE_ENVIRONMENT
环境变量设置为 Production
,这将导致使用 appsettings.Production.json
。 (我不记得大小写是否重要,尽管它可能很重要,尤其是对于 Linux 和 Mac OS 使用的区分大小写的文件系统。最好只确保文件是命名为 appsettings.Production.json
,以防万一。)
此外,特定于环境的 JSON 文件会覆盖非特定的文件。换句话说,首先读入 appsettings.json
,然后读入 appsettings.{environment}.json
。在环境特定版本中设置的 appsettings.json
中的任何内容都将被环境中的该值覆盖-具体版本。
总之,模式应该是这样的。任何不特定于特定环境的配置都应该进入 appsettings.json
。任何特定于环境的配置都应该进入相应的特定于环境的配置文件。我发现在 appsettings.json
中放置特定于环境和秘密配置值的占位符也是一种很好的做法。例如:
"ConnectionStrings": {
"DefaultConnection": "[CONNECTION STRING]"
}
由于配置自身分层的方式,并且由于 appsettings.json
是第一个加载的东西,您可以以任何其他形式的配置提供实际值(特定于环境的 JSON、环境变量、用户机密、Azure Key Vault 等)。然后,这会将您应用程序的所有配置记录在一个地方,并明确指示实际需要提供的内容。