EF Core how to get a DBContext in a class instance? - asp.net-core

The ASP NET Core project uses the Entity Framework Core.
From the method of the Home controller I call Task.Run for the method of the instance of the class MyClass, where I need to access the data from the database.
I tried to pass _dbContext to the constructor of the MyClass class, but I get the exception
Cannot access a disposed object.
HomeController
[Route("api/[controller]")]
[EnableCors("AllowAllOrigin")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly DataBaseContext _dbContext;
public HomeController (DataBaseContext dbContext)
{
_dbContext = dbContext;
}
[EnableCors("AllowAllOrigin")]
[HttpPost("[action]")]
public string UpdateBotSettings()
{
MyClass myClass = new MyClass(_dbContext);
Task task = Task.Run(() => myClass.AnyMethod());
}
}
MyClass
public class MyClass
{
private readonly DataBaseContext _dbContext;
public MyClass(DataBaseContext dbContext)
{
_dbContext = dbContext;
}
public async void AnyMethodAsync()
{
MyData myData = _dbContext.AnyData.Where(d => d.id == 1).FirstOrDefault(); // <- exception
}
}
Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
string connection = Configuration.GetConnectionString("ConnectionDB");
services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection));
// another code
}
Can you please tell me how to access dbContext inside an instance of MyClass?

Most important mistake in your code is that you are using async void in your AnyMethodAsync() method of the Myclass. Don't use async void in your code, instead use async Task. Here is the details: C# – beware of async void in your code
So write your AnyMethodAsync() as follows:
public async Task AnyMethodAsync()
{
MyData myData = await _dbContext.AnyData.Where(d => d.id == 1).FirstOrDefault();
}
Now you can register your MyClass to the ASP.NET Core DI container as follows:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<MyClass>(); // <-- here it is
string connection = Configuration.GetConnectionString("ConnectionDB");
services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection));
// another code
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
Now use it as follows:
[Route("api/[controller]")]
[EnableCors("AllowAllOrigin")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly DataBaseContext _dbContext;
private readonly MyClass _myClass
public HomeController (DataBaseContext dbContext, MyClass myClass)
{
_dbContext = dbContext;
_myClass = myClass;
}
[EnableCors("AllowAllOrigin")]
[HttpPost("[action]")]
public async Task<string> UpdateBotSettings()
{
await _myClass.AnyMethod();
// rest of codes
}
}

Related

Asp.net Core Quartz job calling controller method, but context

I have an aspnetcore server (serving a Blazor wasm app), which has a Quartz scheduled job running. When the job triggers, it is calling a method on one of my server's controllers.
If I call this method on my controller normally (e.g. via a web API call), it works fine.
When I call the method from the Quartz IJob, the DbContext used in the controller seems to be disposed.
I've tried injecting the controller into the job in the normal way, and also via IServiceProvider, and both have the same result.
Controller:
public class NotificationController : ControllerBase
{
private readonly ApplicationDbContext context;
public NotificationService(ApplicationDbContext context)
{
this.context = context;
}
public async Task MyMethod()
{
await context.SaveChangesAsync(); //This is where it fails when Quartz calls it, seems context is not populated
}
}
My job (IServiceProvider attempt):
public class ReminderJob : IJob
{
private readonly IServiceProvider serviceProvider;
public ReminderJob(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public async Task Execute(IJobExecutionContext jobcontext)
{
using (var scope = serviceProvider.CreateScope())
{
await scope.ServiceProvider.GetRequiredService<NotificationController>().MyMethod();
}
}
}
My job (DI attempt):
public class ReminderJob : IJob
{
private readonly NotificationController notificationController;
public ReminderJob(NotificationController notificationController)
{
this.notificationController = notificationController;
}
public async Task Execute(IJobExecutionContext jobcontext)
{
await notificationController.MyMethod();
}
}
My Startup.cs (relevant lines in ConfigureServices):
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
services.AddControllersWithViews();
services.AddMvcCore().AddControllersAsServices();
services.AddRazorPages();
services.AddQuartz(q =>
{
q.UseMicrosoftDependencyInjectionScopedJobFactory(); //I also tried passing options.CreateScope to this method, but no difference
q.AddJobAndTrigger<ReminderJob>(configuration);
});
services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
}
No exception is thrown in VS when it attempts to do context.SaveChangesAsync(), however, a breakpoint directly after it is not hit, however when I check the details of context while debugging, it doesn't seem to be populated correctly.
How do I use the Controller from within the IJob, and ensure the Controller's dependencies are not disposed of?
How do I use the Controller from within the IJob
Do not use the controller in the Job.
Move/Extract/Refactor the desired functionality into a service abstraction
//Service abstraction
public interface INotificationService {
Task MyMethod();
}
public class NotificationService : INotificationService {
private readonly ApplicationDbContext context;
public NotificationService(ApplicationDbContext context) {
this.context = context;
}
public async Task MyMethod() {
await context.SaveChangesAsync();
}
}
and have the job and controller depend on the service to invoke the desired functionality.
public class NotificationController : ControllerBase {
private readonly INotificationService service;
public NotificationController (INotificationService service ) {
this.service = service ;
}
public async Task<IActionResult> MyMethod() {
await service.MyMethod();
return Ok();
}
}
public class ReminderJob : IJob {
private readonly INotificationService service;
public ReminderJob(INotificationService service) {
this.service = service;
}
public Task Execute(IJobExecutionContext jobcontext) {
return service.MyMethod();
}
}
And of course register all the necessary services with the DI container.
//...
services.AddScoped<INotificationService, NotificationService>();
//...

ASP.NET Core Dependency Injection: NullReferenceException trying to access concrete class member defined in its interface

this is getting weird as I've done it several times with no issues.
I'm using ASP.NET Core 3.1 for the record.
The problem is that I register a concrete type and its relative interface in the ConfigureServices Startup method:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("MyDatabase")
));
ProfileManager.RegisterMappingService(services);
services.AddScoped<IUserWorkerService, UserWorkerService>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddControllers();
I inject that dependency in the controller:
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly IUserWorkerService userWorkerService;
public UsersController(IUserWorkerService userWorkerService)
{
this.userWorkerService = userWorkerService ?? throw new ArgumentNullException(nameof(userWorkerService));
}
[AllowAnonymous]
[HttpPost("authenticate")]
public IActionResult Authenticate(string userName, string password)
{
var user = this.userWorkerService.Authenticate(userName.Trim(), password.Trim());
if (user == null)
return Unauthorized();
return Ok(System.Text.Json.JsonSerializer.Serialize(user));
}
}
This is the interface:
public interface IUserWorkerService
{
public UserDto Authenticate(string userName, string password);
}
And this is the concrete class:
public class UserWorkerService : IUserWorkerService
{
private readonly MyDbContext dbContext;
private readonly IMapper mapper;
public UserWorkerService(MyDbContext dbContext, IMapper mapper)
{
this.dbContext = dbContext;
this.mapper = mapper;
}
public UserDto Authenticate(string userName, string password)
{
*blah blah*
}
}
And when I make the POST request, I land correctly on the ActionResult of the controller but the UserWorkerService instance doesn't contain the member defined in its interface, just the injected members IMapper and MyDbContext.
Therefore, when code reaches Authenticate method invoke in the UsersController, debugger throws a NullReferenceException.
What am I missing here?

My ApplicationDbContext is null for some reason

I get the error System.NullReferenceException. Object reference not set to an instance of an object. At Club.BLL.MaintenanceBLL.Maintenance.DoMaintenance()
But the context code is the same as on scaffolded pages, so not sure what I need to do. Any help is appreciated.
The code is:
namespace Club.BLL.MaintenanceBLL
{
public class Maintenance
{
private readonly Club.Data.ApplicationDbContext Context;
public Maintenance(ApplicationDbContext context)
IN DEBUG MODE context IS INDICATED
AS OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT.
HOW DO I FIX THIS.
{
Context = context;
}
public Maintenance()
{
}
public void DoMaintenance()
{
//Parse Maintenance table and action those items
//where Active=True and ActionDate has passed
//==================================
//Retrieve list of rows in Maintenance table
var maintenances = Context.Maintenance; PROGRAM FAILS ON THIS LINE.
I imagine I am missing something fundamental as I am 'learning by doing'. The lines that call up the DoMaintenance routine are located in root/Pages/Index, which is a scaffolded page. The DoMaintenance routine is called from the following lines in the root/Pages/index.cshtml page:
public void OnGet()
{
Maintenance maintenance = new Maintenance();//Create new instance
maintenance.DoMaintenance();
}
AND startup.cs includes the lines
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddRoles()
.AddEntityFrameworkStores();
ANY HELP WOULD BE APPRECIATED.
Thanks and cheers....Alan
According to your description and codes, I found you have register the dbcontext as a service in your application that means if you want to use it, you should inject the dbcontext into your Maintenance class.
Normally, we will create a service as BLL service in asp.net core.
Then we could register the service in the startup.cs and inject it in the razor page.
I suggest you could try to modify the Maintenance class as below codes shows:
You could create a interface called IMaintenance:
public interface IMaintenance
{
public void DoMaintenance();
}
Then you could let Maintenance inherit IMaintenance as below:
public class Maintenance : IMaintenance
{
private readonly TestDbcontext Context;
public Maintenance(TestDbcontext testDbcontext ) {
Context = testDbcontext;
}
public void DoMaintenance() {
var maintenances = Context.Employees.ToList() ;
}
}
At last, you could register the Maintenance as service in startup.cs ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<TestDbcontext>(ServiceLifetime.Transient);
services.AddTransient<IMaintenance, Maintenance>();
}
You could directly use the Maintenance in razor pages like below, the asp.net core dependency injection will inject the dbcontext automatically into Maintenance.
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IStringLocalizer<IndexModel> _localizer;
private readonly IMaintenance _maintance;
public CustomerModel Customer { get; set; }
public IndexModel(ILogger<IndexModel> logger, IStringLocalizer<IndexModel> localizer, IMaintenance maintance)
{
_logger = logger;
_localizer = localizer;
_maintance = maintance;
}
public void OnGet()
{
_maintance.DoMaintenance()
}
}
namespace Club.Pages
{
[AllowAnonymous]
public class IndexModel : PageModel
{
private readonly IMaintenance _maintenance;
public IndexModel (IMaintenance<IndexModel> maintenance)
{
_maintenance = maintenance;
}
public void OnGet()
{
_maintenance.DoMaintenance();
}
}
}
You might have missed to inject your context and since there's default ctor, everything works fine without setting Context property.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<Context>(() => ...));
}

how to unit test controller that in turn calls repository with two parameters

At present, I am writing unit tests for my controller. Below is the structure of my code in the project.
MyController Class
public class MyController : Controller
{
private readonly MyRepository _myRepository;
public MyController()
: this(new MyRepository())
{
}
[HttpGet]
public ActionResult Index()
{
var items = _myRepository.GetAllItems();
if (items.Count() == 0)
return View("EmptyItems");
else
{
return View("List", items);
}
}
}
MyRepository Class
public class MyRepository : IDisposable, IMyRepository
{
private readonly MyDbContext _dbcontext;
private readonly ISecurityService _securityService;
public TodoListItemsRepository() : this(new MyDbContext(), new SecurityService())
{
}
public TodoListItemsRepository(MyDbContext context, ISecurityService securityService)
{
_dbcontext = context;
_securityService = securityService;
}
public IEnumerable<MyModel> GetAllItems()
{
var userid = _securityService.GetUser();
var todoList = _dbcontext.MyList.Where(e => e.UserId == userid);
return todoList;
}
//Other Methods etc...
......
}
SecurityService class
public class SecurityService : ISecurityService
{
public int GetUser()
{
return (int)Membership.GetUser().ProviderUserKey;
}
}
Here all methods inside my repository depends on GetUser method. Hence, I have initialized it inside the constructor. The repository class is initialized from the controller constructor.
My issue is - I couldn't unit the Index action unless I need to initialize dbcontext and the securityservice. Could someone please advise me if I am doing the right thing or any changes required in the structure of my code so that I can unit test my application ? I am new to MVC. So, any suggestions would be much appreciated.

What is the best way to wrap dbContext for DI?

I am thinking something like the following may work ok for injecting dbcontext via constructor to my service layer.... Does anyone out there have a better way?
It seems to work however _context.EntityName etc don't show up in intellisense unless I cast the object to the actual class that inherits from dbcontext.
public interface IContextFactory:IDisposable
{
DbContext Create();
}
public class ContextFactory<TContext> : IContextFactory where TContext : DbContext, new()
{
private DbContext _context;
public DbContext Create()
{
_context = new TContext();
_context.Configuration.LazyLoadingEnabled = true;
return _context;
}
public void Dispose()
{
_context.Dispose();
}
}
As Steven mentioned in his comment, you can just inject the DbContext directly from your Composition Root. Here is an example of how this could work with SimpleInjector.
container.Register<MyDbContext>(
() => new MyDbContext("name=MyDbContext"),
new WebRequestLifestyle(true));
Where MyDbContext is a subclass of DbContext:
public class MyDbContext: DbContext
{
public MyDbContext(string connectionString)
: base(connectionString)
{
this.Configuration.LazyLoadingEnabled = true;
}
/* DbSets<SomeEntity> etc */
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//etc
}
}