HttpRequest Body reading behaviour differs between .NET 8 in-process and dotnet-isolated

When trying to use the request body stream as content for another HttpRequest, the dotnet-isolated function app seems to have issues with certain backend servers which see the request body as empty. The exact same http call in a in-process .NET 8 function works. Materializing the request body and using StringContent for the HttpRequest Content property eliminates this issue.

in-process:

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
using System.Threading;

namespace FunctionApp2
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> GeneratePdfAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "documents/{tenant}/{engine}/{template}")] HttpRequest body,
        string engine,
        string tenant,
        string template,
        CancellationToken ct)
        {
            using var client = new HttpClient() { BaseAddress = new Uri("https://my-backend-func.azurewebsites.net/api/") };
            using var request = new HttpRequestMessage(HttpMethod.Post, new Uri($"pdf/{Uri.EscapeDataString(tenant)}/{Uri.EscapeDataString(template)}", UriKind.Relative));
            //request.Content = new StringContent(await (new StreamReader(pdfRequest.Payload)).ReadToEndAsync());

            request.Content = new StreamContent(body.Body);
            using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
            if (response.StatusCode == HttpStatusCode.OK)
                return new FileStreamResult(await response.Content.ReadAsStreamAsync(ct), "application/pdf");
            else
                return new BadRequestObjectResult(await response.Content.ReadAsStringAsync());
        }
    }
}

dotnet-isolated:

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Net.Http;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using System.IO;
using Microsoft.Azure.Functions.Worker.Http;

namespace FunctionApp1
{
    public class Function1
    {
        [Function("Function1")]
        public async Task<IActionResult> GeneratePdfAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "documents/{tenant}/{engine}/{template}")] HttpRequestData req,
        string engine,
        string tenant,
        string template,
        CancellationToken ct)
        {
            using var client = new HttpClient() { BaseAddress = new Uri("https://my-backend-func.azurewebsites.net/api/") };
            using var request = new HttpRequestMessage(HttpMethod.Post, new Uri($"pdf/{Uri.EscapeDataString(tenant)}/{Uri.EscapeDataString(template)}", UriKind.Relative));
            //request.Content = new StringContent(await (new StreamReader(pdfRequest.Payload)).ReadToEndAsync());
            request.Content = new StreamContent(req.Body);
            using var response = await client.SendAsync(request, ct);
            if (response.StatusCode == HttpStatusCode.OK)
                return new FileStreamResult(await response.Content.ReadAsStreamAsync(ct), "application/pdf");
            else
                return new BadRequestObjectResult(await response.Content.ReadAsStringAsync());
        }
    }
}

I expected the behaviour of this simple POC to match between the different host runtimes. I have not found anything online explaining this behaviour.

New contributor

Niklas Schilli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật