I am performing a really simple operation of Insert
using the Entity Framework in Azure Function but unable to understand why I am getting the error. Below is the code snippets of different files:
LoggingMiddleware.cs
public class LoggingMiddleware : ILoggingMiddleware
{
private readonly AppsContext _context;
public LoggingMiddleware(AppsContext context)
{
_context = context;
}
public async Task LogRequestAsync(HttpContext httpContext)
{
try
{
var requestHeaders = string.Join(Environment.NewLine, httpContext.Request.Headers.Select(header => $"{header.Key} = {header.Value}"));
using var reader = new StreamReader(httpContext.Request.Body);
string requestBody = await reader.ReadToEndAsync();
var httpRequestApiLog = new HttpRequestApiLog
{
Url = httpContext.Request.GetDisplayUrl(),
RequestMethod = httpContext.Request.Method,
RequestHeaders = requestHeaders,
RequestBody = requestBody
};
_context.HttpRequestApiLogs.Add(httpRequestApiLog);
await _context.SaveChangesAsync();
}
catch (Exception)
{
throw;
}
}
}
Azure Function:
private readonly ILoggingMiddleware _loggingMiddleware;
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
await _loggingMiddleware.LogRequestAsync(req.HttpContext);
// Other lines of code
}
Exception happens at line:
await _context.SaveChangesAsync();
And the below file is opened which I have no idea what it is doing and throwing the exception. Below snip is from file SNINativeMethodWrapper.cs
What should I do to fix this issue and keep myself rolling further? Stuck on this issue since 2 days and tried all possible answers on the internet.