private static Func <DispatchMiddlewareContext, Func <Task>, Task> WrapMiddleware(Type middlewareType) { return((context, next) => { IServiceScope scope = OrchestrationScope.GetScope( context.GetProperty <OrchestrationInstance>().InstanceId); var middleware = (ITaskMiddleware)scope.ServiceProvider.GetServiceOrCreateInstance(middlewareType); return middleware.InvokeAsync(context, next); }); }
private static Func <DispatchMiddlewareContext, Func <Task>, Task> BeginMiddlewareScope(IServiceProvider serviceProvider) { return(async(context, next) => { IOrchestrationScope scope = OrchestrationScope.GetOrCreateScope( context.GetProperty <OrchestrationInstance>().InstanceId, serviceProvider); using (scope.Enter()) { await next().ConfigureAwait(false); } }); }
/// <summary> /// Creates a new <see cref="IOrchestrationScope"/> for the orchestration instance. /// </summary> /// <param name="orchestrationInstanceId">The orchestration instance id. Not null or empty.</param> /// <param name="serviceProvider">The service provider. Not null.</param> /// <returns>The newly created scope.</returns> public static IOrchestrationScope CreateScope( string orchestrationInstanceId, IServiceProvider serviceProvider) { Check.NotNullOrEmpty(orchestrationInstanceId, nameof(orchestrationInstanceId)); Check.NotNull(serviceProvider, nameof(serviceProvider)); lock (s_scopes) { if (s_scopes.ContainsKey(orchestrationInstanceId)) { throw new InvalidOperationException(Strings.ScopeAlreadyExists(orchestrationInstanceId)); } #pragma warning disable CA2000 // Dispose objects before losing scope IOrchestrationScope scope = new OrchestrationScope(serviceProvider.CreateScope(), orchestrationInstanceId); s_scopes[orchestrationInstanceId] = scope; return(scope); #pragma warning restore CA2000 // Dispose objects before losing scope } }
public ScopeRef(OrchestrationScope scope) { _scope = scope; scope.Increment(); }