我有一个非常好的asp.net核心1.1.0网站,只要它在我的机器上运行。它甚至可以通过TFS构建代理构建和创建发布的网站。发布管理器正确地拾取工件并将其复制到创建新apppool / website的远程“DEV”服务器。它甚至没有任何问题。服务器环境变量用作ASPNETCORE_ENVIRONMENT(DEV)和连接字符串的存储,并且由于发布管理器,也可以为该环境正确设置。
我的问题是,当网站启动时(通过IIS),输出日志表明托管环境是“生产”。日志输出:
Hosting environment: Production
Content root path: D:\Apps\FreightRates
Now listening on: http://localhost:20977
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://auqldrv00dev1si:10000/
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action Jbssa.FreightRates.Web.Controllers.HomeController.Index (Jbssa.FreightRates.Web) in 66.0757ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 812.9611ms 401 text/plain
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://auqldrv00dev1si:10000/
info: Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory[1]
Executed DbCommand (103ms) [Parameters=[@__name_0='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT [u].[ClaimType], [u].[Value]
FROM [scoControl].[UserClaim] AS [u]
WHERE [u].[User] = @__name_0
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1]
Authorization was successful for user: <mydomainname>.
info: History[0]
Refreshing Dashboard
然后它会崩溃,因为它试图从我的localdb实例而不是配置的环境变量连接字符串中读取数据。
我的startup.cs类读取配置详细信息的启动构造函数如下所示:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
builder.AddEnvironmentVariables();
var connectionStringConfig = builder.Build();
var config = new ConfigurationBuilder()
.AddUserConfiguration(
options => options.UseSqlServer(connectionStringConfig.GetConnectionString("FreightRatesConnection")));
foreach (var source in config.Sources)
builder.Add(source);
Configuration = builder.Build();
}
在“开发”中,一切正常,在本地PC环境中,但似乎没有按预期在Web服务器上加载环境变量。
任何人都可以看到任何明显的部署环境未切换的原因。顺便说一句,我们使用以下环境:
根据用户的复杂性和风险,可以添加其他一些其他内容。我们不使用Asp.Net Core中默认提供的“暂存,生产”这两个术语。为简单起见,我们将 LCL 重命名为Development。
更新 我正在运行AppPool,因此用户无法看到保存连接字符串的机器环境变量。我还验证了在运行Release任务后它们已设置并正确。我还手动停止并启动了appappol和网站“以防万一”虽然这些任务也是由发布任务执行的。
答案 0 :(得分:2)
我必须在IIS中的ApplicationPool上设置LoadUserProfile。我有点惊讶我必须为机器环境变量做这件事。