I got this error when using await in front of // Start the server
await server.StartAsync();
"The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'."
using System;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Server;
using MQTTnet;
using System.Text;
using static System.Console;
namespace MqttBroker
{
class Program
{
static void Main(string[] args)
{
// Create the options for MQTT Brokervar options =new MqttServerOptionsBuilder()
//Set endpoint to localhost
.WithDefaultEndpoint();
// Create a new mqtt servervar server =new MqttFactory().CreateMqttServer(options.Build());
//Add Interceptor for logging incoming messages
server.InterceptingPublishAsync += Server_InterceptingPublishAsync;
// Start the server//await server.StartAsync();
server.StartAsync();
// Keep application running until user press a key
ReadLine();
Task Server_InterceptingPublishAsync(InterceptingPublishEventArgs arg)
{
// Convert Payload to stringvar payload = arg.ApplicationMessage?.Payload == null ? null : Encoding.UTF8.GetString(arg.ApplicationMessage?.Payload);
WriteLine(
" TimeStamp: {0} -- Message: ClientId = {1}, Topic = {2}, Payload = {3}, QoS = {4}, Retain-Flag = {5}",
DateTime.Now,
arg.ClientId,
arg.ApplicationMessage?.Topic,
payload,
arg.ApplicationMessage?.QualityOfServiceLevel,
arg.ApplicationMessage?.Retain);
return Task.CompletedTask;
}
}
}
If I did comment out "//await server.StartAsync();" and used server.StartAsync(); it's ok.
So,let me know solution,and also I am waiting for MQTT broker with username/password authenticated from clients request.
Bigberm
Hi,
I got this error when using await in front of // Start the server await server.StartAsync();
"The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'."
How to fix it?
Thank you,
Bigberm