Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Accessing dbcontext in Constructor of Singleton Service

$
0
0

Scenario

In the server project of a Blazor web assembly application, I have a service that will utilize certain metadata from the database. This metadata will only change with changes to the schema - not during normal application execution.

My intent is to create a singleton service that can be called from a controller to access this information. I intend to capture this metadata once and then make it available through the service.

I believe the way to do this is to make the DbContext available within the constructor of the service, capture the information, and then destroy the DbContext as it will not be needed again within the service. Other methods will pass this metadata to the caller.

Question 1

Does this approach make sense? The basic logic will be:

public ServiceConstructor(){    // Get copy of the dbcontext    // Do some work using the dbcontext    // destroy the dbcontext.}

Question 2

How do I go about getting a copy of the DbContext from the scoped service and does it make sense to do this without passing it in to the constructor as a parameter?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>