File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -81,7 +81,13 @@ builder.Services
8181 By default, all controllers within the executing assembly are
8282 discovered (just pass nothing here). To provide a list of assemblies
8383 explicitly, pass an array of Assembly[] here.
84- */ );
84+ */ )
85+ /*
86+ optionally, set System.Text.Json serialization default for use with
87+ [FromPayload] in the controllers. We can specify a JsonSerializerOptions
88+ or use JsonSerializerDefaults, useful for case-sensitivity or comment-handling
89+ */
90+ .AddMqttDefaultJsonOptions (new JsonSerializerOptions (JsonSerializerDefaults .Web ));
8591
8692var app = builder .Build ();
8793
@@ -129,6 +135,23 @@ public class MqttWeatherForecastController : MqttBaseController // Inherit from
129135
130136 return Ok ();
131137 }
138+
139+ // Supports binding JSON message payload to parameters with [FromPayload] attribute,
140+ // Similar to ASP.NET Core [FromBody]
141+ [MqttRoute (" {deviceName}/telemetry" )]
142+ public async Task NewTelemetry (string deviceName , [FromPayload ] Telemetry telemetry )
143+ {
144+ // here telemetry is JSON-deserialized from message payload to type Telemetry
145+ bool success = await DoSomething (telemetry );
146+ if (success ) {
147+ await Ok ();
148+ return ;
149+ }
150+ else {
151+ await BadMessage ();
152+ return ;
153+ }
154+ }
132155}
133156```
134157
You can’t perform that action at this time.
0 commit comments