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

improving DI scope behavior #6721

Merged
merged 1 commit into from
Oct 1, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace DryIoc
using MemberAssignmentExpr = System.Linq.Expressions.MemberAssignment;
using FactoryDelegateExpr = System.Linq.Expressions.Expression<FactoryDelegate>;
using global::Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.DryIoc;
using global::Microsoft.Azure.WebJobs.Script;
using global::Microsoft.Azure.WebJobs.Script.Config;
#endif

/// <summary>IoC Container. Documentation is available at https://bitbucket.org/dadhi/dryioc. </summary>
Expand Down Expand Up @@ -1941,7 +1943,15 @@ private Container(Rules rules, Ref<Registry> registry, IScope singletonScope,
_registry = registry;

_singletonScope = singletonScope;
_scopeContext = scopeContext ?? new AsyncScopeContext();

_scopeContext = scopeContext;

if (_scopeContext == null && !FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableEnhancedScopes))
{
// Enhanced scopes do not need this context.
_scopeContext = new AsyncScopeContext();
}

_ownCurrentScope = ownCurrentScope;

_disposed = disposed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Threading.Tasks;
using DryIoc;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
Expand Down Expand Up @@ -55,6 +56,12 @@ internal ServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
}));

var scope = new ServiceScope(resolver, scopedRoot);

if (FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableEnhancedScopes))
{
scopedContext.UseInstance<IServiceProvider>(scope.ServiceProvider);
}

ChildScopes.TryAdd(scope, null);

scope.DisposalTask.ContinueWith(t => ChildScopes.TryRemove(scope, out object _));
Expand Down
1 change: 1 addition & 0 deletions src/WebJobs.Script/ScriptConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static class ScriptConstants
public const string FeatureFlagEnableActionResultHandling = "EnableActionResultHandling";
public const string FeatureFlagAllowSynchronousIO = "AllowSynchronousIO";
public const string FeatureFlagRelaxedAssemblyUnification = "RelaxedAssemblyUnification";
public const string FeatureFlagEnableEnhancedScopes = "EnableEnhancedScopes";

public const string AdminJwtValidAudienceFormat = "https://{0}.azurewebsites.net/azurefunctions";
public const string AdminJwtValidIssuerFormat = "https://{0}.scm.azurewebsites.net";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
Expand All @@ -12,6 +11,8 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.DependencyInjection
{
public class JobHostServiceProviderTests
{
private delegate object ServiceFactory(Type type);

[Fact]
public void Dispose_OnJobHostScope_DoesNotDisposeRootSingletonService()
{
Expand Down Expand Up @@ -88,6 +89,90 @@ public void Dispose_OnJobHost_DoesNotDisposRootScopedService()
Assert.False(rootService.Disposed);
}

[Fact]
public void Scopes_ChildScopeIsIsolated()
{
using (new TestScopedEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, ScriptConstants.FeatureFlagEnableEnhancedScopes))
{
var services = new ServiceCollection();
services.AddScoped<A>();

var rootScopeFactory = new WebHostServiceProvider(new ServiceCollection());
var jobHostProvider = new JobHostServiceProvider(services, rootScopeFactory, rootScopeFactory);

var a1 = jobHostProvider.GetService<A>();
jobHostProvider.CreateScope();
var a2 = jobHostProvider.GetService<A>();
Assert.NotNull(a1);
Assert.NotNull(a2);
Assert.Same(a1, a2);
}
}

[Fact]
public void Scopes_Factories()
{
using (new TestScopedEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, ScriptConstants.FeatureFlagEnableEnhancedScopes))
{
IList<IServiceProvider> serviceProviders = new List<IServiceProvider>();

var services = new ServiceCollection();
services.AddTransient<A>(p =>
{
serviceProviders.Add(p);
return new A();
});

var rootScopeFactory = new WebHostServiceProvider(new ServiceCollection());
var jobHostProvider = new JobHostServiceProvider(services, rootScopeFactory, rootScopeFactory);

// Get this service twice.
// The IServiceProvider passed to the factory should be different because they are separate scopes.
var scope1 = jobHostProvider.CreateScope();
scope1.ServiceProvider.GetService<A>();

var scope2 = jobHostProvider.CreateScope();
scope2.ServiceProvider.GetService<A>();

Assert.Equal(2, serviceProviders.Count);
Assert.NotSame(serviceProviders[0], serviceProviders[1]);
}
}

[Theory]
[InlineData("")]
[InlineData(ScriptConstants.FeatureFlagEnableEnhancedScopes)]
public void Scopes_DelegateFactory(string flag)
{
using (new TestScopedEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, flag))
{
var services = new ServiceCollection();

services.AddScoped<A>();
services.AddScoped<ServiceFactory>(provider => (type) => provider.GetRequiredService(type));

var rootScopeFactory = new WebHostServiceProvider(new ServiceCollection());
var jobHostProvider = new JobHostServiceProvider(services, rootScopeFactory, rootScopeFactory);

var scope1 = jobHostProvider.CreateScope();
var a1 = scope1.ServiceProvider.GetService<ServiceFactory>()(typeof(A));

var scope2 = jobHostProvider.CreateScope();
var a2 = scope2.ServiceProvider.GetService<ServiceFactory>()(typeof(A));

Assert.NotNull(a1);
Assert.NotNull(a2);
Assert.NotSame(a1, a2);
}
}

private class A
{
public A()
{
}
}

private class TestService : IService, IDisposable
{
public bool Disposed { get; private set; }
Expand Down