Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for auto-activated keyed singletons #4222

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<NoWarn>$(NoWarn);AD0001</NoWarn>

<!-- Experimental warnings are for customers, not for this repo -->
<NoWarn>$(NoWarn);EXTEXP0001;EXTEXP0002;EXTEXP0003;EXTEXP0004;EXTEXP0005;EXTEXP0006;EXTEXP0007;EXTEXP0008;EXTEXP0009;EXTEXP0010;EXTEXP0011</NoWarn>
<NoWarn>$(NoWarn);EXTEXP0001;EXTEXP0002;EXTEXP0003;EXTEXP0004;EXTEXP0005;EXTEXP0006;EXTEXP0007;EXTEXP0008;EXTEXP0009;EXTEXP0010;EXTEXP0011;EXTEXP0012</NoWarn>

<!-- NU5104: A stable release of a package should not have a prerelease dependency -->
<NoWarn>$(NoWarn);NU5104</NoWarn>
Expand Down
3 changes: 2 additions & 1 deletion docs/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ if desired.
| `EXTEXP0008` | Resource monitoring experiments |
| `EXTEXP0009` | Hosting experiments |
| `EXTEXP0010` | Object pool experiments |

| `EXTEXP0011` | Document database experiments |
| `EXTEXP0012` | Auto-activation experiments |

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Shared.Diagnostics;
Expand All @@ -14,24 +12,27 @@ namespace Microsoft.Extensions.DependencyInjection;

internal sealed class AutoActivationHostedService : IHostedService
{
private readonly Type[] _autoActivators;
private readonly AutoActivatorOptions _options;
private readonly IServiceProvider _provider;

public AutoActivationHostedService(IServiceProvider provider, IOptions<AutoActivatorOptions> options)
{
_provider = provider;
var value = Throw.IfMemberNull(options, options.Value);

_autoActivators = value.AutoActivators.ToArray();
_options = Throw.IfMemberNull(options, options.Value);
}

public Task StartAsync(CancellationToken cancellationToken)
{
foreach (var singleton in _autoActivators)
foreach (var singleton in _options.AutoActivators)
{
_ = _provider.GetRequiredService(singleton);
}

foreach (var (serviceType, serviceKey) in _options.KeyedAutoActivators)
{
_ = _provider.GetRequiredKeyedService(serviceType, serviceKey);
}

return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ namespace Microsoft.Extensions.DependencyInjection;
internal sealed class AutoActivatorOptions
{
public HashSet<Type> AutoActivators { get; } = new();
public HashSet<(Type serviceType, object? serviceKey)> KeyedAutoActivators { get; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<Workstream>Fundamentals</Workstream>
</PropertyGroup>

<PropertyGroup>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
</PropertyGroup>

<PropertyGroup>
<Stage>normal</Stage>
<MinCodeCoverage>100</MinCodeCoverage>
Expand Down
3 changes: 2 additions & 1 deletion src/Shared/DiagnosticIds/Experiments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Shared.DiagnosticIds;
/// </summary>
/// <remarks>
/// When adding a new experiment, add a corresponding suppression to the root <c>Directory.Build.targets</c> file, and add a documentation entry to
/// <c>docs/Experiments.md</c>.
/// <c>docs/list-of-diagnostics.md</c>.
/// </remarks>
internal static class Experiments
{
Expand All @@ -29,4 +29,5 @@ internal static class Experiments
internal const string Hosting = "EXTEXP0009";
internal const string ObjectPool = "EXTEXP0010";
internal const string DocumentDb = "EXTEXP0011";
internal const string AutoActivation = "EXTEXP0012";
}
Loading