示例#1
0
        public async Task <CloudMessage> Create(HttpRequest httpRequest)
        {
            if (httpRequest is null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

            var cloudMessageContent = new HttpRequestCloudMessageContent
            {
                Method  = httpRequest.Method,
                Uri     = httpRequest.Path + httpRequest.QueryString,
                Content = await LoadHttpRequestContent(httpRequest).ConfigureAwait(false)
            };

            if (!string.IsNullOrEmpty(httpRequest.ContentType))
            {
                cloudMessageContent.Headers = new Dictionary <string, string>
                {
                    ["Content-Type"] = httpRequest.ContentType
                };
            }

            var requestMessage = new CloudMessage
            {
                Type    = CloudMessageType.HttpInvoke,
                Payload = _cloudMessageSerializer.Pack(cloudMessageContent)
            };

            return(requestMessage);
        }
        public static void SetContent <T>(this CloudMessage cloudMessage, T value)
        {
            if (cloudMessage == null)
            {
                throw new ArgumentNullException(nameof(cloudMessage));
            }

            if (value == null)
            {
                cloudMessage.Content = null;
            }

            if (value is JToken token)
            {
                cloudMessage.Content = new CloudMessageContent
                {
                    Data = MessagePackSerializer.Get <string>().PackSingleObject(token.ToString(Formatting.None))
                };
            }
            else
            {
                cloudMessage.Content = new CloudMessageContent
                {
                    Data = MessagePackSerializer.Get <T>().PackSingleObject(value)
                };
            }
        }
        public static T GetContent <T>(this CloudMessage cloudMessage)
        {
            if (cloudMessage == null)
            {
                throw new ArgumentNullException(nameof(cloudMessage));
            }

            if (cloudMessage.Content?.Data == null)
            {
                return(default);
        public async Task <CloudMessage> Create(HttpRequest httpRequest)
        {
            if (httpRequest is null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

            var contentLength        = (int)(httpRequest.ContentLength ?? 0L);
            var requestContentBuffer = ArrayPool <byte> .Shared.Rent(contentLength);

            try
            {
                var cloudMessageContent = new HttpRequestCloudMessageContent
                {
                    Method = httpRequest.Method,
                    Uri    = httpRequest.Path + httpRequest.QueryString
                };

                if (contentLength > 0)
                {
                    await httpRequest.Body.ReadAsync(requestContentBuffer, 0, contentLength).ConfigureAwait(false);

                    cloudMessageContent.Content = new ArraySegment <byte>(requestContentBuffer, 0, contentLength);
                }

                if (!string.IsNullOrEmpty(httpRequest.ContentType))
                {
                    cloudMessageContent.Headers = new Dictionary <string, string>
                    {
                        ["Content-Type"] = httpRequest.ContentType
                    };
                }

                var requestMessage = new CloudMessage
                {
                    Type    = CloudMessageType.HttpInvoke,
                    Payload = _cloudMessageSerializer.Pack(cloudMessageContent)
                };

                return(requestMessage);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(requestContentBuffer);
            }
        }