Ejemplo n.º 1
0
    protected override async Task <ServiceResult <object> > ReadHttpContentAsyncCore(Type objectType, HttpContent content, CancellationToken cancellationToken)
    {
        try
        {
#if NET6_0_OR_GREATER
            var stream = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);

            await using var streamScope = stream.ConfigureAwait(false);
#elif NETSTANDARD2_1_OR_GREATER
            var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);

            await using var streamScope = stream.ConfigureAwait(false);
#else
            using var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
#endif
            var deserializedContent = await m_serializer.FromStreamAsync(stream, objectType, cancellationToken).ConfigureAwait(false);

            if (deserializedContent is null)
            {
                return(ServiceResult.Failure(HttpServiceErrors.CreateInvalidContent("Content must not be empty.")));
            }
            return(ServiceResult.Success(deserializedContent));
        }
        catch (ServiceSerializationException exception)
        {
            return(ServiceResult.Failure(HttpServiceErrors.CreateInvalidContent(exception.Message)));
        }
    }
Ejemplo n.º 2
0
 public async Task EmptyJson()
 {
     await GetApiInfoInvalidResponse(
         _ => new HttpResponseMessage(HttpStatusCode.OK)
     {
         Content = new StringContent("", Encoding.UTF8, "application/json")
     },
         ServiceErrors.InvalidResponse, HttpServiceErrors.CreateInvalidContent("").Message);
 }