public void AsModelBinderForTypesRegistersTypeModelBinder() { IDependencyResolver originalResolver = null; try { originalResolver = DependencyResolver.Current; var builder = new ContainerBuilder(); builder.RegisterType <TestModelBinder>().AsModelBinderForTypes(typeof(TestModel1), typeof(TestModel2)); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container, new StubLifetimeScopeProvider(container))); var provider = new AutofacModelBinderProvider(); Assert.IsInstanceOf <TestModelBinder>(provider.GetBinder(typeof(TestModel1)), "The model binder was not registered for TestModel1"); Assert.IsInstanceOf <TestModelBinder>(provider.GetBinder(typeof(TestModel2)), "The model binder was not registered for TestModel2"); } finally { DependencyResolver.SetResolver(originalResolver); } }
public void AsModelBinderForTypesRegistersInstanceModelBinder() { IDependencyResolver originalResolver = null; try { originalResolver = DependencyResolver.Current; var builder = new ContainerBuilder(); var binder = new TestModelBinder(); builder.RegisterInstance(binder).AsModelBinderForTypes(typeof(TestModel1)); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container, new StubLifetimeScopeProvider(container))); var provider = new AutofacModelBinderProvider(); Assert.AreSame(binder, provider.GetBinder(typeof(TestModel1))); } finally { DependencyResolver.SetResolver(originalResolver); } }