-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for scoped service injection
- Loading branch information
Showing
27 changed files
with
465 additions
and
185 deletions.
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
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
38 changes: 38 additions & 0 deletions
38
src/Microsoft.Azure.WebJobs.Host/Executors/FunctionInstanceExtensions.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,38 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Executors | ||
{ | ||
public static class FunctionInstanceExtensions | ||
{ | ||
public static IServiceProvider GetInstanceServices(this IFunctionInstance instance) | ||
{ | ||
if (instance is IFunctionInstanceEx functionInstance) | ||
{ | ||
return functionInstance.InstanceServices; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
internal static IFunctionInvokerEx GetFunctionInvoker(this IFunctionInstance instance) | ||
{ | ||
if (instance.Invoker == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (instance.Invoker is IFunctionInvokerEx invoker) | ||
{ | ||
return invoker; | ||
} | ||
|
||
|
||
return new FunctionInvokerWrapper(instance.Invoker); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Microsoft.Azure.WebJobs.Host/Executors/FunctionInstanceWrapper.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,63 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Azure.WebJobs.Host.Protocols; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Executors | ||
{ | ||
internal class FunctionInstanceWrapper : IFunctionInstanceEx, IDisposable | ||
{ | ||
private readonly IFunctionInstance _instance; | ||
private readonly IServiceScopeFactory _serviceScopeFactory; | ||
private IServiceScope _instanceServicesScope; | ||
private IServiceProvider _instanceServices; | ||
|
||
public FunctionInstanceWrapper(IFunctionInstance instance, IServiceScopeFactory serviceScopeFactory) | ||
{ | ||
_instance = instance; | ||
_serviceScopeFactory = serviceScopeFactory; | ||
} | ||
|
||
public Guid Id => _instance.Id; | ||
|
||
public IDictionary<string, string> TriggerDetails => _instance.TriggerDetails; | ||
|
||
public Guid? ParentId => _instance.ParentId; | ||
|
||
public ExecutionReason Reason => _instance.Reason; | ||
|
||
public IBindingSource BindingSource => _instance.BindingSource; | ||
|
||
public IFunctionInvoker Invoker => _instance.Invoker; | ||
|
||
public FunctionDescriptor FunctionDescriptor => _instance.FunctionDescriptor; | ||
|
||
public IServiceProvider InstanceServices | ||
{ | ||
get | ||
{ | ||
if (_instanceServicesScope == null && _serviceScopeFactory != null) | ||
{ | ||
_instanceServicesScope = _serviceScopeFactory.CreateScope(); | ||
_instanceServices = _instanceServicesScope.ServiceProvider; | ||
} | ||
|
||
return _instanceServices; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_instanceServicesScope != null) | ||
{ | ||
_instanceServicesScope.Dispose(); | ||
} | ||
|
||
_instanceServicesScope = null; | ||
_instanceServices = null; | ||
} | ||
} | ||
} |
Oops, something went wrong.