Browse Source

修改mqtt获取配置文件方式

release/3.x.x
1 2 years ago
parent
commit
9fc98c409d
5 changed files with 31 additions and 7 deletions
  1. +16
    -0
      BPA.MQTTnet.Extensions/BPA - Backup (1).MQTTnet.Extensions.csproj
  2. +2
    -1
      BPA.MQTTnet.Extensions/BPA.MQTTnet.Extensions.csproj
  3. +4
    -6
      BPA.MQTTnet.Extensions/Extensions/ServiceCollectionExtension.cs
  4. +1
    -0
      MQTTnet.client.Test/Startup.cs
  5. +8
    -0
      MQTTnet.client.Test/appsettings.json

+ 16
- 0
BPA.MQTTnet.Extensions/BPA - Backup (1).MQTTnet.Extensions.csproj View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<VersionPrefix>1.0.4</VersionPrefix>
<PackageReleaseNotes />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BPA.MQTTnet" Version="1.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Nuget.Tools.V2" Version="1.1.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

+ 2
- 1
BPA.MQTTnet.Extensions/BPA.MQTTnet.Extensions.csproj View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<VersionPrefix>1.0.3</VersionPrefix>
@@ -8,6 +8,7 @@
<PackageReference Include="BPA.MQTTnet" Version="1.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Nuget.Tools.V2" Version="1.1.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>


+ 4
- 6
BPA.MQTTnet.Extensions/Extensions/ServiceCollectionExtension.cs View File

@@ -7,8 +7,9 @@ using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using Newtonsoft.Json;
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace BPA.MQTTnet.Extensions.Extensions
@@ -18,12 +19,9 @@ namespace BPA.MQTTnet.Extensions.Extensions

public static IServiceCollection AddMqttClientHostedService(this IServiceCollection services, IConfiguration configuration)
{
ClientSettings clientSettings = new ClientSettings();
configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
var clientSettings = JsonConvert.DeserializeObject<ClientSettings>(configuration.GetSection(nameof(ClientSettings)).Value);
services.AddSingleton(clientSettings);

BrokerHostSettings brokerHostSettings = new BrokerHostSettings();
configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
var brokerHostSettings = JsonConvert.DeserializeObject<BrokerHostSettings>(configuration.GetSection(nameof(BrokerHostSettings)).Value);
services.AddSingleton(brokerHostSettings);
services.AddMqttClientServiceWithConfig();
//services.AddMqttClientServiceWithConfig(aspOptionBuilder =>


+ 1
- 0
MQTTnet.client.Test/Startup.cs View File

@@ -27,6 +27,7 @@ namespace MQTTnet.client.Test
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services(Configuration);
services.AddMqttClientHostedService(Configuration);
}
private void MapBrokerHostSettings()


+ 8
- 0
MQTTnet.client.Test/appsettings.json View File

@@ -16,5 +16,13 @@
"Id": "5eb020f043ba8930506acbdd",
"UserName": "rafiul",
"Password": "12345678"
},
"ApolloClientSettings": {
"Host": "http://10.2.1.21",
"Port": 28080,
"Env": "Dev",
"CommonNameSpace": "DEV.Config",
"ClientNameSpace": "application",
"AppId": "dev1_order"
}
}

Loading…
Cancel
Save