سؤال

Im having trouble understand why all my classes derived from ApiController is registered and accessible. I have two ApiControllers:

MasterController : ApiController
SlaveController : ApiController

And here is how i setup the server with the 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 a NUnit test is that even though i only try to register a single controller, both of them are accessible during the test. If i only register the MasterController for instance, i expect this url to be valid (master)

var response = await client.GetAsync(new Uri(http://localhost:8080, "api/v1/master/mytest"));

and this url to give me a 404 (slave)

var response = await client.GetAsync(new Uri(http://localhost:8080, "api/v1/slave/mytest"));

but instead both of them work. I have tried all types of different registrations with autofac, but all of them seems to register all ApiControllers and not just the one i want, what am i missing?

var cb = new ContainerBuilder();

// tried this one and added alot of .where´s to try and filter on it
cb.RegisterApiControllers(typeof (MasterController).Assembly);

// tried many different version, dont have to list them all since obviously im missing something
cb.RegisterType<MasterController>();
cb.RegisterType(typeof(MasterController));

var container = cb.Build();

My goal is to only register the MasterController during the test, and understand what im doing wrong when attempting that.

هل كانت مفيدة؟

المحلول

I found out through a collegue that i was poking at the wrong place, it doesnt matter how i setup the container because the code that bootstraps this is not the DependencyResolver.

I added this line to my code, when setting up the server:

httpConf.Services.Replace(typeof(IHttpControllerTypeResolver), new AutofacHttpControllerTypeResolver(container));   

and this is how my 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 = from r in _container.ComponentRegistry.Registrations
                        let t = r.Activator.LimitType
                        where typeof(IHttpController).IsAssignableFrom(t) && t.Name.EndsWith("Controller")
                        select t;
                return q.ToList();
            }
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top