c# - Autofac injects objects with the same reference -
i keep getting reference same object. can see in constructor there 2 objects same type. expect 2 different instances whenever set property of _player1, _player2 being set accordingly same value. doing wrong? thanks
ioc container
public static void configurecontainer() { var builder = new containerbuilder(); builder.registercontrollers(typeof(mvcapplication).assembly); builder.registerinstance<iplayer>(new player()); var container = builder.build(); dependencyresolver.setresolver(new autofacdependencyresolver(container)); }
controller constructor
public flaggamecontroller(iplayer player1, iplayer player2) { _player1 = player1; _player2 = player2; _currentplayer = player1; }
when
builder.registerinstance
you setting return same instance. if want return different instance every time, use
builder.registertype<player>().as<iplayer>().instanceperdependency();
ref - http://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-per-dependency
Comments
Post a Comment