0

The application uses ASP.NET Core 3. At the first call, a project class service is created.

Startup.cs

public void ConfigureServices(IServiceCollection services) { string connection = Configuration.GetConnectionString("ConnectionDB"); services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection), ServiceLifetime.Transient, ServiceLifetime.Singleton); services.AddSingleton<Project>(); } 

Project.cs

public class Project { private readonly DataBaseContext _dbContext; public Project(DataBaseContext dbContext) { _dbContext = dbContext; Init(); } public async void Init() { await SomeMethod('text'); } public async Task SomeMethod(string message) { _dbContext.Items.Add(message); await _dbContext.SaveChangesAsync(); } } 

This is not entirely correct and I want to create a service when the application starts.

public void ConfigureServices(IServiceCollection services) { // AddDbContext Project project = new Project(dbContext); // How to get dbcontext? services.AddSingleton(typeof(Project), project); } 

How to pass dbcontext in this case?

UPDATE Now in the Stratup class, I call the init () method of the project service. Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) { Project project = serviceProvider.GetService<Project>(); project.Init(); // some code } 
2
  • Your first code will work fine so why bother with instantiating the Project class? DbContext wil be automatically injected via Dependency injection Commented Oct 30, 2019 at 4:42
  • @keysl I need an instance of the Project class to be created when the application starts. Now it is created when the first client makes a request to the server. Commented Oct 30, 2019 at 4:44

1 Answer 1

3

Dunno why would you not use the automatic Dependecy Injection at your first code

Singleton are created upon app start. And as long as the init method is called inside the constructor it will run. So this code will work on your case already

public void ConfigureServices(IServiceCollection services) { string connection = Configuration.GetConnectionString("ConnectionDB"); services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection), ServiceLifetime.Transient, ServiceLifetime.Singleton); services.AddSingleton<Project>(); } 

But anyway if you insist on instantiating the Project class then you can use this. Get the DBContext using ServiceProvider.

public void ConfigureServices(IServiceCollection services) { // AddDbContext var sp = services.BuildServiceProvider(); var dbContext = sp.GetRequiredService<DbContext>(); Project project = new Project(dbContext); services.AddSingleton(typeof(Project), project); } 
Sign up to request clarification or add additional context in comments.

6 Comments

If I put a breakpoint in the constructor of the Project class, it is called only after the first client requests the server. "BuildServiceProvider" is not the best way out.
hmm I think DbContext is some sort of a service, so without building it you would not be able to get the DbContext. Why do you it's not best way out?
VS writes: Calling 'BuildServiceProvider' from application code results in an additional copy of singleton services being created.
You're right. DataBaseContext is created as a service.
@al.koval Clever! :D
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.