修改获取配置

main
zhangbin 2 months ago
parent 202cfbe72f
commit 06277e6a5d

@ -12,6 +12,7 @@ namespace OpenAuth.WebApi.Model.mqtt
private readonly MqttMessageCenter _mqttCenter;
private readonly AirportMaintenanceApp _app;
public MqttHostedService(IServiceProvider serviceProvider, MqttMessageCenter mqttCenter, AirportMaintenanceApp app)
{
_serviceProvider = serviceProvider;
@ -23,14 +24,29 @@ namespace OpenAuth.WebApi.Model.mqtt
{
using var scope = _serviceProvider.CreateScope();
var handlers = scope.ServiceProvider.GetServices<IMqttMessageHandler>();
// 创建配置构建器
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json",
optional: true)
.AddEnvironmentVariables();
// 构建配置
var configuration = builder.Build();
// 读取连接字符串
var serverIp = configuration["MQTT:Server"];
var port = configuration["MQTT:Port"];
var username = configuration["MQTT:UserName"];
var password = configuration["MQTT:Password"];
await _mqttCenter.InitializeAsync(
handlers,
server: "175.27.168.120",
port: 6011,
server: serverIp,
port: int.Parse(port),
clientId: Guid.NewGuid().ToString(),
username: "sdhc",
password: ""
username: username,
password: password
);
//查询网关,订阅主题
var topics = new List<string>();

Loading…
Cancel
Save