SetNet.DependencyInjection 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SetNet.DependencyInjection --version 1.1.0
                    
NuGet\Install-Package SetNet.DependencyInjection -Version 1.1.0
                    
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="SetNet.DependencyInjection" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SetNet.DependencyInjection" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SetNet.DependencyInjection" />
                    
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 SetNet.DependencyInjection --version 1.1.0
                    
#r "nuget: SetNet.DependencyInjection, 1.1.0"
                    
#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 SetNet.DependencyInjection@1.1.0
                    
#: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=SetNet.DependencyInjection&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SetNet.DependencyInjection&version=1.1.0
                    
Install as a Cake Tool

<p align="center"> <img src="https://raw.githubusercontent.com/Povstalez/SetNet/master/assets/icon.png" alt="SetNet" width="96"> </p>

SetNet.DependencyInjection

Constructor injection for your SetNet message handlers.

By default SetNet discovers your [MessageHandler] classes by reflection and instantiates them with a parameterless constructor. This package bridges handler construction to a Microsoft.Extensions.DependencyInjection container, so your handlers can take injected dependencies — services, loggers, repositories, game state — through their constructors instead of a bare new.

Use it whenever your handlers need collaborators, or when you already run on the .NET Generic Host / ASP.NET Core. Pairs naturally with SetNet.Hosting and SetNet.HealthChecks.

Install

dotnet add package SetNet
dotnet add package SetNet.DependencyInjection

Usage

Call provider.UseSetNetHandlers() before you construct your BaseClient / BaseServer — handlers are discovered and built when the executor is created inside that constructor, so the seam must already be in place.

using Microsoft.Extensions.DependencyInjection;
using SetNet.DependencyInjection;

var services = new ServiceCollection();

// your app dependencies:
services.AddSingleton<IPlayerStore, SqlPlayerStore>();
services.AddSingleton<ILeaderboard, Leaderboard>();

// (optional) register the handler itself so it's resolved from the container
// instead of activated ad hoc — do this if the handler is a singleton or
// needs container-managed lifetime/disposal:
services.AddSingleton<PlayerMoveHandler>();

var provider = services.BuildServiceProvider();

provider.UseSetNetHandlers();          // <-- install the seam FIRST

var server = new MyServer(config);     // handlers discovered & built here
await server.StartAsync();

A handler with an injected dependency looks exactly like a normal handler — the constructor argument is filled by the container:

[MessageHandler((ushort)MessageTypes.PlayerMove)]
public class PlayerMoveHandler : IServerMessageHandler<PlayerMoveMessage>
{
    private readonly IPlayerStore _players;

    public PlayerMoveHandler(IPlayerStore players) => _players = players;   // injected

    public async Task HandleAsync(BasePeer peer, PlayerMoveMessage message)
    {
        await _players.UpdatePositionAsync(message.PlayerId, message.X, message.Y);
    }
}

How it works

UseSetNetHandlers() sets the core HandlerActivator.Factory seam to route every handler through your provider:

Case Resolution
Handler type is registered in the container Resolved via provider.GetService(type) (honours its registered lifetime)
Handler type is not registered Built with ActivatorUtilities.CreateInstance — constructor injection still works, pulling each ctor arg from the container

So you get injection either way; registering the handler only matters when you want the container to own its lifetime.

Notes

  • Ordering is load-bearing. UseSetNetHandlers() must run before the BaseClient / BaseServer constructor. Called afterward, the executor has already built its handlers with the default parameterless activator.
  • One process-wide factory. The seam is a single static HandlerActivator.Factory; the last provider to call UseSetNetHandlers() wins. Typical apps have one provider, so this is a non-issue.
  • Lifetime. Handlers are constructed once at executor build time. Prefer singleton/transient collaborators; for per-message scoping, inject an IServiceScopeFactory and open a scope inside HandleAsync.
  • Works identically on client (BaseClient) and server (BaseServer).

Documentation & source

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 101 8/5/2026
1.1.0 129 7/2/2026