I want to create Visitor page
private readonly DBDatacontext _db;
private RequestDelegate _requestDelegate;
public VisitorMiddleWare(RequestDelegate requestDelegate, DBDatacontext db)
{
_requestDelegate = requestDelegate;
_db = db;
}
public async Task InvokeAsync(HttpContext context)
{
string visitorId = context.Request.Cookies["VisitorId"];
string remoteIpAddress = context.Connection.RemoteIpAddress.ToString();
if (visitorId == null)
{
context.Response.Cookies.Append("VisitorId", Guid.NewGuid().ToString(), new
CookieOptions()
{
Path = "/",
HttpOnly = true,
Secure = false,
});
visitorId = context.Request.Cookies["VisitorId"];
var model = new Visitor()
{
Ip = remoteIpAddress
};
_db.AddAsync(model);
_db.SaveChangesAsync();
}
await _requestDelegate(context);
}
and in the startup
app.UseMiddleware(typeof(VisitorMidlleWare));
but I get this error:
Cannot resolve scoped service 'DBDatacontext' from root provider.