Skip to content

Commit 0f55395

Browse files
authored
Merge pull request #4 from seikosantana/master
Updated README.md
2 parents c17d201 + b8fb314 commit 0f55395

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

README.md

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,49 @@ This library is available as a nuget package: <https://www.nuget.org/packages/MQ
4848
## Usage
4949

5050
Install this package and MQTTnet from nuget.
51+
For dotnet CLI:
52+
```bash
53+
dotnet add package MQTTnet.AspNetCore.Routing
54+
```
5155

52-
Modify your `Startup.cs` with the following options:
56+
Example configuration on ASP.NET Core 6 MVC Configuration
5357

5458
```csharp
55-
public void ConfigureServices(IServiceCollection services)
56-
{
57-
// ... All your other configuration ...
58-
59-
// Identify and build routes
60-
services.AddMqttControllers(
61-
/*
62-
By default, all controllers within the executing assembly are
63-
discovered (just pass nothing here). To provide a list of assemblies
64-
explicitly, pass an array of Assembly[] here.
65-
*/
66-
);
67-
68-
services
69-
.AddHostedMqttServerWithServices(s =>
70-
{
71-
// Optionally set server options here
72-
s.WithoutDefaultEndpoint();
73-
74-
// Enable Attribute routing
75-
s.WithAttributeRouting(
76-
/*
77-
By default, messages published to topics that don't
78-
match any routes are rejected. Change this to true
79-
to allow those messages to be routed without hitting
80-
any controller actions.
81-
*/
82-
allowUnmatchedRoutes: false
83-
);
84-
})
85-
.AddMqttConnectionHandler()
86-
.AddConnections();
87-
}
59+
using MQTTnet.AspNetCore;
60+
using MQTTnet.AspNetCore.Routing;
61+
62+
var builder = WebApplication.CreateBuilder(args);
63+
builder.WebHost.ConfigureKestrel(o =>
64+
{
65+
o.ListenAnyIP(iotaboardMqttSettings.Port, l => l.UseMqtt());
66+
o.ListenAnyIP(iotaboardHttpSettings.Port);
67+
}
68+
);
69+
70+
// Configure MQTTServer service
71+
builder.Services
72+
.AddHostedMqttServerWithServices(o =>
73+
{
74+
// other configurations
75+
o.WithoutDefaultEndpoint();
76+
})
77+
.AddMqttConnectionHandler()
78+
.AddConnections()
79+
.AddMqttControllers( // <== NOTICE THIS PART
80+
/*
81+
By default, all controllers within the executing assembly are
82+
discovered (just pass nothing here). To provide a list of assemblies
83+
explicitly, pass an array of Assembly[] here.
84+
*/);
85+
86+
var app = builder.Build();
87+
88+
app.MapControllers();
89+
app.UseMqttServer(server => {
90+
// other MqttServer configurations, for example client connect intercepts
91+
server.WithAttributeRouting(app.Services, allowUnmatchedRoutes: false);
92+
});
93+
app.Run();
8894
```
8995

9096
Create your controllers by inheriting from MqttBaseController and adding actions to it like so:
@@ -132,7 +138,7 @@ public class MqttWeatherForecastController : MqttBaseController // Inherit from
132138

133139
## Contributions
134140

135-
Contributions are welcome. Please open an issue to discuss your idea prior to sending a PR.
141+
Contributions are welcome. Please open an issue to discuss your idea prior to sending a PR.
136142

137143
## MIT License
138144

0 commit comments

Comments
 (0)