-
Notifications
You must be signed in to change notification settings - Fork 757
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
Conversation
Tests will fail in this PR until dotnet/runtime#89447 is fixed. |
...raries/Microsoft.Extensions.DependencyInjection.AutoActivation.Tests/AcceptanceTest.Keyed.cs
Show resolved
Hide resolved
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
We're blocked by dotnet/aspnetcore#49704. |
ea12738
to
7500a1f
Compare
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Microsoft.Extensions.DependencyInjection.AutoActivation.Tests are failing |
Tests failed because of the same reason: |
dotnet/aspnetcore#49704 was merged 14 hours ago, I will try to update the repo. |
It seems that the latest build failed: https://dev.azure.com/dnceng/internal/_build/results?buildId=2235253&view=logs&j=316d5c15-0c50-544e-8051-e6b14a1ab674&t=976ed21e-c736-59e1-f431-046bb8d7ebcd Thus, there's no artifacts to consume on our side yet. |
Hopefully #4240 brought the latest changes. |
@geeknoid can you please try to merge main into this branch and see whether it helps? |
7500a1f
to
8cadf6f
Compare
Yep, the fundamental issue is fixed. Now I just have to get my own shitty code working :-) |
...es/Microsoft.Extensions.DependencyInjection.AutoActivation/AutoActivationExtensions.Keyed.cs
Outdated
Show resolved
Hide resolved
...es/Microsoft.Extensions.DependencyInjection.AutoActivation/AutoActivationExtensions.Keyed.cs
Outdated
Show resolved
Hide resolved
ad309ad
to
2c787ce
Compare
@xakep139 This PR is finally ready to go... |
/// <param name="serviceKey">The <see cref="ServiceDescriptor.ServiceKey"/> of the service.</param> | ||
/// <returns>A reference to this instance after the operation has completed.</returns> | ||
[Experimental(diagnosticId: Experiments.AutoActivation, UrlFormat = Experiments.UrlFormat)] | ||
public static IServiceCollection ActivateKeyed<TService>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rename it to ActivateKeyedSingleton
? All DI-related extension method specify desired lifetime it their name (e.g. AddScoped()
, AddTransient()
, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, if we don't care about lifetime, we should remove singleton
from XML docs
} | ||
|
||
/// <summary> | ||
/// Adds an auto-activated singleton service of the type specified in TService with an implementation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Adds an auto-activated singleton service of the type specified in TService with an implementation | |
/// Adds an auto-activated singleton service of the type specified in <c>TService</c> with an implementation |
|
||
/// <summary> | ||
/// Adds an auto-activated singleton service of the type specified in TService with an implementation | ||
/// type specified in TImplementation using the factory specified in implementationFactory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// type specified in TImplementation using the factory specified in implementationFactory | |
/// type specified in <c>TImplementation</c> using the factory specified in <paramref name="implementationFactory"/> |
} | ||
|
||
/// <summary> | ||
/// Adds an auto-activated singleton service of the type specified in serviceType with a factory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no serviceType
in this overload
} | ||
|
||
/// <summary> | ||
/// Adds an auto-activated singleton service of the type specified in TService with an implementation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Adds an auto-activated singleton service of the type specified in TService with an implementation | |
/// Adds an auto-activated keyed singleton service of the type specified in TService with an implementation |
.AddActivatedKeyedSingleton<IFakeService, FakeService>(serviceKey)) | ||
.StartAsync(); | ||
|
||
var service = host.Services.GetKeyedService<IFakeService>(serviceKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we also check that instanceCount.Counter
equals to 1
before we get the service from the provider?
.AddActivatedKeyedSingleton(typeof(IFakeService), serviceKey, typeof(FakeService)) | ||
.AddActivatedKeyedSingleton<IFactoryService, FactoryService>(serviceKey, (sp, sk) => | ||
{ | ||
return new FactoryService(sp.GetKeyedService<IFakeService>(sk)!, sp.GetRequiredService<IFactoryServiceCounter>()!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return new FactoryService(sp.GetKeyedService<IFakeService>(sk)!, sp.GetRequiredService<IFactoryServiceCounter>()!); | |
return new FactoryService(sp.GetKeyedService<IFakeService>(sk)!, sp.GetRequiredService<IFactoryServiceCounter>()); |
} | ||
|
||
[Fact] | ||
public async Task ShouldAddAndActivateOnlyOnce_WhenHasChildAsync_Keyed() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public async Task ShouldAddAndActivateOnlyOnce_WhenHasChildAsync_Keyed() | |
public async Task ShouldAddAndActivateOnlyOnce_WhenHasChild_Keyed() |
} | ||
|
||
[Fact] | ||
public async Task ShouldResolveComponentsAutomaticallyAsync_Keyed() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public async Task ShouldResolveComponentsAutomaticallyAsync_Keyed() | |
public async Task ShouldResolveComponentsAutomatically_Keyed() |
} | ||
|
||
[Fact] | ||
public async Task CanActivateEnumerableAsync_Keyed() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public async Task CanActivateEnumerableAsync_Keyed() | |
public async Task CanActivateEnumerable_Keyed() |
Assert.Equal(0, anotherFakeServiceCount.Counter); | ||
} | ||
|
||
// ------------------------------------------------------------------------------ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove that line?
2c787ce
to
842146c
Compare
842146c
to
5da0e9d
Compare
null
Microsoft Reviewers: Open in CodeFlow