Issue
,将直接关闭。xunit集成测试时,环境变量设置不起作用
不管如何设置api程序集的环境变量,在环境变量一直显示是Production
异常堆栈是什么?
using var httpClient = _factory.CreateClient();
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
var env = App.HostEnvironment;
string environmentName = env.EnvironmentName; //这个地方总是Production环境
var b = App.Configuration["Baidu:AppKey"];
var a = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
var c = App.Configuration.GetSection("ConnectionConfigs").Get<List<ConnectionConfig>>();
services.AddFileLogging(options =>
{
options.FileNameRule = fileName =>
{
return string.Format(fileName, DateTime.Now);
};
});
services.AddFileLogging("logs/error/error-{0:yyyy}-{0:MM}-{0:dd}.log", options =>
{
options.FileNameRule = fileName =>
{
return string.Format(fileName, DateTime.Now);
};
options.WriteFilter = (logMsg) =>
{
return logMsg.LogLevel == LogLevel.Error;
};
});
// 添加即时通讯
services.AddSignalR();
services.AddCorsAccessor();
services.AddMvcFilter<LoggingMonitorAttribute>();
//全局映射
TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true);
//定时任务
services.AddTaskScheduler();
#region 认证和授权
services.AddJwt((options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}), null, (o =>
{
o.Events = new()
{
OnMessageReceived = (context) =>
{
if (!context.HttpContext.Request.Path.HasValue)
{
return Task.CompletedTask;
}
var path = context.HttpContext.Request.Path;
if (path.StartsWithSegments("/hubs/chathub"))
{
var accessToken = context.Request.Query["access_token"];
if (!string.IsNullOrWhiteSpace(accessToken))
{
context.Token = accessToken;
}
}
return Task.CompletedTask;
}
};
}));
services.AddAuthorization();
#endregion
BaiduIntelligent.AddBaiduIntelligentSetup(services);
services.AddControllers().AddInjectWithUnifyResult<RESTfulResultProvider>();
SqlsugarSetup.AddSqlsugarSetup(services);
services.AddSingleton<IUserIdProvider, UserIdProvider>();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
//注册事件订阅
services.AddEventBus();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// 添加规范化结果状态码,需要在这里注册
app.UseUnifyResultStatusCodes();
app.UseHttpsRedirection();
app.Use((context, next) =>
{
context.Request.EnableBuffering();
return next();
});
app.UseRouting();
// 配置跨域
// 注意 SignalR 必须使用 AllowCredentials,其他的可以自由配置
app.UseCorsAccessor(builder =>
builder.SetIsOriginAllowed(_ => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseAuthentication();
app.UseAuthorization();
app.UseInject(string.Empty);
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/hubs/chathub");
endpoints.MapControllers();
});
}
}
您的代码下载地址?
能否提供一个demo。。。我这样很难搞。。。
git@github.com:lgw2403/FurionXunitTest.git
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
这个解决方案,您验证过吗,我这边总是报 未将对象引用到对象的实例
protected override IWebHostBuilder CreateWebHostBuilder() { return base.CreateWebHostBuilder().UseEnvironment("Development"); //这个地方报错 }
1.我是图片中的方法,.net 6.0是支持环境变量的更改的,但是 在Furion 中使用 Serve.Run(RunOptions.Default); 方式启动项目时,环境变量怎么也修改不了
2.net 6.0 集成测试时在不使用 WithWebHostBuilder 进行环境变量修改时,默认也是加载程序集中的环境变量配置,.net 6.0 在集成测试时 环境变量是没有问题的
3.如果我不使用Serve.Run(RunOptions.Default); 方式启动程序,而是使用 WebApplication.CreateBuilder(args).Inject(); 方式, 环境变量也是没有问题的
所以我怀疑Serve.Run(RunOptions.Default); 方式启动程序时加载环境变量的逻辑出现了问题
v4.2.4 版本, Serve.Run(RunOptions.Default.WithArgs(args));
非常感谢您,Furion 升级v4.2.4 版本,测试通过了
下次我提交问题时,会提供代码和详细的操作步骤的
登录 后才可以发表评论