微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。