Проблема в том, что я не могу получить информацию о конфигурации в nacos.
Вот мой тестовый класс для HomeController:
Код: Выделить всё
using Xunit;
namespace ApiTestProject
public class HomeControllerTest
{
public HomeControllerTest()
{
Init(); // using DI to mock register services
controller = new HomeController();
}
// in this method, I can not access the nacos config strings
private void Init()
{
var builder = WebApplication.CreateBuilder();
// add appsettings.json
builder.Host.ConfigureAppConfiguration(cbuilder =>
{
cbuilder.AddJsonFile("appsettings.Test.json", optional: false, reloadOnChange: true);
});
// get nacosconfig in appsettings
var nacosconfig = builder.Configuration.GetSection("NacosConfig");
builder.Host.ConfigureAppConfiguration((context, builder) =>
{
// add nacos
builder.AddNacosV2Configuration(nacosconfig);
});
// try to get the "DbConn" in nacos, but connstr is null
string connstr = builder.Configuration["DbConn"];
// other register logic...
}
}
Код: Выделить всё
{
"NacosConfig": {
"Listeners": [
{
"Optional": false,
"DataId": "global.dbconn",
"Group": "DEFAULT_GROUP"
},
{
"Optional": false,
"DataId": "global.redisconfig",
"Group": "DEFAULT_GROUP"
},
{
"Optional": false,
"DataId": "global.baseurlandkey",
"Group": "DEFAULT_GROUP"
}
],
"Namespace": "my-dev",
"ServerAddresses": [
"http://mynacos.url.address/"
],
"UserName": "dotnetcore",
"Password": "123456",
"AccessKey": "",
"SecretKey": "",
"EndPoint": "",
"ConfigUseRpc": false,
"NamingUseRpc": false
}
}
Подробнее здесь: https://stackoverflow.com/questions/782 ... net-core-6