Whatsmeow.Extensions.Hosting 1.0.0-rc.4

This is a prerelease version of Whatsmeow.Extensions.Hosting.
dotnet add package Whatsmeow.Extensions.Hosting --version 1.0.0-rc.4
                    
NuGet\Install-Package Whatsmeow.Extensions.Hosting -Version 1.0.0-rc.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Whatsmeow.Extensions.Hosting" Version="1.0.0-rc.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Whatsmeow.Extensions.Hosting" Version="1.0.0-rc.4" />
                    
Directory.Packages.props
<PackageReference Include="Whatsmeow.Extensions.Hosting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Whatsmeow.Extensions.Hosting --version 1.0.0-rc.4
                    
#r "nuget: Whatsmeow.Extensions.Hosting, 1.0.0-rc.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Whatsmeow.Extensions.Hosting@1.0.0-rc.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Whatsmeow.Extensions.Hosting&version=1.0.0-rc.4&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Whatsmeow.Extensions.Hosting&version=1.0.0-rc.4&prerelease
                    
Install as a Cake Tool

Whatsmeow.Extensions.Hosting

Dependency injection and a generic host for Whatsmeow.Client. AddWhatsmeow() binds the options from the Whatsmeow configuration section, starts the sidecar with the host and stops it after every hosted service, declares sessions as keyed IWhatsAppClient services, and runs IWhatsAppEventHandler<TEvent> handlers in a DI scope per event.

This is not an official WhatsApp client and has nothing to do with WhatsApp or Meta.

Quick start

Two sessions that answer every text with the same text:

using Microsoft.Extensions.DependencyInjection;
using Whatsmeow.Events;
using Whatsmeow.Extensions.Hosting;
using Whatsmeow.History;

namespace EchoBot;

/// <summary>The echo bot's setup: one sidecar for the host, two sessions, their handlers, and pairing on the console.</summary>
public static class EchoBotServices
{
	/// <summary>The first session's id, which is also its service key.</summary>
	public const string Alice = "alice";

	/// <summary>The second session's id, which is also its service key.</summary>
	public const string Bob = "bob";

	public static WhatsmeowBuilder AddEchoBot(this IServiceCollection services)
	{
		var whatsmeow = services
			.AddWhatsmeow()
			.AddSession(Alice)
			.AddSession(Bob)
			.AddHandler<MessageReceived, EchoHandler>()
			.AddHandler<SessionStateChanged, StateLog>()
			.AddHandler<HistoryReceived, HistoryLog>();
		_ = services.AddHostedService<QrPairing>();
		return whatsmeow;
	}
}
using System.Security.Cryptography;
using System.Text;
using Microsoft.Extensions.Logging;
using Whatsmeow;
using Whatsmeow.Events;
using Whatsmeow.Extensions.Hosting;

namespace EchoBot;

/// <summary>Answers each text a session receives with the same text, in the same chat.</summary>
public sealed partial class EchoHandler(ILogger<EchoHandler> logger) : IWhatsAppEventHandler<MessageReceived>
{
	private const string Prefix = "echo: ";

	public async ValueTask HandleAsync(MessageReceived e, IWhatsAppClient client, CancellationToken cancellationToken)
	{
		// Never a status (status@broadcast), which cannot be answered; never the account's own texts, nor an echo: two
		// sessions that text each other would echo each other forever.
		if (IsBroadcast(e.Source.Chat) || e.Source.IsFromMe || e.Content is not TextContent { Text: var text } || text.StartsWith(Prefix, StringComparison.Ordinal))
			return;

		// A message can come again, after a stop during the send or a crash before its ack; the echo's id is the same
		// each time, and a send of an id already sent is never sent again, so each text is echoed once.
		_ = await client.SendTextAsync(e.Source.Chat, Prefix + text, EchoId(e), cancellationToken: cancellationToken).ConfigureAwait(false);
		Echoed(logger, e.Id.Value, e.SessionId.Value, e.Timestamp);
	}

	private static bool IsBroadcast(Jid chat) => string.Equals(chat.Server, "broadcast", StringComparison.Ordinal);

	/// <summary>The echo's id, from the session, the chat and the message's id, in the only format a send takes: <c>3EB0</c> and 18 uppercase hex digits.</summary>
	private static MessageId EchoId(MessageReceived e) =>
		MessageId.From("3EB0" + Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes($"{e.SessionId.Value}\n{e.Source.Chat}\n{e.Id.Value}")))[..18]);

	[LoggerMessage(1, LogLevel.Information, "Echoed {MessageId} of session {SessionId}, sent at {Timestamp:O}")]
	private static partial void Echoed(ILogger logger, string messageId, string sessionId, DateTimeOffset timestamp);
}

The full sample, with its QR code pairing and OpenTelemetry, is dotnet/samples/EchoBot. Hosting and testing covers configuration, keyed sessions and the host's lifecycle.

License: MPL-2.0.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Whatsmeow.Extensions.Hosting:

Package Downloads
Whatsmeow.Testing

FakeWhatsAppClient, an in-memory IWhatsAppClient to test Whatsmeow.NET handlers without WhatsApp, and UseFakeClient for a host.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-rc.4 81 11/12/2026
1.0.0-rc.2 84 11/11/2026
1.0.0-rc.1 96 11/11/2026 1.0.0-rc.1 is deprecated because it has critical bugs.