ASP.NET Core 3.1无法在Windows服务器上读取用户环境变量

问题描述

这是我的startup.cs文件的一部分:

   public void ConfigureServices(IServiceCollection services)
        {
            services.AdddistributedMemoryCache();

            services.AddSession(options =>
            {
                // Set a short timeout for easy testing.
                options.IdleTimeout = TimeSpan.FromMinutes(30);
                options.Cookie.HttpOnly = true;
                // Make the session cookie essential
                options.Cookie.IsEssential = true;
            });

            services.AddControllersWithViews().AddRazorRuntimeCompilation();

            services.AddDbContext<WangKunDbContext>(options => options.UseMysqL(Environment.GetEnvironmentvariable("MysqLConnection",EnvironmentvariableTarget.User)));

            // Register the Swagger generator,defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1",new OpenApiInfo { Title = "wulikunkun's api",Version = "v1" });
                });
        }

我想让EF Core从用户环境变量获取其连接信息,当我在开发环境上运行项目时,没有问题,并且我也可以通过EF Core命令成功迁移数据库

dotnet ef database update

但是当我将该项目发布到Windows服务器站点时,我根本无法启动该项目,也没有引发异常,我试图检查Windows日志并发现了这一点:

System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value,String parameterName)

因此,我使用Nlog输出是否获取了环境变量,日志文件确定 它根本没有得到变量:

2020-10-22 20:43:26.1168||INFO|Wulikunkun.Web.Startup|Faild to get the environment variable! 

我还尝试了其他方法,例如使IIS的'LoadUserProfile'保持为'ON',但没有效果

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)