-
Couldn't load subscription status.
- Fork 1.1k
Refactoring MQTTnet.AspNetCore #2103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
@dotnet-policy-service agree |
…IMqttClientAdapterFactory
…in certain areas.
… with TestEnvironment.
|
The development of this PR has been completed and has passed the same one-sided tests as MQTTnet(Client) and MQTTnet.Server. |
After several submissions, I have implemented the following refactorings. See also #2101
IMqttBuilder
IMqttBuilder is used to provide common functions for build MqttServer and MqttClient, such as logging.
Extensions
IMqttServerBuilder
Extensions
IMqttClientBuilder
Extensions
New implementation of IMqttClientAdapterFactory
The SocketConnection that the original MqttClientConnectionContextFactory depends on is a copy of the early code of the kestrel project. It is now deleted and replaced by
the IConnectionFactory service of Asp.NetCore to create ConnectionContext instead of SocketConnectionClientConnectionContext, which is a wrapper class for Stream and now supports TCP, TLS, WS and WSS. See also #2104 #2105IConnectionFactory only provides an asynchronous ConnectAsync() method. In order to call this asynchronous method, we need to modify the CreateClientAdapter method of IMqttClientAdapterFactory toValueTask<IMqttChannelAdapter> CreateClientAdapterAsync().Now, the implementation type of IMqttClientAdapterFactory is AspNetCoreMqttClientAdapterFactory, which is an internal modified type and is used by users through IMqttClientBuilder.UseAspNetCoreMqttClientAdapterFactory().
The dependency chain is IMqttClientAdapterFactory-->AspNetCoreMqttClientAdapterFactory-->MqttClientChannelAdapter-->MqttChannel-->ClientConnectionContext
Split MqttHostedServer
MqttHostedServer acts as both MqttServer and HostedService, which makes it difficult to register the service and start the service background.
It has now been split into AspNetCoreMqttServer and AspNetCoreMqttHostedServer. AspNetCoreMqttHostedServer inherits BackgroundService. In its ExecuteAsync method, it waits for the application to start and then starts AspNetCoreMqttServer. #2102
Split MqttConnectionHandler
MqttConnectionHandler no longer implements the IMqttServerAdapter interface. The implementation type of IMqttServerAdapter is AspNetCoreMqttServerAdapter.
The dependency chain is IMqttServerAdapter-->AspNetCoreMqttServerAdapter-->MqttConnectionHandler-->MqttServerChannelAdapter-->MqttChannel-->Kestrel ConnectionContext
Add MqttBufferWriterPool in server mode
MqttBufferWriterPool pools the MqttBufferWriters of channels whose lifecycles are less than one minute to reduce the number of MqttBufferWriters created when clients frequently connect and disconnect.
Add MqttConnectionMiddleware
MqttConnectionMiddleware allows a single port to support both mqtt and mqtt-overt-websocket protocols. It is ultimately applied by IConnectionBuilder.UseMqtt(MqttProtocols).
Add KestrelServerOptions.ListenMqtt(MqttProtocols) extensions
This extension perfectly adapts the listening options of MqttServerOptions to kestrel, allowing users to continue to use the familiar MqttServerOptionsBuilder. In addition, we also provide enhanced httpsOptions configuration.
Make all implementation types internal
Now all implementation types have been modified to internal, and use InternalsVisibleToAttribute to expose them to the specified assembly.
Logger
The new AspNetCoreMqttNetLogger type is now the default IMqttNetLogger, which writes log content to Microsoft.Extensions.Logging.
Nullable
Nullable feature is enabled.
UnitTest
The new
AspNetCoreTestEnvironmentis added to the test environment and participates in the test at the same time as the defaultTestEnvironment.Exception
Now the exception types and exception behaviors of MqttChannel are exactly the same as those of MqttChannelAdapter.