public void Mvc_DependencyResolver_GetService_should_return_null_for_unregistered_types() { ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(services => { }) .AddMvcDependencyResolver() .Build(); using ( di ) { DependencyInjectionMvcDependencyResolver mvcResolverInstance = di.GetClients() .OfType <DependencyInjectionMvcDependencyResolver>() .Single(); // IDependencyResolver mvcResolver = mvcResolverInstance; // { Object implementation = mvcResolver.GetService(typeof(INonRegisteredService)); implementation.ShouldBeNull(); } // And call it again for good measure: { Object implementation = mvcResolver.GetService(typeof(INonRegisteredService)); implementation.ShouldBeNull(); } } }
public void Mvc_DependencyResolver_GetServices_should_return_empty_enumerable_when_unresolved() { ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(services => { }) .AddMvcDependencyResolver() .Build(); using ( di ) { DependencyInjectionMvcDependencyResolver mvcResolverInstance = di.GetClients() .OfType <DependencyInjectionMvcDependencyResolver>() .Single(); // IDependencyResolver mvcResolver = mvcResolverInstance; // IEnumerable <Object> services = mvcResolver.GetServices(typeof(INonRegisteredService)); _ = services.ShouldNotBeNull(); services.Count().ShouldBe(expected: 0); // Check twice, in case of IEnumerable<T> implementation shenanigans: services.Count().ShouldBe(expected: 0); } }
// See \AspNetWebStack\test\System.Web.Mvc.Test\Test\DependencyResolverTest.cs /// <summary>Don't call this method from tests that verify this logic works, like <see cref="AddWebApiDependencyResolver_should_add_single_DependencyInjectionWebApiDependencyResolver"/>.</summary> private static ApplicationDependencyInjection CreateMvcDIWithTestServiceRegistrations(out IDependencyResolver mvcResolver) { ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(services => { _ = services.AddTransient <ITestTransient, TestTransient>(); _ = services.AddScoped <ITestScoped, TestScoped>(); _ = services.AddSingleton <ITestSingleton, TestSingleton>(); _ = services.AddSingleton <TestAbstractClass, TestAbstractImpl>(); _ = services.AddSingleton(serviceType: typeof(ITestGeneric <>), implementationType: typeof(TestestGeneric <>)); }) .AddMvcDependencyResolver() .Build(); DependencyInjectionMvcDependencyResolver mvcResolverInstance = di.GetClients() .OfType <DependencyInjectionMvcDependencyResolver>() .Single(); mvcResolver = mvcResolverInstance; return(di); }