【问题标题】:NetCore App Published on IIS only work with Development Environment Variable在 IIS 上发布的 NetCore 应用程序仅适用于开发环境变量
【发布时间】:2020-03-21 04:07:30
【问题描述】:

我已经向 IIS 发布了一个应用程序,由于我试图访问它,我遇到了环境变量问题。

我已经按照这些步骤Setting up Env. Variables Manually

如果我将其配置为开发,应用程序工作正常,但如果我将其设置为生产,则屏幕上不断显示以下错误消息:Production Error Screen

应用程序正在使用 IIS 10 的 Windows Server 2019 标准版上部署。 认证方式:匿名认证

这是我的 Program.cs:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace SistemaGestor.SOO.Mvc
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseIISIntegration()
            .UseStartup<Startup>();
    }
}

这是我的 Startup.cs,没有 using 指令

namespace SistemaGestor.SOO.Mvc
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<SOOContext>(srv => SOOContextFactory.CreateInstance(Configuration));
            //Fernando Milanez 25/09/2019
            services.AddTransient<IVesselInterface,VesselRepository>();
            services.AddTransient<ICountryFlagInterface, CountryFlagRepository>();
            services.AddTransient<IAutenticacao, AutenticacaoRepositorio>();

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            }).AddCookie(options =>
            {
                options.LoginPath = "/Autenticacao/Login";
                options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
            });

            services.AddSession(options =>
            {
                options.Cookie.IsEssential = true;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
                .AddSessionStateTempDataProvider();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
            {
                SupportedCultures = new List<CultureInfo> { new CultureInfo("pt-BR") },
                SupportedUICultures = new List<CultureInfo> { new CultureInfo("pt-BR") },
                DefaultRequestCulture = new RequestCulture("pt-BR")
            };
            app.UseRequestLocalization(localizationOptions);

            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();
            app.UseAuthentication(); //Requisito para ativar a autenticao, então é sempre assim adiciono o middlewaree depois defino para usar
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Autenticacao}/{action=Login}/{id?}");
            });

        }
    }
}

【问题讨论】:

  • 您能否在不设置任何其他环境变量的情况下让 .NET 核心应用程序正常工作?因为您的应用似乎已经在生产模式下运行了。
  • @JokiesDing 仅在调试模式下。问题是当我在发布模式下发布应用程序时。
  • 这很奇怪。您是否尝试分析转储文件以找到真正的异常? WINDBG 和 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\x.x.x\sos.dll 可以帮你实现。

标签: c# asp.net-mvc asp.net-core iis


【解决方案1】:

不久前我在设置我的网站时犯了同样的错误。

确保您使用的是系统环境变量,而不是用户环境变量。

System Environment Variable

我不知道它是否会有所帮助,但我有几个 Nuget 软件包可能会有所帮助: DataJuggler.Core.UltimateHelper(点网框架) DataJuggler.UltimateHelper.Core(点网核心) 2012 年发布了 Dot Net Framework 的第一个版本,这就是为什么名称令人困惑的原因。

有一个名为 EnvironmentVariableHelper 的类可以让获取环境变量变得简单:

string value = GetEnvironmentVariableValue(string value);

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多