繁体   English   中英

.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 core 2.1部署到Windows 10中的IIS。我已按照所有步骤进行操作,包括创建applicationpool'No Managed Code',并且一切正常。 2天后,它停止工作,然后我使用发行版类型将我的项目重新部署为Debug,在这里,我在浏览器中显示了此异常,与日志文件中的异常相同。 浏览网络应用程序时出错

但是,同一应用程序在Visual Studio中可以正常工作。 我的机器安装了以下.net软件包。 .Net Core Runtme 2.1.7(x64).Net Core 2.1.7-Windows Server Hosting .net Core Runtime 2.1.7(x86).Net Core SDK 2.1.503(x86).Net Core SDK 2.1.503(x64) Microsoft Web部署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 10中部署到IIS时无法正常工作。请帮助我解决此问题。 任何意见将是有益的。 提前致谢。

有点奇怪,部署的文件具有三个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,Web应用程序现在可以正常工作了。 在此处输入图片说明

这些文件是appsettings.jsonappsettings.{environment}.json ASP.NET Core依靠环境变量( ASPNETCORE_ENVIRONMENT )确定要加载的配置。 默认情况下,在Visual Studio中将其设置为Development ,这当然会导致appsettings.Development.json被利用。 发布应用程序时,应将目标位置的ASPNETCORE_ENVIRONMENT环境变量设置为Production ,这将导致使用appsettings.Production.json (我不记得大小写是否重要,尽管可能如此,特别是对于区分大小写的文件系统(例如Linux和Mac OS使用的大小写)。最好以确保文件名为appsettings.Production.json ,以防万一。)

此外,特定于环境的JSON文件会覆盖非特定文件。 换句话说, appsettings.json读取appsettings.json ,然后appsettings.{environment}.json同样在特定于环境的版本中设置的appsettings.json中的任何内容都将被特定于环境的版本中的该值覆盖。

简而言之,模式应该是这样。 任何不特定于特定环境的配置都应放入appsettings.json 任何特定于环境的配置都应放入相应的特定于环境的配置文件中。 我发现将特定于环境和秘密配置值的占位符也放置在appsettings.json是一种好appsettings.json 例如:

 "ConnectionStrings": {
     "DefaultConnection": "[CONNECTION STRING]"
 }

由于配置本身的方式,并且由于appsettings.json是首先要加载的内容,因此您可以采用其他任何形式的配置(特定于环境的JSON,环境变量,用户机密,Azure密钥)提供实际值保险柜等)。 然后,将所有应用程序的配置记录在一个地方,并清楚指示实际需要提供的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM