ApiFeatures.Mqtt.MongoDatabases
9.1.0
dotnet add package ApiFeatures.Mqtt.MongoDatabases --version 9.1.0
NuGet\Install-Package ApiFeatures.Mqtt.MongoDatabases -Version 9.1.0
<PackageReference Include="ApiFeatures.Mqtt.MongoDatabases" Version="9.1.0" />
<PackageVersion Include="ApiFeatures.Mqtt.MongoDatabases" Version="9.1.0" />
<PackageReference Include="ApiFeatures.Mqtt.MongoDatabases" />
paket add ApiFeatures.Mqtt.MongoDatabases --version 9.1.0
#r "nuget: ApiFeatures.Mqtt.MongoDatabases, 9.1.0"
#:package ApiFeatures.Mqtt.MongoDatabases@9.1.0
#addin nuget:?package=ApiFeatures.Mqtt.MongoDatabases&version=9.1.0
#tool nuget:?package=ApiFeatures.Mqtt.MongoDatabases&version=9.1.0
ApiFeatures.Mqtt.MongoDatabases
MongoDB persistence for the MQTT module. Isolated from ApiFeatures.Mqtt.Apis so the Mongo layer can be
composed by the host and swapped without touching the domain (ApiFeatures.Mqtt.Cores) or the API
surface (ApiFeatures.Mqtt.Apis).
Contents
Databases/MongoMqttDatabaseContext.cs— the Mongo client/database/collections, index creation, and BSON class-map registration.Serializers/MqOptionsBsonSerializer.cs— polymorphic BSON serializer forMqOptions(HiveMQ / self-hosted broker options).Models/Options/MongoMqttDatabaseOptions.cs— extends the sharedApiFeatures.Modules.Cores.Models.Options.ModuleMongoDatabaseOptions(Type == Mongo, broken-out connection components:Host,Port,DatabaseName,Username,Password,AuthenticationDatabase,ReplicaSet,UseTls— no single connection string), plus the MQTT-specificCollections(per-collection name overrides).Models/Options/MongoMqttDatabaseCollectionOptions.cs— one nullable name override per collection (Mqtts,MqttConnections,Brokers,BrokerUsers); null/blank falls back to the agreed PascalCase default (Mqtts,MqttConnections,Brokers,BrokerUsers).Services/{MqttService,MqttConnectionService,BrokerService,BrokerUserService}.cs— Mongo-backed implementations of theApiFeatures.Mqtt.Cores.Interfacesservice contracts.Extensions/MqttMongoDatabaseExtensions.cs—WithMongoDatabase(this IServiceCollection, options)composition entry point.
Connection equivalence (vs. the former single ConnectionString)
The former MongoMqttDatabaseContext built its client from a single connection string (with an injected
uuidRepresentation=standard hint and mongoUrl.DatabaseName ?? options.DatabaseName resolution). This
project assembles the client from the broken-out ModuleMongoDatabaseOptions components instead:
Servers = [new MongoServerAddress(Host, Port ?? 27017)].- When
Usernameis non-blank:Credential = MongoCredential.CreateCredential(AuthenticationDatabase ?? DatabaseName, Username, Password)—AuthenticationDatabaseis the old connection string'sauthSource. - When
ReplicaSetis non-blank:ReplicaSetName = ReplicaSet. - When
UseTlsistrue:UseTls = true. - Database name resolution comes solely from
DatabaseName; an empty/missingDatabaseNamethrowsArgumentException— the same guard as before, just no longer sourced from a connection string. - GUID serialization equivalence: the former
uuidRepresentation=standardconnection-string hint is not needed — the context has always setGuidSerializer(GuidRepresentation.Standard)explicitly on every entity's Guid-typed BSON class-map member (MqttEntity.Id,MqttConnectionEntity.Id/MqttId,BrokerEntity.Id,BrokerUserEntity.Id/BrokerId). Those class maps move here unchanged, so GUID serialization behaviour is identical.
Collection-name defaults
The pre-existing codebase had two different defaults for the same collections: the context's internal
fallback ("mqtts", "mqttConnections", "Brokers", "BrokerUsers", used only when Collections was
null) and the options class's documented defaults ("Mqtts", "MqttConnections", "Brokers",
"BrokerUsers", used when a non-null Collections left a property at its default). This project
resolves that inconsistency to one agreed PascalCase default per collection —
MongoMqttDatabaseCollectionOptions.DefaultMqtts / DefaultMqttConnections / DefaultBrokers /
DefaultBrokerUsers — matching the documented options defaults and the real demo host's explicit
appsettings.Development.json values (so the running app and any consumer with an explicit
Collections section are unaffected). A null or whitespace-only override falls back to the default.
Usage
using ApiFeatures.Mqtt.MongoDatabases.Extensions;
using ApiFeatures.Mqtt.MongoDatabases.Models.Options;
var mongoOptions = new MongoMqttDatabaseOptions
{
Host = "localhost",
Port = 27017,
DatabaseName = "demo-mqtt",
// Username/Password/AuthenticationDatabase/ReplicaSet/UseTls optional
Collections = new MongoMqttDatabaseCollectionOptions
{
Mqtts = "Mqtts",
MqttConnections = "MqttConnections",
Brokers = "Brokers",
BrokerUsers = "BrokerUsers"
}
};
builder.Services.WithMongoDatabase(mongoOptions);
WithMongoDatabase registers MongoMqttDatabaseContext plus IMqttService, IMqttConnectionService,
IBrokerService, and IBrokerUserService (all keyed ApiFeatures.Mqtt.Cores.Constants.ServiceKeys.Default,
scoped). It does not register IBrokerUserPasswordHasher — the host must also call
AddMqttsModule(...) (from ApiFeatures.Mqtt.Apis), which registers the hasher implementation
unconditionally; BrokerUserService resolves it lazily at request time from the same container, so call
order between WithMongoDatabase and AddMqttsModule does not matter.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- ApiFeatures.Modules.Cores (>= 9.0.2)
- ApiFeatures.Mqtt.Cores (>= 9.1.0)
- DataMagic.MongoDatabase (>= 1.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.11)
- MongoDB.Driver (>= 3.5.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.