c# - Why is my Funq container not working properly? -


i'm getting null reference exception when hitting code tries access database.

this in global.asax , have stepped through debugger , code being executed.

public class mvcapplication : system.web.httpapplication {     public class dmsapphost : apphostbase     {         public dmsapphost() : base("api", typeof(appusersservice).assembly) { }          public override void configure(funq.container container)         {             var dbconnectionfactory = new ormliteconnectionfactory("server=localhost;port=5432;user id=dms; password=dms; database=dms", postgresqldialect.provider);             container.register<idbconnectionfactory>(dbconnectionfactory);             container.registerautowiredas<appuserrepository, iappuserrepository>();         }     }      protected void application_start()     {         new dmsapphost().init();          arearegistration.registerallareas();          filterconfig.registerglobalfilters(globalfilters.filters);         routeconfig.registerroutes(routetable.routes);         bootstrapsupport.bootstrapbundleconfig.registerbundles(system.web.optimization.bundletable.bundles);     }  } 

when hit code (using (var db = dbconnectionfactory.opendbconnection())) getting nullreferenceexception.

object reference not set instance of object.

public class appuserrepository : iappuserrepository {     public idbconnectionfactory dbconnectionfactory { get; set; }      public appuser getappuserbyusername(string username)     {         appuser appuser;         using (var db = dbconnectionfactory.opendbconnection())         {             appuser = db.querysingle<appuser>(new { username });         }         return appuser;     } } 

am assume wrong connection or there i'm not doing right make funq work?

you have shown funq container configuration injecting dependencies appuserrepository class. haven't shown how , class used. since question tagged asp.net-mvc assume happens inside asp.net mvc controller action. along lines of:

public class homecontroller: controller {     private readonly iappuserrepository repo;     public homecontroller(iappuserrepository repo)     {         this.repo = repo;     }      public actionresult index(int id)     {         var model = this.repo.get(id);         return view(model);     } } 

if want use dependency injection framework in asp.net mvc have write custom dependency resolver injection controllers.

so let's go ahead , write 1 funq:

public class funqdependencyresolver : idependencyresolver {     private readonly containerresolvecache cache;     public funqdependencyresolver(container container)     {         this.cache = new containerresolvecache(container);         var controllertypes =             (from type in assembly.getcallingassembly().gettypes()              typeof(icontroller).isassignablefrom(type)              select type).tolist();         container.registerautowiredtypes(controllertypes);     }      public object getservice(type servicetype)     {         return this.cache.createinstance(servicetype, true);     }      public ienumerable<object> getservices(type servicetype)     {         return enumerable.empty<object>();     } } 

and register in application_start:

protected void application_start() {     var container = new container();     var dbconnectionfactory = new ormliteconnectionfactory("server=localhost;port=5432;user id=dms; password=dms; database=dms", postgresqldialect.provider);     container.register<idbconnectionfactory>(dbconnectionfactory);     container.registerautowiredas<appuserrepository, iappuserrepository>();     dependencyresolver.setresolver(new funqdependencyresolver(container));      arearegistration.registerallareas();      filterconfig.registerglobalfilters(globalfilters.filters);     routeconfig.registerroutes(routetable.routes);     bootstrapsupport.bootstrapbundleconfig.registerbundles(system.web.optimization.bundletable.bundles); } 

also recommend using constructor injection appuserrepository class because dbconnectionfactory absolutely required dependency class work properly:

public class appuserrepository : iappuserrepository {     private readonly idbconnectionfactory dbconnectionfactory;     public class appuserrepository(idbconnectionfactory dbconnectionfactory)     {         this.dbconnectionfactory = dbconnectionfactory;     }      public appuser getappuserbyusername(string username)     {         appuser appuser;         using (var db = this.dbconnectionfactory.opendbconnection())         {             appuser = db.querysingle<appuser>(new { username });         }         return appuser;     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -