public static Task <HttpResponseMessage> TriggerAsync(this IHttpUpstreamTrigger trigger, UpstreamContext context, HubConnectionContext connectionContext, InvokeUpstreamParameters parameters, Action <HttpRequestMessage> configureRequest = null, CancellationToken token = default)
        {
            var upstreamFeatures = connectionContext.Features.Get <IHttpUpstreamPropertiesFeature>();

            if (upstreamFeatures == null)
            {
                // defensive code, should not happen
                throw new InvalidOperationException("IHttpUpstreamPropertiesFeature expected");
            }

            return(trigger.TriggerAsync(context, upstreamFeatures, parameters, configureRequest, token));
        }
        public static async Task <HttpResponseMessage> GetResponseAsync(this IHttpUpstreamTrigger httpUpstream, UpstreamContext context, HubConnectionContext connectionContext, ServerlessProtocol.InvocationMessage message, InvokeUpstreamParameters parameters, MediaTypeHeaderValue mediaType = null, CancellationToken token = default)
        {
            var upstream = connectionContext.Features.Get <IHttpUpstreamPropertiesFeature>();

            if (upstream == null)
            {
                // defensive code, should not happen
                throw new InvalidOperationException("IHttpUpstreamPropertiesFeature expected");
            }

            if (message.Payload.IsSingleSegment)
            {
                return(await httpUpstream.TriggerAsync(
                           context,
                           upstream,
                           parameters,
                           request =>
                {
                    request.Content = new ReadOnlyMemoryContent(message.Payload.First);
                    request.Content.Headers.ContentType = mediaType;
                },
                           token));
            }

            using (var owner = ExactSizeMemoryPool.Shared.Rent((int)message.Payload.Length))
            {
                message.Payload.CopyTo(owner.Memory.Span);

                return(await httpUpstream.TriggerAsync(
                           context,
                           upstream,
                           parameters,
                           request =>
                {
                    request.Content = new ReadOnlyMemoryContent(owner.Memory);
                    request.Content.Headers.ContentType = mediaType;
                },
                           token));
            }
        }
Esempio n. 3
0
        public async Task AddClientConnectionAsync(HubConnectionContext connectionContext, CancellationToken token = default)
        {
            var parameters = new InvokeUpstreamParameters(_hubName, ServerlessProtocol.Constants.Categories.ConnectionCategory, ServerlessProtocol.Constants.Events.ConnectedEvent);

            if (!_httpUpstreamTrigger.TryGetMatchedUpstreamContext(parameters, out var context))
            {
                // Upstream for this event is not set, ignore the event
                return;
            }

            using (var response = await _httpUpstreamTrigger.TriggerAsync(
                       context,
                       connectionContext,
                       parameters,
                       request =>
            {
                request.Content = new ByteArrayContent(OpenConnectionPayload);
                request.Content.Headers.ContentType = JsonMediaType;
            },
                       token))
            {
                response.CheckResponse(parameters, _logger, IsExpectedResponse);
            }
        }