/// <summary>
            /// OnEvent is called whenever the event stream yields a payload and triggers an
            /// execution of the subscription query.
            /// </summary>
            /// <param name="payload">
            /// The event stream payload.
            /// </param>
            /// <returns>
            /// Returns a query result which will be enqueued to the response stream.
            /// </returns>
            private async Task <IQueryResult> OnEvent(object payload)
            {
                using IDisposable es             = _diagnosticEvents.OnSubscriptionEvent(new(this, payload));
                using IServiceScope serviceScope = _requestContext.Services.CreateScope();

                OperationContext operationContext = _operationContextPool.Get();

                try
                {
                    var eventServices = serviceScope.ServiceProvider;
                    var dispatcher    = eventServices.GetRequiredService <IBatchDispatcher>();

                    // we store the event payload on the scoped context so that it is accessible
                    // in the resolvers.
                    ImmutableDictionary <string, object?> scopedContext =
                        ImmutableDictionary <string, object?> .Empty
                        .SetItem(WellKnownContextData.EventMessage, payload);

                    // next we resolve the subscription instance.
                    var rootValue = RootValueResolver.Resolve(
                        _requestContext,
                        eventServices,
                        _subscriptionType,
                        ref _cachedRootValue);

                    // last we initialize a standard operation context to execute
                    // the subscription query with the standard query executor.
                    operationContext.Initialize(
                        _requestContext,
                        eventServices,
                        dispatcher,
                        _requestContext.Operation !,
                        _queryPlan,
                        _requestContext.Variables !,
                        rootValue,
                        _resolveQueryRootValue);

                    operationContext.Result.SetContextData(
                        WellKnownContextData.EventMessage,
                        payload);

                    IQueryResult result = await _queryExecutor
                                          .ExecuteAsync(operationContext, scopedContext)
                                          .ConfigureAwait(false);

                    _diagnosticEvents.SubscriptionEventResult(new(this, payload), result);

                    return(result);
                }
                catch (Exception ex)
                {
                    _diagnosticEvents.SubscriptionEventError(new(this, payload), ex);
                    throw;
                }
                finally
                {
                    _operationContextPool.Return(operationContext);
                }
            }
예제 #2
0
            private async Task <IQueryResult> OnEvent(object payload)
            {
                using IServiceScope serviceScope = _requestContext.Services.CreateScope();

                IServiceProvider eventServices = serviceScope.ServiceProvider;
                IBatchDispatcher dispatcher    = eventServices.GetRequiredService <IBatchDispatcher>();

                OperationContext operationContext = _operationContextPool.Get();

                try
                {
                    ImmutableDictionary <string, object?> scopedContext =
                        ImmutableDictionary <string, object?> .Empty
                        .SetItem(WellKnownContextData.EventMessage, payload);

                    object?rootValue = RootValueResolver.Resolve(
                        _requestContext,
                        eventServices,
                        _subscriptionType,
                        ref _cachedRootValue);

                    operationContext.Initialize(
                        _requestContext,
                        eventServices,
                        dispatcher,
                        _requestContext.Operation !,
                        _requestContext.Variables !,
                        rootValue,
                        _resolveQueryRootValue);

                    return(await _queryExecutor
                           .ExecuteAsync(operationContext, scopedContext)
                           .ConfigureAwait(false));
                }
                finally
                {
                    _operationContextPool.Return(operationContext);
                }
            }