public void GetService_GuardClauses() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); // Act & assert ExceptionAssert.ThrowsArgumentNull(() => defaultServices.GetService(serviceType: null), "serviceType"); ExceptionAssert.ThrowsArgument(() => defaultServices.GetService(typeof(object)), "serviceType"); }
public void GetService_CachesResultFromDependencyInjectionContainer() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); var mockDependencyResolver = new Mock <IDependencyResolver>(); config.DependencyResolver = mockDependencyResolver.Object; // Act defaultServices.GetService(typeof(ICommandHandlerSelector)); defaultServices.GetService(typeof(ICommandHandlerSelector)); // Assert mockDependencyResolver.Verify(dr => dr.GetService(typeof(ICommandHandlerSelector)), Times.Once()); }
public void Class_IsDefaultIAssembliesResolver() { var serviceResolver = new DefaultServices(new HttpConfiguration()); Assert.IsType <DefaultAssembliesResolver>( serviceResolver.GetService(typeof(IAssembliesResolver)) ); }
// Runtime code shouldn't call GetService() directly. Instead, have a wrapper (like the ones above) and call through the wrapper. private static TService GetService <TService>(this DefaultServices services) { if (services == null) { throw Error.ArgumentNull("services"); } return((TService)services.GetService(typeof(TService))); }
private static T GetServiceOrThrow <T>(this DefaultServices services) { T result = services.GetService <T>(); if (result == null) { throw Error.InvalidOperation(SRResources.DependencyResolverNoService, typeof(T).FullName); } return(result); }
private void GetService_CachesResultFromDependencyInjectionContainer_Impl(IDependencyResolver resolver) { DependencyResolverWrapper resolverWrapper = new DependencyResolverWrapper(resolver, allowBeginScope: true); // Arrange HttpConfiguration config = new HttpConfiguration(); DefaultServices defaultServices = new DefaultServices(config); config.DependencyResolver = resolverWrapper; // Act IActionValueBinder a = (IActionValueBinder)defaultServices.GetService(typeof(IActionValueBinder)); IActionValueBinder b = (IActionValueBinder)defaultServices.GetService(typeof(IActionValueBinder)); // Assert resolverWrapper.GetServiceCalls.Count.ShouldBe(1); resolverWrapper.GetServicesCalls.Count.ShouldBe(0); Object.ReferenceEquals(a, b).ShouldBeTrue(); }
public void Constructor_DefaultServicesInContainer() { // Arrange var config = new ProcessorConfiguration(); // Act var defaultServices = new DefaultServices(config); // Assert Assert.IsType(typeof(DefaultCommandHandlerSelector), defaultServices.GetService(typeof(ICommandHandlerSelector))); Assert.IsType(typeof(DefaultCommandHandlerActivator), defaultServices.GetService(typeof(ICommandHandlerActivator))); Assert.IsType(typeof(DefaultCommandHandlerTypeResolver), defaultServices.GetService(typeof(ICommandHandlerTypeResolver))); Assert.IsType(typeof(DefaultAssembliesResolver), defaultServices.GetService(typeof(IAssembliesResolver))); Assert.IsType(typeof(DefaultInterceptionProvider), defaultServices.GetService(typeof(IInterceptionProvider))); object[] filterProviders = defaultServices.GetServices(typeof(IFilterProvider)).ToArray(); Assert.Equal(2, filterProviders.Length); Assert.IsType(typeof(ConfigurationFilterProvider), filterProviders[0]); Assert.IsType(typeof(HandlerFilterProvider), filterProviders[1]); Assert.IsType(typeof(DefaultCommandValidator), defaultServices.GetService(typeof(ICommandValidator))); object[] interceptors = defaultServices.GetServices(typeof(IInterceptor)).ToArray(); Assert.Equal(0, interceptors.Length); }
public void Constructor_DefaultServicesInContainer() { // Arrange var config = new ProcessorConfiguration(); // Act var defaultServices = new DefaultServices(config); // Assert Assert.IsType(typeof(DefaultCommandHandlerSelector), defaultServices.GetService(typeof(ICommandHandlerSelector))); Assert.IsType(typeof(DefaultCommandHandlerActivator), defaultServices.GetService(typeof(ICommandHandlerActivator))); Assert.IsType(typeof(DefaultCommandHandlerTypeResolver), defaultServices.GetService(typeof(ICommandHandlerTypeResolver))); Assert.IsType(typeof(DefaultAssembliesResolver), defaultServices.GetService(typeof(IAssembliesResolver))); Assert.IsType(typeof(DefaultInterceptionProvider), defaultServices.GetService(typeof(IInterceptionProvider))); object[] filterProviders = defaultServices.GetServices(typeof(IFilterProvider)).ToArray(); Assert.Equal(2, filterProviders.Length); Assert.IsType(typeof(ConfigurationFilterProvider), filterProviders[0]); Assert.IsType(typeof(HandlerFilterProvider), filterProviders[1]); Assert.IsType(typeof(DefaultCommandValidator), defaultServices.GetService(typeof(ICommandValidator))); object[] interceptors = defaultServices.GetServices(typeof(IInterceptor)).ToArray(); Assert.Equal(0, interceptors.Length); }
public void GetService_ReturnsNullWhenServiceListEmpty() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); defaultServices.Clear(typeof(ICommandHandlerSelector)); // Act object service = defaultServices.GetService(typeof(ICommandHandlerSelector)); // Assert Assert.Null(service); }
private void GetService_PrefersServiceInDependencyInjectionContainer_Impl(IDependencyResolver resolver, TestActionValueBinder serviceInstance) { // Arrange HttpConfiguration config = new HttpConfiguration(); DefaultServices defaultServices = new DefaultServices(config); IActionValueBinder filterProvider = serviceInstance; config.DependencyResolver = resolver; // Act object service = defaultServices.GetService(typeof(IActionValueBinder)); // Assert Object.ReferenceEquals(filterProvider, service).ShouldBeTrue(); }
public void GetService_PrefersServiceInDependencyInjectionContainer() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); var filterProvider = new Mock <ICommandHandlerSelector>().Object; var mockDependencyResolver = new Mock <IDependencyResolver>(); mockDependencyResolver.Setup(dr => dr.GetService(typeof(ICommandHandlerSelector))).Returns(filterProvider); config.DependencyResolver = mockDependencyResolver.Object; // Act object service = defaultServices.GetService(typeof(ICommandHandlerSelector)); // Assert Assert.Same(filterProvider, service); }
public static IBodyModelValidator GetBodyModelValidator(this DefaultServices services) { return(services.GetService <IBodyModelValidator>()); }
public void GetService_CachesResultFromDependencyInjectionContainer() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); var mockDependencyResolver = new Mock<IDependencyResolver>(); config.DependencyResolver = mockDependencyResolver.Object; // Act defaultServices.GetService(typeof(ICommandHandlerSelector)); defaultServices.GetService(typeof(ICommandHandlerSelector)); // Assert mockDependencyResolver.Verify(dr => dr.GetService(typeof(ICommandHandlerSelector)), Times.Once()); }
public void GetService_ReturnsNullWhenServiceListEmpty() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); defaultServices.Clear(typeof(ICommandHandlerSelector)); // Act object service = defaultServices.GetService(typeof(ICommandHandlerSelector)); // Assert Assert.Null(service); }
public void GetService_PrefersServiceInDependencyInjectionContainer() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); var filterProvider = new Mock<ICommandHandlerSelector>().Object; var mockDependencyResolver = new Mock<IDependencyResolver>(); mockDependencyResolver.Setup(dr => dr.GetService(typeof(ICommandHandlerSelector))).Returns(filterProvider); config.DependencyResolver = mockDependencyResolver.Object; // Act object service = defaultServices.GetService(typeof(ICommandHandlerSelector)); // Assert Assert.Same(filterProvider, service); }
public void GetService_GuardClauses() { // Arrange var config = new ProcessorConfiguration(); var defaultServices = new DefaultServices(config); // Act & assert ExceptionAssert.ThrowsArgumentNull(() => defaultServices.GetService(serviceType: null), "serviceType"); ExceptionAssert.ThrowsArgument(() => defaultServices.GetService(typeof(object)), "serviceType"); }
public static IDocumentationProvider GetDocumentationProvider(this DefaultServices services) { return(services.GetService <IDocumentationProvider>()); }
public static IContentNegotiator GetContentNegotiator(this DefaultServices services) { return(services.GetService <IContentNegotiator>()); }
public static IActionValueBinder GetActionValueBinder(this DefaultServices services) { return(services.GetService <IActionValueBinder>()); }
public static ITraceWriter GetTraceWriter(this DefaultServices services) { return(services.GetService <ITraceWriter>()); }