c# - Why is autofac registering all instances of apicontroller instead of just the one i specify -
im having trouble understand why classes derived apicontroller registered , accessible. have 2 apicontrollers:
mastercontroller : apicontroller slavecontroller : apicontroller
and here how setup server container
action<iappbuilder> appbuilderaction = appbuilder => { var httpconf = new httpconfiguration(); httpconf.dependencyresolver = new autofacwebapidependencyresolver(container); httpconf.routes.maphttproute("defaultapi", "api/v1/{controller}/{action}", new { action = "get" }); appbuilder.usewebapi(httpconf); }; return webapp.start(baseaddress, appbuilderaction);
the problem im having when creating nunit test though try register single controller, both of them accessible during test. if register mastercontroller instance, expect url valid (master)
var response = await client.getasync(new uri(http://localhost:8080, "api/v1/master/mytest"));
and url give me 404 (slave)
var response = await client.getasync(new uri(http://localhost:8080, "api/v1/slave/mytest"));
but instead both of them work. have tried types of different registrations autofac, of them seems register apicontrollers , not 1 want, missing?
var cb = new containerbuilder(); // tried 1 , added alot of .where´s try , filter on cb.registerapicontrollers(typeof (mastercontroller).assembly); // tried many different version, dont have list them since im missing cb.registertype<mastercontroller>(); cb.registertype(typeof(mastercontroller)); var container = cb.build();
my goal register mastercontroller during test, , understand im doing wrong when attempting that.
i found out through collegue poking @ wrong place, doesnt matter how setup container because code bootstraps not dependencyresolver.
i added line code, when setting server:
httpconf.services.replace(typeof(ihttpcontrollertyperesolver), new autofachttpcontrollertyperesolver(container));
and how autofachttpcontrollertyperesolver class looks like:
class autofachttpcontrollertyperesolver : ihttpcontrollertyperesolver { private readonly icontainer _container; public autofachttpcontrollertyperesolver(icontainer container) { this._container = container; } public icollection<type> getcontrollertypes(iassembliesresolver assembliesresolver) { var q = r in _container.componentregistry.registrations let t = r.activator.limittype typeof(ihttpcontroller).isassignablefrom(t) && t.name.endswith("controller") select t; return q.tolist(); } }
Comments
Post a Comment