Skip to content

Commit 1239255

Browse files
committed
Added SimpleInjectorAddOptions.EnableHostedServiceResolution to disable hosted service resolution. Fixes #15
1 parent aa551b1 commit 1239255

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/SimpleInjector.Integration.ServiceCollection/SimpleInjectorAddOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace SimpleInjector.Integration.ServiceCollection
55
{
66
using System;
77
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
89
using SimpleInjector.Advanced;
910

1011
/// <summary>
@@ -79,6 +80,15 @@ public IServiceProviderAccessor ServiceProviderAccessor
7980
/// <value>A boolean value.</value>
8081
public bool DisposeContainerWithServiceProvider { get; set; } = true;
8182

83+
/// <summary>
84+
/// Gets or sets the value indicating whether the <see cref="Container"/> allows resolving
85+
/// <see cref="IHostedService"/> implementations and injecting them with cross-wired framework dependencies.
86+
/// The default is <c>true</c>. Set this value to <b>false</b> when running in an environment that does not
87+
/// support hosted services, e.g. Azure Functions.
88+
/// </summary>
89+
/// /// <value>A boolean value.</value>
90+
public bool EnableHostedServiceResolution { get; set; } = true;
91+
8292
/// <summary>
8393
/// Gets the <see cref="IServiceProvider"/> instance that will be used by Simple Injector to resolve
8494
/// cross-wired framework components. It's value will be set when

src/SimpleInjector.Integration.ServiceCollection/SimpleInjectorServiceCollectionExtensions.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,18 @@ public static IServiceCollection AddSimpleInjector(
7373
// Set lifestyle before calling setupAction. Code in the delegate might depend on that.
7474
TrySetDefaultScopedLifestyle(container);
7575

76-
HookAspNetCoreHostHostedServiceServiceProviderInitialization(options);
77-
7876
setupAction?.Invoke(options);
7977

8078
RegisterServiceScope(options);
8179

80+
// Unfortunately, the addition of the IHostedService breaks Azure Functions. Azure Functions do no support
81+
// IHostedService. See: https://stackoverflow.com/questions/59947132/. This is why we had to make this
82+
// conditional.
83+
if (options.EnableHostedServiceResolution)
84+
{
85+
HookAspNetCoreHostHostedServiceServiceProviderInitialization(options);
86+
}
87+
8288
if (options.AutoCrossWireFrameworkComponents)
8389
{
8490
AddAutoCrossWiring(options);
@@ -597,16 +603,18 @@ private static void TrySetDefaultScopedLifestyle(Container container)
597603
private static void HookAspNetCoreHostHostedServiceServiceProviderInitialization(
598604
SimpleInjectorAddOptions options)
599605
{
600-
// ASP.NET Core 3's new Host class resolves hosted services much earlier in the pipeline. This
601-
// registration ensures that the IServiceProvider is assigned before such resolve takes place,
602-
// to ensure that that hosted service can be injected with cross-wired dependencies.
603-
options.Services.AddSingleton<IHostedService>(provider =>
606+
// ASP.NET Core 3's new Host class resolves hosted services much earlier in the pipeline. This registration
607+
// ensures that the IServiceProvider is assigned before such resolve takes place, to ensure that that
608+
// hosted service can be injected with cross-wired dependencies.
609+
// We must ensure that this hosted service gets resolved by ASP.NET before any other hosted service is
610+
// resolve; that's why we do the Insert(0).
611+
options.Services.Insert(0, ServiceDescriptor.Singleton<IHostedService>(provider =>
604612
{
605613
options.SetServiceProviderIfNull(provider);
606614

607615
// We can't return null here, so we return an empty implementation.
608616
return new NullSimpleInjectorHostedService();
609-
});
617+
}));
610618
}
611619

612620
private static void EnsureMethodOnlyCalledOnce(

0 commit comments

Comments
 (0)