C
C#9mo ago
wwww

✅ DB context injection not working

hi, i have app like this -
DotNetEnv.Env.Load("../build/");
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
WebRootPath = "Static"
});

builder.Services.AddMvc();

builder.Services.AddDbContext<MainContext>();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.LoginPath = "/login");

var app = builder.Build();

app.UseDefaultFiles();
app.UseStaticFiles();

app.MapControllers();

app.Run();
DotNetEnv.Env.Load("../build/");
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
WebRootPath = "Static"
});

builder.Services.AddMvc();

builder.Services.AddDbContext<MainContext>();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.LoginPath = "/login");

var app = builder.Build();

app.UseDefaultFiles();
app.UseStaticFiles();

app.MapControllers();

app.Run();
and when i use MainContext in endpoint as argmuent it says - (screenshot). example of endpoint
[Route("api/{genre}/{id}")]
public IActionResult IndexGenre(MainContext db, string keyword, int id)
{
if (keyword == "default")
{
Console.WriteLine("1");
var result = db.Products.Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList();
return Json(result);
}
else if (keyword.Contains("#"))
{
string genre = keyword.Substring(0, keyword.Length - 1);
var result = db.Products.Where(p => p.Type.Contains(genre)).Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList(); ;
return Json(result);
}
else
{
var result = db.Products.Where(p => p.Title.Contains(keyword) || p.Description.Contains(keyword)).Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList(); ;
return Json(result);
}
}
[Route("api/{genre}/{id}")]
public IActionResult IndexGenre(MainContext db, string keyword, int id)
{
if (keyword == "default")
{
Console.WriteLine("1");
var result = db.Products.Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList();
return Json(result);
}
else if (keyword.Contains("#"))
{
string genre = keyword.Substring(0, keyword.Length - 1);
var result = db.Products.Where(p => p.Type.Contains(genre)).Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList(); ;
return Json(result);
}
else
{
var result = db.Products.Where(p => p.Title.Contains(keyword) || p.Description.Contains(keyword)).Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList(); ;
return Json(result);
}
}
No description
14 Replies
wwww
wwww9mo ago
also, it works if put it into Using construction
JakenVeina
JakenVeina9mo ago
where is it that you're abusing IoC lifetimes? cause the error tells you exactly what's happening you're trying to reuse an existing MainContext instance to perform multiple queries, in parallel DbContext instances do not support parallel use (I.E. multi-threading) each incoming HTTP request needs to use its own MainContext instance for performing queries ASP.NET Core sets all this up for you by creating processing all HTTP requests within their own service scope and by default, DbContext dependencies are registered as scoped it's also possible, I suppose, that you're performing multiple queries within the same request, but not awaiting one, so that the end up stepping on each other however, it doesn't look like you're using async/await
wwww
wwww9mo ago
i mean, anyway i can't get like
JakenVeina
JakenVeina9mo ago
wait...
wwww
wwww9mo ago
it works like this -
app.MapGet("api/{keyword}/{id}", (MainContext db, string keyword, int id) =>
{
if (keyword == "default")
{
Console.WriteLine("1");
var result = db.Products.Skip(id * 24).Take(24).ToList();
return Results.Json(result);
}
else if (keyword.Contains('#'))
{
string genre = keyword.Substring(0, keyword.Length - 1);
var result = db.Products.Where(p => p.Type.Contains(genre)).Skip(id * 24).Take(24).ToList(); ;
return Results.Json(result);
}
else
{
var result = db.Products.Where(p => p.Title.Contains(keyword) || p.Description.Contains(keyword)).Skip(id * 24).Take(24).ToList(); ;
return Results.Json(result);
}
});
app.MapGet("api/{keyword}/{id}", (MainContext db, string keyword, int id) =>
{
if (keyword == "default")
{
Console.WriteLine("1");
var result = db.Products.Skip(id * 24).Take(24).ToList();
return Results.Json(result);
}
else if (keyword.Contains('#'))
{
string genre = keyword.Substring(0, keyword.Length - 1);
var result = db.Products.Where(p => p.Type.Contains(genre)).Skip(id * 24).Take(24).ToList(); ;
return Results.Json(result);
}
else
{
var result = db.Products.Where(p => p.Title.Contains(keyword) || p.Description.Contains(keyword)).Skip(id * 24).Take(24).ToList(); ;
return Results.Json(result);
}
});
but don't works if literally same code is in controller
JakenVeina
JakenVeina9mo ago
why would you expect it to work the same
wwww
wwww9mo ago
¯\_(ツ)_/¯
JakenVeina
JakenVeina9mo ago
if you look at documentation for building controllers, you'll notice it doesn't specify that parameter-injection is supported not without an additional [FromServices] annotation on each parameter that you want to be populated from the IoC container
wwww
wwww9mo ago
yeah is see
wwww
wwww9mo ago
No description
wwww
wwww9mo ago
ef core was last topic im on 6 yet there is a bit different way
JakenVeina
JakenVeina9mo ago
ef core or not, the normal method of dependency injection for controllers is via the constructor if you want to use parameter injection, you have to use the annotation I mentioned
JakenVeina
JakenVeina9mo ago
Dependency injection into controllers in ASP.NET Core
Discover how ASP.NET Core MVC controllers request their dependencies explicitly via their constructors with dependency injection in ASP.NET Core.
wwww
wwww9mo ago
ill look at that later 🙂 thx for help,
MainContext db;

public HomeController(MainContext db)
{
this.db = db;
}
MainContext db;

public HomeController(MainContext db)
{
this.db = db;
}
works this way
Want results from more Discord servers?
Add your server
More Posts