Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        private void GetServices_CachesResultFromDependencyInjectionContainer_Impl(IDependencyResolver resolver)
        {
            DependencyResolverWrapper rw = new DependencyResolverWrapper(resolver, allowBeginScope: true);

            // Arrange
            HttpConfiguration config          = new HttpConfiguration();
            DefaultServices   defaultServices = new DefaultServices(config);

            config.DependencyResolver = rw;

            // Act
            Object a = defaultServices.GetServices(typeof(IFilterProvider));
            Object b = defaultServices.GetServices(typeof(IFilterProvider));

            _ = a.ShouldNotBeNull();
            _ = b.ShouldNotBeNull();

            // Assert
            rw.GetServicesCalls.Count.ShouldBe(1);
//			mockDependencyResolver.Verify(dr => dr.GetServices(typeof(IFilterProvider)), Times.Once());
        }
Ejemplo n.º 3
0
        private void Create_ThrowsForNullDependencyScope_Impl(IDependencyResolver resolverOrig)
        {
            IDependencyResolver resolver2 = new DependencyResolverWrapper(inner: resolverOrig, allowBeginScope: false);

            // Arrange
            HttpConfiguration config = new HttpConfiguration();

            config.DependencyResolver = resolver2;

            HttpRequestMessage request = new HttpRequestMessage();

            request.SetConfiguration(config);

            HttpControllerDescriptor descriptorSimpleController = new HttpControllerDescriptor(config, "Simple", typeof(SimpleController));

            DefaultHttpControllerActivator activator = new DefaultHttpControllerActivator();

            // Act & Assert
            try
            {
                IHttpController controller = activator.Create(request, descriptorSimpleController, typeof(SimpleController));

                true.ShouldBeFalse("Method did not throw an exception.");
            }
            catch (InvalidOperationException ioEx)
            {
                ioEx.Message.ShouldBe("An error occurred when trying to create a controller of type 'SimpleController'. Make sure that the controller has a parameterless public constructor.");

                // 'BeginScopeNull_DependencyResolver'
                // 'ObjectProxy(_\\d+)?'

                ioEx.InnerException.Message.ShouldBe(
                    "A dependency resolver of type '" + nameof(DependencyResolverWrapper) + "' returned an invalid value of null from its " +
                    "BeginScope method. If the container does not have a concept of scope, consider returning a scope " +
                    "that resolves in the root of the container instead."
                    );
            }
        }