dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.5k stars 1.07k forks source link

Cannot find MqttServerFactory? #2085

Closed JansthcirlU closed 1 month ago

JansthcirlU commented 1 month ago

Describe the bug

I've created a new class library project, added MQTTnet to it, but when trying to make a new MqttServerFactory, the IDE complains that that class cannot be found.

Which component is your bug related to?

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of MQTTnet '4.3.7.1207'.
  2. Using .NET 8
  3. Create a new class library using dotnet new classlib.
  4. Add a field of type MqttServerFactory.
  5. See error CS0246: The type or namespace name 'MqttServerFactory' could not be found (are you missing a using directive or an assembly reference?).

Expected behavior

The MqttServerFactory class, which lives inside the Server namespace and is public, should be recognized.

Screenshots

image

Code example

using Microsoft.Extensions.Hosting;
using MQTTnet.Server;

namespace MqttHosting;

public class MqttHost : IHostedService
{
    private readonly MqttServerFactory _serverFactory;

    public MqttHost(MqttServerFactory serverFactory)
    {
        _serverFactory = serverFactory;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("server starting");
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("server stopping");
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="MQTTnet" Version="4.3.7.1207" />
  </ItemGroup>

</Project>
rg0815 commented 1 month ago

Had the same problem. There is an error in the given example.

Solution: Replace "MqttServerFactory" with "MqttFactory". This should solve your problem.

SeppPenner commented 1 month ago

There is an error in the given example.

No, the wiki is just not used anymore, just for version 3.x. For version 4.x, refer to the https://github.com/dotnet/MQTTnet/tree/master/Samples folder, please.