Femur.DependencyInjection 0.0.31

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

Femur.DependencyInjection

A library for proxying services from one ServiceProvider into another ServiceCollection, preserving lifetimes and handling edge cases like open generics and singleton instances.

Overview

This package allows you to share services across different DI containers by creating factory delegates that resolve from a source ServiceProvider. This is useful when you need to maintain service instances across container boundaries while preserving lifetimes and instance sharing.

Installation

dotnet add package Femur.DependencyInjection

Basic Usage

using Femur.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

// Create a source container with some services
var sourceServices = new ServiceCollection();
sourceServices.AddSingleton<IMyService, MyService>();
sourceServices.AddScoped<IOtherService, OtherService>();
var sourceProvider = sourceServices.BuildServiceProvider();

// Create a target container and proxy the services
var targetServices = new ServiceCollection();
targetServices.AddProxiedServices(sourceServices, sourceProvider);

// Build the target provider
var targetProvider = targetServices.BuildServiceProvider();

// Services resolved from target provider come from source provider
var service = targetProvider.GetRequiredService<IMyService>();

How It Works

The AddProxiedServices extension method analyzes each service descriptor and handles it appropriately:

1. Open Generic Types

Services registered with open generic types (e.g., IOptions<>) are copied as-is to preserve their implementation type:

sourceServices.AddSingleton(typeof(IOptions<>), typeof(OptionsManager<>));

2. Singleton Instances

Services registered with ImplementationInstance are copied directly to ensure the same instance is used:

var instance = new MyService();
sourceServices.AddSingleton<IMyService>(instance);
// The exact same instance will be available in the target container

3. Factory Functions

By default, services registered with factory functions are proxied to resolve from the source provider, ensuring singleton instances are shared:

sourceServices.AddSingleton<IMyService>(_ => new MyService());
// The target provider will resolve the same singleton instance from the source

4. Regular Types

All other services are registered with factory delegates that resolve from the source provider:

sourceServices.AddTransient<IMyService, MyService>();
// Resolved via factory: _ => sourceProvider.GetRequiredService<IMyService>()

Advanced Options

Filtering Services

You can filter which services to proxy using the ShouldSkipService predicate:

var options = new ProxyOptions
{
    ShouldSkipService = descriptor => descriptor.ServiceType == typeof(ILoggerProvider)
};

targetServices.AddProxiedServices(sourceServices, sourceProvider, options);

Preserving Factory Behavior

By default, factory functions are proxied to share instances. If you want to preserve the original factory behavior (creating new instances):

var options = new ProxyOptions
{
    PreserveExistingFactories = true
};

targetServices.AddProxiedServices(sourceServices, sourceProvider, options);

Important Considerations

Lifecycle Management

  • The source ServiceProvider must remain alive for as long as the target provider needs to resolve services
  • Disposing the source provider before the target provider can cause resolution failures
  • Singleton instances are shared between both providers

Service Resolution

  • Services resolved from the target provider are actually resolved from the source provider
  • This means service lifetimes are controlled by the source provider
  • Scoped services will use the source provider's scope, not the target's

Use Cases

This library is particularly useful for:

  1. Bootstrap Scenarios: Setting up services before the main application container is built
  2. Service Sharing: Sharing expensive singleton instances across container boundaries
  3. Gradual Migration: Moving services between containers while maintaining compatibility
  4. Testing: Creating isolated test containers that share specific services

Example: Bootstrap Logger

// Create a bootstrap logger with its own container
var bootstrapServices = new ServiceCollection();
bootstrapServices.AddLogging(builder => builder.AddConsole());
var bootstrapProvider = bootstrapServices.BuildServiceProvider();

// Use the logger during startup
var logger = bootstrapProvider.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Starting up...");

// Later, transfer the logging services to the main container
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddProxiedServices(bootstrapServices, bootstrapProvider);

// The main app uses the same logger instances from bootstrap
var app = builder.Build();

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 is compatible.  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 is compatible.  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 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on Femur.DependencyInjection:

Package Downloads
Femur.Logging.Bootstrap

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.31 207 3/30/2026
0.0.30 277 1/25/2026