Hi i’m implementing a middleware in project azure function (Isolated), i need get the value from request body and show it in console (from middleware class), but i ever get empty value “”, and another problem its the functionality in the next(context) method apparently its not called and never ends.
The middleware:
public class HistoryLogMiddleware : IFunctionsWorkerMiddleware
{
public HistoryLogMiddleware() { }
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
var requestData = await context.GetHttpRequestDataAsync();
var body = await new StreamReader(requestData.Body).ReadToEndAsync();
Console.WriteLine($"Request Body: {body}");
requestData.Body.Position = 0;
await next(context); //when arrivals here does nothing...
}
}
The function:
public class RegisterClientFunction
{
private readonly IClientsDomain _clientsDomain;
public RegisterClientFunction(IClientsDomain clientsDomain)
{
_clientsDomain = clientsDomain;
}
[Function(nameof(RegisterClientFunction))]
public async Task<HttpResponseData> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
{
//log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var data = JsonConvert.DeserializeObject<RegisterClientRequest>(requestBody);
var respDomain = await ExecuterDomain.ExecuteAsync(() => _clientsDomain.RegisterClient(data));
var response = req.CreateResponse((HttpStatusCode)respDomain.Status);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
await response.WriteStringAsync(JsonConvert.SerializeObject(respDomain).ToString(), Encoding.UTF8);
return response;
}
}
The Program.cs :
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureFunctionsWorkerDefaults(worker => worker.UseMiddleware<HistoryLogMiddleware>())
.ConfigureServices(services => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddDatabases();
services.AddExternals();
services.AddUseCases();
})
.Build();
await host.RunAsync();
Result with middleware:
Result without miiddleware (i comment .ConfigureFunctionsWorkerDefaults(worker => worker.UseMiddleware())):
ignore the result 500, its normal in my example.
Does anybody know why it occurs when i use the middleware?