There are some common services that the ASP.NET framework has made easy-to-register by providing unique AddX methods. You might recognize:
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddDbContext<CountryContext>(options => options.UseSqlServer(Configuration.GetConnectionString("CountryContext"))); }
The AddRazorPages()
method allows us to use page model features and the AddDbContext<T>()
connect our app to a database. In our page model constructor, we can access the database service like so:
public class IndexModel : PageModel { private readonly CountryContext _context; public IndexModel(CountryContext context) { _context = context; } public IList<Country> Country { get;set; } public async Task OnGetAsync() { Country = await _context.Country.ToListAsync(); } }
Note that this is one exception to the Dependency Inversion Principle: we depend on the class CountryContext
instead of an interface because our app relies specifically on this type.
There are more common services that ASP.NET just assumes you need, such as ILogger
services. You don’t even need to register these in ConfigureServices()
. Those are registered automatically*. We can just request them in the page model:
public class PrivacyModel : PageModel { private readonly ILogger<PrivacyModel> _logger; public PrivacyModel(ILogger<PrivacyModel> logger) { _logger = logger; } }
* Those “automatically” registered services are actually determined by the host type, which is defined in Program.cs. For more information, visit the Microsoft documentation.
Instructions
For this exercise, we’ve provided a database of Tower
objects. We’ll access the database via the TowerContext
class.
In Startup.cs, add the database context service with this code:
services.AddDbContext<TowerContext>(options => options.UseSqlite(Configuration.GetConnectionString("TowerContext")));
This code registers TowerContext
as a service, uses a SQLite database, and connects to that database using the connection string defined in appsettings.json as "TowerContext"
.
Access the TowerContext
service in Index.cshtml.cs:
- Request the service by adding a
TowerContext
parameter in the constructor. - Store the parameter value in the pre-defined
_context
field.
TowerContext
has a Towers
property, which has a ToListAsync()
method. We’ll be using that to access the database records.
- In
OnGetAsync()
, call the field’sTowers.ToListAsync()
method and store the returned value inTowers
(It’s an asynchronous method, so you’ll need to use theawait
prefix).
In the same constructor, request the logging service by adding an ILogger<IndexModel>
parameter to the constructor and storing it in the pre-defined _logger
field.
For brevity, we won’t actually be using the logger in this code. If we did, the pattern is the same: we’d use a method like LogDebug()
in the handler methods.