Femur.DependencyInjection
0.0.31
dotnet add package Femur.DependencyInjection --version 0.0.31
NuGet\Install-Package Femur.DependencyInjection -Version 0.0.31
<PackageReference Include="Femur.DependencyInjection" Version="0.0.31" />
<PackageVersion Include="Femur.DependencyInjection" Version="0.0.31" />
<PackageReference Include="Femur.DependencyInjection" />
paket add Femur.DependencyInjection --version 0.0.31
#r "nuget: Femur.DependencyInjection, 0.0.31"
#:package Femur.DependencyInjection@0.0.31
#addin nuget:?package=Femur.DependencyInjection&version=0.0.31
#tool nuget:?package=Femur.DependencyInjection&version=0.0.31
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
ServiceProvidermust 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:
- Bootstrap Scenarios: Setting up services before the main application container is built
- Service Sharing: Sharing expensive singleton instances across container boundaries
- Gradual Migration: Moving services between containers while maintaining compatibility
- 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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.0)
- Microsoft.Extensions.Logging.Abstractions (>= 3.1.0)
- Microsoft.Extensions.Options (>= 3.1.0)
- System.Memory (>= 4.6.3)
- System.Reflection.Emit (>= 4.7.0)
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.2)
- Microsoft.Extensions.Options (>= 10.0.2)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
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.