kanayabhattad / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

IAutofacActionFilter.OnActionExecuting gets called twice post login #435

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The below code gets called one time during startup
builder.RegisterType<AjaxAuthRequiredActionFilter>().AsWebApiActionFilterFor<Val
uesController>()
                   .InstancePerApiRequest();

public class AjaxAuthRequiredActionFilter : IAutofacActionFilter
{
...
public AjaxAuthRequiredActionFilter(HttpContextBase context)
        {
            _context = context;
        }
public void OnActionExecuting(HttpActionContext actionContext){...}
 public void OnActionExecuted(HttpActionExecutedContext actionExecutedContext){...}
}
I use 
            builder.RegisterModule(new AutofacWebTypesModule());
to assure that HttpContextBase gets injected.

Strange behavior.  
When NOT logged in, this filter will get called one time each for 
OnActionExecuting and OnActionExecuted.
When Logged on
It gets called twice.
order of breakpoints.
1. AjaxAuthRequiredActionFilter constructor
2. OnActionExecuting - 2 times
3. The ValuesController.Get method
4. OnActionExecuted - 2 times.

Why?
Again, pre login, works as I would suspect
post login, 2 times each.

This is my registration code

public static void RegisterAllIocs()
{
 Log.Info("Start RegisterAllIocs");

 var assemblies = AppDomain.CurrentDomain.GetAssemblies();
 var builder = new ContainerBuilder();

 builder.RegisterControllers(assemblies);
 builder.RegisterApiControllers(assemblies);

                  builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

 var autofacModules = new List<Module>();

            GatherRegistrants(f =>
                {
                    var typeAssembly = f.GetType().Assembly;
                    Log.InfoFormat("found {0}", f.GetType().ToString());
                    var registration = (IoCRegistration) Activator.CreateInstance(f.GetType());
                    var var = typeAssembly.Location.Substring(0, typeAssembly.Location.LastIndexOf('\\'));

                    var currentAutofacModules = registration.FetchAutofacModules();
                    autofacModules.AddRange(currentAutofacModules);
                });
            builder.RegisterModule(new IoCModules(autofacModules));
            builder.RegisterModule(new AutofacWebTypesModule());
            builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

            var container = builder.Build();

            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            // this sets up the Common Service Locator abstraction from Microsoft Patterns & Practices
            //http://commonservicelocator.codeplex.com/

            var autofacServiceLocator =
                new Autofac.Extras.CommonServiceLocator.AutofacServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => autofacServiceLocator);

            var resolver = new AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(resolver);

            Log.Info("End RegisterAllIocs");
        }

Let me know if you need more info.
Thanks

Original issue reported on code.google.com by ghst...@gmail.com on 18 May 2013 at 12:25

GoogleCodeExporter commented 8 years ago
Sorry, user is idiot.
apparently calling
 builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
twice does not lead to a desired behavior.

Original comment by ghst...@gmail.com on 20 May 2013 at 8:14

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 21 May 2013 at 1:21