Stashbox.AspNetCore.Hosting
4.2.0
See the version list below for details.
dotnet add package Stashbox.AspNetCore.Hosting --version 4.2.0
NuGet\Install-Package Stashbox.AspNetCore.Hosting -Version 4.2.0
<PackageReference Include="Stashbox.AspNetCore.Hosting" Version="4.2.0" />
paket add Stashbox.AspNetCore.Hosting --version 4.2.0
#r "nuget: Stashbox.AspNetCore.Hosting, 4.2.0"
// Install Stashbox.AspNetCore.Hosting as a Cake Addin #addin nuget:?package=Stashbox.AspNetCore.Hosting&version=4.2.0 // Install Stashbox.AspNetCore.Hosting as a Cake Tool #tool nuget:?package=Stashbox.AspNetCore.Hosting&version=4.2.0
stashbox-extensions-dependencyinjection
This repository contains Stashbox integrations for ASP.NET Core, .NET Generic Host and simple ServiceCollection based applications.
Package | Version |
---|---|
Stashbox.Extensions.Dependencyinjection | |
Stashbox.Extensions.Hosting | |
Stashbox.AspNetCore.Hosting | |
Stashbox.AspNetCore.Multitenant |
Options turned on by default:
- Automatic tracking and disposal of
IDisposable
andIAsyncDisposable
services. - Lifetime validation for
Developement
environments, but can be extended to all environment types.
Table of Contents
- ASP.NET Core
- .NET Generic Host
- ServiceCollection Based Applications
- Additional IServiceCollection Extensions
ASP.NET Core
The following example shows how you can integrate Stashbox (with the Stashbox.Extensions.Hosting
package) as the default IServiceProvider
implementation into your ASP.NET Core application:
ASP.NET Core 5
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(String[] args)
{
return Host.CreateDefaultBuilder(args)
.UseStashbox(container => // Optional configuration options.
{
// This one enables the lifetime validation for production environments too.
container.Configure(config => config.WithLifetimeValidation());
})
.ConfigureContainer<IStashboxContainer>((context, container) =>
{
// Execute a dependency tree validation.
if (context.HostingEnvironment.IsDevelopment())
container.Validate();
})
.ConfigureWebHostDefaults(
webBuilder => webBuilder
.UseStartup<Startup>());
}
}
You can also use the ConfigureContainer()
method in your Startup
class to use further configuration options:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Your service configuration.
}
public void ConfigureContainer(IStashboxContainer container)
{
// Your container configuration.
container.Configure(config => config.WithLifetimeValidation());
}
public void Configure(IApplicationBuilder app)
{
// Your application configuration.
}
}
ASP.NET Core 6
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseStashbox(container => // Optional configuration options.
{
// This one enables the lifetime validation for production environments too.
container.Configure(config => config.WithLifetimeValidation());
});
builder.Host.ConfigureContainer<IStashboxContainer>((context, container) =>
{
// Execute a dependency tree validation.
if (context.HostingEnvironment.IsDevelopment())
container.Validate();
});
Controller / View activation
By default the ASP.NET Core framework uses the DefaultControllerActivator
to instantiate controllers, but it uses the ServiceProvider
only for instantiating their constructor dependencies. This behaviour could hide important errors Stashbox would throw in case of a misconfiguration, so it's recommended to let Stashbox activate your controllers and views.
You can enable this by adding the following options to your service configuration:
ASP.NET Core 5
public void ConfigureServices(IServiceCollection services)
{
// For controllers only.
services.AddControllers()
.AddControllersAsServices();
// For controllers and views.
services.AddControllersWithViews()
.AddControllersAsServices()
.AddViewComponentsAsServices();
}
ASP.NET Core 6
// For controllers only.
builder.Services.AddControllers()
.AddControllersAsServices();
// For controllers and views.
builder.Services.AddControllersWithViews()
.AddControllersAsServices()
.AddViewComponentsAsServices();
Multitenant
The Stashbox.AspNetCore.Multitenant
package provides support for multitenant applications with a component called TenantDistributor
. It's responsible for the following tasks:
- Create / maintain the application level Root Container. This container is used to hold the default service registrations for your application.
- Configure / maintain tenant specific containers. These containers are used to override the default services with tenant specific registrations.
- Tenant identification. Determines the tenant Id based on the current context. To achieve that, you have to provide an
ITenantIdExtractor
implementation.
// The type used to extract the current tenant identifier.
// This implementation shows how to extract the tenant id from a HTTP header.
public class HttpHeaderTenantIdExtractor : ITenantIdExtractor
{
public Task<object> GetTenantIdAsync(HttpContext context)
{
if (!context.Request.Headers.TryGetValue("TENANT-ID", out var value))
return Task.FromResult<object>(null);
return Task.FromResult<object>(value.First());
}
}
ASP.NET Core 5
public static IHostBuilder CreateHostBuilder(String[] args)
{
return Host.CreateDefaultBuilder(args)
.UseStashboxMultitenant<HttpHeaderTenantIdExtractor>(
distributor => // The tenant distributor configuration options.
{
// The default service registration.
// It also could be registered into the default
// service collection through the ConfigureServices() api.
distributor.RootContainer.Register<IDependency, DefaultDependency>();
// Configure tenants.
distributor.ConfigureTenant("TenantA", container =>
// Register tenant specific service override
container.Register<IDependency, TenantASpecificDependency>());
distributor.ConfigureTenant("TenantB", container =>
// Register tenant specific service override
container.Register<IDependency, TenantBSpecificDependency>());
})
.ConfigureContainer<TenantDistributor>((context, distributor) =>
{
// Validate the root container and all the tenants.
if (context.HostingEnvironment.IsDevelopment())
distributor.Validate();
})
.ConfigureWebHostDefaults(
webBuilder => webBuilder
.UseStartup<Startup>());
}
ASP.NET Core 6
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseStashboxMultitenant<HttpHeaderTenantIdExtractor>(
distributor => // The tenant distributor configuration options.
{
// The default service registration.
// It also could be registered into the default
// service collection through the ConfigureServices() api.
distributor.RootContainer.Register<IDependency, DefaultDependency>();
// Configure tenants.
distributor.ConfigureTenant("TenantA", container =>
// Register tenant specific service override
container.Register<IDependency, TenantASpecificDependency>());
distributor.ConfigureTenant("TenantB", container =>
// Register tenant specific service override
container.Register<IDependency, TenantBSpecificDependency>());
});
builder.Host.ConfigureContainer<TenantDistributor>((context, distributor) =>
{
// Validate the root container and all the tenants.
if (context.HostingEnvironment.IsDevelopment())
distributor.Validate();
});
With this example setup, you can differentiate tenants in a per-request basis identified by a HTTP header, where every tenant gets their overridden services.
.NET Generic Host
The following example adds Stashbox (with the Stashbox.Extensions.Hosting
package) as the default IServiceProvider
implementation into your .NET Generic Host application:
public class Program
{
public static async Task Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.UseStashbox(container => // Optional configuration options.
{
// This one enables the lifetime validation for production environments too.
container.Configure(config => config.WithLifetimeValidation());
})
.ConfigureContainer<IStashboxContainer>((context, container) =>
{
// Execute a dependency tree validation.
if (context.HostingEnvironment.IsDevelopment())
container.Validate();
})
.ConfigureServices((context, services) =>
{
services.AddHostedService<Service>();
}).Build();
await host.RunAsync();
}
}
ServiceCollection Based Applications
With the Stashbox.Extensions.Dependencyinjection
package you can replace Microsoft's built-in dependency injection container with Stashbox. This package contains the core functionality used by the Stashbox.Extensions.Hosting
, Stashbox.AspNetCore.Hosting
and Stashbox.AspNetCore.Multitenant
packages.
The following example shows how you can use this integration:
public class Program
{
public static async Task Main(string[] args)
{
// Create the service collection.
var services = new ServiceCollection();
// Configure your service collection.
services.AddLogging();
services.AddOptions();
// Add your services.
services.AddScoped<IService, Service>();
// Integrate Stashbox with the collection and grab your ServiceProvider.
var serviceProvider = services.UseStashbox(container => // Optional configuration options.
{
container.Configure(config => config.WithLifetimeValidation());
});
// Start using the application.
using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<IService>();
await service.DoSomethingAsync();
}
}
}
Or you can use your own StashboxContainer
to integrate with the ServiceCollection
:
public class Program
{
public static async Task Main(string[] args)
{
// Create your container.
var container = new StashboxContainer(config => // Optional configuration options.
{
config.WithLifetimeValidation();
});
// Create the service collection.
var services = new ServiceCollection();
// Configure your service collection.
services.AddLogging();
services.AddOptions();
// Add your services.
services.AddScoped<IService, Service>();
// Or add them through Stashbox.
container.RegisterScoped<IService, Service>();
// Integrate Stashbox with the collection.
services.UseStashbox(container);
// Execute a dependency tree validation.
container.Validate();
// Start using the application.
await using (var scope = container.BeginScope())
{
var service = scope.Resolve<IService>();
await service.DoSomethingAsync();
}
}
}
Additional IServiceCollection
Extensions
Most of Stashbox's service registration functionalities are available as extension methods of IServiceCollection
.
-
class Service2 : IService2 { private readonly IService service; public Service2(IService service) { this.service = service; } } var services = new ServiceCollection(); services.AddTransient<IService, Service>(); // Name-less registration. services.AddTransient<IService, AnotherService>("serviceName"); // Register dependency with name. services.AddTransient<IService2, Service2>(config => // Inject the named service as dependency. config.WithDependencyBinding<IService>( "serviceName" // Name of the dependency. ));
Service configuration with Stashbox's Fluent Registration API:
var services = new ServiceCollection(); services.AddTransient<IService, Service>(config => config.WithFactory<IDependency>(dependency => new Service(dependency)).AsImplementedTypes());
-
class ServiceDecorator : IService { private readonly IService decorated; public ServiceDecorator(IService service) { this.decorated = service; } } var services = new ServiceCollection(); services.AddTransient<IService, Service>(); services.Decorate<IService, ServiceDecorator>();
-
var services = new ServiceCollection(); services.ScanAssemblyOf<IService>( // Set a filter for which types should be excluded/included in the registration process. // In this case, only the publicly available types are selected from the assembly. type => type.IsPublic, // The service type selector. Used to filter which interface or base types the implementation should be mapped to. // In this case, we are registering only by interfaces. (implementationType, serviceType) => serviceType.IsInterface, false, // Do not map services to themselves. E.g: Service -> Service. config => { // Register IService instances as scoped. if (config.ServiceType == typeof(IService)) config.WithScopedLifetime(); } );
-
class CompositionRoot : ICompositionRoot { public void Compose(IStashboxContainer container) { container.Register<IService, Service>(); } } var services = new ServiceCollection(); services.ComposeBy<CompositionRoot>(); // Or let Stashbox find all composition roots in an assembly. services.ComposeAssembly(typeof(CompositionRoot).Assembly);
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 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net461 is compatible. 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 | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.1
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.2.0)
- Stashbox.Extensions.Dependencyinjection (>= 4.2.0)
-
.NETStandard 2.1
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.2.0)
- Stashbox.Extensions.Dependencyinjection (>= 4.2.0)
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 |
---|---|---|
5.7.0 | 76 | 12/20/2024 |
5.6.0 | 142 | 8/21/2024 |
5.5.4 | 152 | 7/26/2024 |
5.5.3 | 194 | 4/10/2024 |
5.5.2 | 127 | 4/8/2024 |
5.5.1 | 129 | 4/2/2024 |
5.5.0 | 297 | 12/15/2023 |
5.4.0 | 309 | 11/19/2023 |
5.3.0 | 481 | 6/21/2023 |
5.2.2 | 488 | 6/13/2023 |
5.2.1 | 481 | 6/9/2023 |
5.2.0 | 477 | 6/5/2023 |
5.1.2 | 461 | 6/2/2023 |
5.1.1 | 490 | 6/1/2023 |
5.1.0 | 449 | 5/31/2023 |
5.0.0 | 513 | 5/28/2023 |
4.6.2 | 637 | 3/29/2023 |
4.6.1 | 560 | 3/29/2023 |
4.6.0 | 543 | 2/28/2023 |
4.5.3 | 653 | 1/26/2023 |
4.5.2 | 633 | 1/26/2023 |
4.5.1 | 598 | 1/20/2023 |
4.5.0 | 689 | 12/19/2022 |
4.4.0 | 647 | 12/6/2022 |
4.3.2 | 684 | 11/29/2022 |
4.3.1 | 873 | 10/14/2022 |
4.3.0 | 823 | 10/12/2022 |
4.2.3 | 762 | 9/9/2022 |
4.2.2 | 899 | 6/2/2022 |
4.2.1 | 783 | 5/16/2022 |
4.2.0 | 790 | 5/3/2022 |
4.1.2 | 821 | 4/10/2022 |
4.1.1 | 803 | 3/12/2022 |
4.1.0 | 785 | 3/7/2022 |
4.0.1 | 771 | 2/10/2022 |
4.0.0 | 759 | 2/9/2022 |
3.2.1 | 859 | 1/30/2022 |
3.2.0 | 1,081 | 12/5/2021 |
3.1.1 | 644 | 11/22/2021 |
3.1.0 | 647 | 11/22/2021 |
3.0.0 | 828 | 11/22/2021 |
2.11.4 | 828 | 5/26/2021 |
2.11.3 | 845 | 3/16/2021 |
2.11.2 | 1,365 | 1/31/2021 |
2.11.1 | 1,084 | 11/16/2020 |
2.11.0 | 902 | 11/15/2020 |
2.10.1 | 882 | 11/5/2020 |
2.10.0 | 904 | 11/2/2020 |
2.9.9 | 803 | 11/2/2020 |
2.9.8 | 952 | 10/19/2020 |
2.9.7 | 814 | 10/16/2020 |
2.9.6 | 848 | 10/16/2020 |
2.9.5 | 885 | 10/14/2020 |
2.9.4 | 1,157 | 7/21/2020 |
2.9.3 | 1,083 | 7/9/2020 |
2.9.2 | 963 | 6/29/2020 |
2.9.1 | 936 | 6/22/2020 |
2.9.0 | 839 | 6/8/2020 |
2.8.6 | 1,554 | 1/15/2020 |
2.8.5 | 1,147 | 11/11/2019 |
2.8.4 | 954 | 10/4/2019 |
2.8.3 | 1,116 | 9/12/2019 |
2.8.1 | 1,407 | 9/11/2019 |
2.7.1 | 1,171 | 6/10/2019 |
2.6.8 | 4,839 | 3/21/2019 |
2.6.7 | 1,282 | 1/13/2019 |
2.6.5 | 1,119 | 12/27/2018 |
2.6.4 | 1,029 | 12/26/2018 |
2.6.3 | 1,739 | 10/24/2018 |
2.6.2 | 1,492 | 7/3/2018 |
2.6.1 | 1,375 | 6/15/2018 |
2.6.0 | 1,502 | 3/20/2018 |
2.5.9 | 1,326 | 2/8/2018 |
2.5.8 | 1,388 | 2/5/2018 |
2.5.7 | 1,290 | 11/24/2017 |
2.5.6 | 1,299 | 11/24/2017 |
2.5.5 | 1,335 | 8/27/2017 |
2.5.4 | 1,279 | 8/7/2017 |
2.5.3 | 1,342 | 6/28/2017 |
2.5.2 | 1,347 | 6/9/2017 |
2.5.1 | 1,320 | 5/17/2017 |
2.4.8 | 1,311 | 5/16/2017 |
2.4.7 | 1,337 | 5/10/2017 |
2.4.6 | 1,365 | 5/10/2017 |
2.4.5 | 1,426 | 5/9/2017 |
2.4.3 | 1,263 | 5/3/2017 |
2.4.1 | 1,324 | 5/3/2017 |
2.3.1 | 1,280 | 3/24/2017 |
2.2.4 | 1,312 | 3/18/2017 |
2.2.3 | 1,300 | 3/17/2017 |
2.2.2 | 1,326 | 3/14/2017 |
2.2.1 | 1,380 | 3/11/2017 |
2.2.0 | 1,371 | 3/2/2017 |
2.1.2 | 1,396 | 2/21/2017 |
2.1.1 | 1,352 | 2/18/2017 |
2.1.0 | 1,322 | 2/18/2017 |
1.0.15 | 1,350 | 2/9/2017 |
1.0.14 | 1,384 | 2/9/2017 |
1.0.13 | 1,357 | 1/27/2017 |
1.0.12 | 1,356 | 1/26/2017 |
1.0.11 | 1,316 | 1/26/2017 |