zarusz / SlimMessageBus

Lightweight message bus interface for .NET (pub/sub and request-response) with transport plugins for popular message brokers.
Apache License 2.0
475 stars 78 forks source link

[Host.Kafka] Kafka consumers started twice on Host.Run #213

Closed mib85 closed 8 months ago

mib85 commented 8 months ago

Hello,

I'm noticing unexpected behavior when using SlimMessageBus.Host.Kafka with .NET Generic Host.

When I run this code

using Microsoft.Extensions.Hosting;
using SlimMessageBus.Host;
using SlimMessageBus.Host.Kafka;

namespace SlimMessageBusTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((_, services) =>
                {
                    services.AddSlimMessageBus(mbb =>
                    {
                        mbb.WithProviderKafka(cfg => cfg.BrokerList = "localhost:9092");
                    });
                })
                .Build()
                .Run();
        }
    }
}

I get this output on Console

info: SlimMessageBus.Host.Kafka.KafkaMessageBus[0]
      Creating producers
info: SlimMessageBus.Host.MessageBusBase[0]
      Starting consumers for Main bus...
info: SlimMessageBus.Host.MessageBusBase[0]
      Starting consumers for Main bus...
info: SlimMessageBus.Host.MessageBusBase[0]
      Creating consumers
info: SlimMessageBus.Host.MessageBusBase[0]
      Creating consumers
info: SlimMessageBus.Host.MessageBusBase[0]
      Started consumers for Main bus
info: SlimMessageBus.Host.MessageBusBase[0]
      Started consumers for Main bus
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production

It seems that the consumers are started twice. I don't know if this is normal/managed behavior or if I should use a different configuration.

This is my csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.2" />
    <PackageReference Include="SlimMessageBus.Host.Kafka" Version="2.2.2" />
  </ItemGroup>
</Project>

I also noticed that when updating Microsoft.Extensions.Hosting.WindowsServices to version 8.0.0 this behavior does not occur and the consumers are started only once.

zarusz commented 8 months ago

Thanks for raising. Sounds like this is a bug. Let me try to replicate and look into this.

zarusz commented 8 months ago

hey @mib85 please check version 2.2.3 - it should be resolved. Please let me know if any issues. Thanks!

mib85 commented 8 months ago

Hi @zarusz. I did some tests and everything seems fine. Thank you very much!