asp.net core - looking for samples on how to user services.add* in asp.vnext -
i know can find samples explains differences among services.addinstance
, services.addscoped
, services.addsingleton
, service.addtransient
.
i found articles explain point in generic way, think source sample more clear.
the scope of questions rather large, since seems looking addscoped
information narrowed sample down scoping inside web application.
inside web application addscoped
mean pretty scope of request. entityframework
using scoping internally, doesn't affect user code in cases i'm sticking user code shown below.
if register dbcontext
service, , register scoped service, each request single instance of scoped service resolve dbcontext
.
the example code below should make clearer. in general recommend trying out way i'm showing below familiarize behavior, stepping through code in debugger. start empty web application. note code i'm showing beta2 (since in beta2 added [fromservices]
attribute makes easier demonstrate, underlying behavior same regardless of version.
startup.cs
public void configureservices(iservicecollection services) { // add ef services services container. services.addentityframework(configuration) .addsqlserver() .adddbcontext<userdbcontext>(); services.addscoped<userservice>(); // add mvc services services container. services.addmvc(); }
userdbcontext.cs
public class userdbcontext : dbcontext { public userservice userservice { get; } public userdbcontext(userservice userservice) { _userservice = userservice; } }
homecontroller.cs
public class homecontroller : controller { private userdbcontext _dbcontext; public homecontroller(userdbcontext dbcontext) { _dbcontext = dbcontext; } public string index([fromservices]userdbcontext dbcontext, [fromservices]userservice userservice) { // [fromservices] available start beta2, , resolve service di // dbcontext == _ctrcontext // , of course dbcontext.userservice == _ctrcontext.userservice; if (dbcontext != _dbcontext) throw new invalidoperationexception(); if (dbcontext.userservice != _dbcontext.userservice) throw new invalidoperationexception(); if (dbcontext.userservice != userservice) throw new invalidoperationexception(); return "match"; } }
alternatively if resolve user service service, time registered transient transient service have new instance everytime resolved, scoped service remain same within scope of request.
create new service
public class anotheruserservice { public userservice userservice { get; } public anotheruserservice(userservice userservice) { userservice = userservice; } }
add following lines startup.cs
services.addtransient<anotheruserservice>();
and rewrite homecontroller.cs follows public class homecontroller : controller { private anotheruserservice _anotheruserservice;
public homecontroller(anotheruserservice anotheruserservice) { _anotheruserservice = anotheruserservice; } public string index([fromservices]anotheruserservice anotheruserservice, [fromservices]userservice userservice) { // since user service tranient expect new instance if (anotheruserservice == _anotheruserservice) throw new invalidoperationexception(); // scoped service should remain same instance if (anotheruserservice.userservice != _anotheruserservice.userservice) throw new invalidoperationexception(); if (anotheruserservice.userservice != userservice) throw new invalidoperationexception(); return "match"; } }
Comments
Post a Comment