This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
Create a scoped service provider for the call to Configure #1106
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Builder; | ||
using static Microsoft.AspNetCore.Hosting.Tests.StartupManagerTests; | ||
|
||
namespace Microsoft.AspNetCore.Hosting.Fakes | ||
{ | ||
public class StartupWithScopedServices | ||
{ | ||
public DisposableService DisposableService { get; set; } | ||
|
||
public void Configure(IApplicationBuilder builder, DisposableService disposable) | ||
{ | ||
DisposableService = disposable; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,31 @@ public void StartupClassMayHaveHostingServicesInjected() | |
Assert.Equal(2, callbackStartup.MethodsCalled); | ||
} | ||
|
||
[Fact] | ||
public void StartupClassMayHaveScopedServicesInjected() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddSingleton<IServiceProviderFactory<IServiceCollection>>(new DefaultServiceProviderFactory(new ServiceProviderOptions | ||
{ | ||
ValidateScopes = true | ||
})); | ||
|
||
serviceCollection.AddScoped<DisposableService>(); | ||
var services = serviceCollection.BuildServiceProvider(); | ||
|
||
var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", "WithScopedServices"); | ||
var startup = StartupLoader.LoadMethods(services, type, "WithScopedServices"); | ||
Assert.NotNull(startup.StartupInstance); | ||
|
||
var app = new ApplicationBuilder(services); | ||
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); | ||
startup.ConfigureDelegate(app); | ||
|
||
var instance = (StartupWithScopedServices)startup.StartupInstance; | ||
Assert.NotNull(instance.DisposableService); | ||
Assert.True(instance.DisposableService.Disposed); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("Dev")] | ||
|
@@ -405,5 +430,15 @@ public void ConfigurationMethodCalled(object instance) | |
_configurationMethodCalledList.Add(instance); | ||
} | ||
} | ||
|
||
public class DisposableService : IDisposable | ||
{ | ||
public bool Disposed { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Were you going to use this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, but it's impossible to get to at the instance. I can remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixing it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🆙 📅 |
||
|
||
public void Dispose() | ||
{ | ||
Disposed = true; | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You could've just registered factory and stored last created object.