public ActionValueBinderTracer(IActionValueBinder innerBinder, ITraceWriter traceWriter)
        {
            Contract.Assert(innerBinder != null);
            Contract.Assert(traceWriter != null);

            _innerBinder = innerBinder;
            _traceWriter = traceWriter;
        }
        public PerRequestActionValueBinder(IActionValueBinder innerActionValueBinder)
        {
            if (innerActionValueBinder == null)
            {
                throw Error.ArgumentNull("innerActionValueBinder");
            }

            _innerActionValueBinder = innerActionValueBinder;
        }
Ejemplo n.º 3
0
        public void Controller_Set_Null()
        {
            HttpConfiguration config = new HttpConfiguration();
            ServicesContainer global = config.Services;

            ControllerServices cs = new ControllerServices(config.Services);

            // Act
            // Setting to null is not the same as clear. Clear() means fall through to global config.
            cs.Replace(typeof(IActionValueBinder), null);

            // Assert
            IActionValueBinder localVal = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder));

            Assert.Null(localVal);
        }
Ejemplo n.º 4
0
        private static HttpActionBinding GetActionBinding(HttpActionDescriptor actionDescriptor)
        {
            HttpControllerDescriptor controllerDescriptor = actionDescriptor.ControllerDescriptor;

            if (controllerDescriptor == null)
            {
                return(null);
            }

            ServicesContainer  controllerServices = controllerDescriptor.Configuration.Services;
            IActionValueBinder actionValueBinder  = controllerServices.GetActionValueBinder();
            HttpActionBinding  actionBinding      =
                actionValueBinder != null?actionValueBinder.GetBinding(actionDescriptor) : null;

            return(actionBinding);
        }
        private Collection <ApiDescription> InitializeApiDescriptions()
        {
            Collection <ApiDescription> apiDescriptions = new Collection <ApiDescription>();

            //webapi
            foreach (var item in base.ApiDescriptions)
            {
                apiDescriptions.Add(item);
            }

            //dynamic api
            var dynamicapiinfos = DynamicApiControllerManager.GetAll();

            foreach (var dynamicapiinfo in dynamicapiinfos)
            {
                foreach (var item in dynamicapiinfo.Actions)
                {
                    ApiDescription api        = new ApiDescription();
                    var            httpaction = new HttpControllerDescriptor();
                    httpaction.Configuration  = _config;
                    httpaction.ControllerType = dynamicapiinfo.ServiceInterfaceType;
                    httpaction.ControllerName = dynamicapiinfo.ServiceName;
                    var action = new DynamicHttpActionDescriptor(_abpWebApiConfiguration, httpaction, item.Value);
                    api.ActionDescriptor = action;
                    api.HttpMethod       = GetMethod(item.Value.Verb);
                    IActionValueBinder actionValueBinder = _config.Services.GetActionValueBinder();
                    HttpActionBinding  actionBinding     = actionValueBinder.GetBinding(action);

                    //parameter
                    IList <ApiParameterDescription> parameterDescriptions = CreateParameterDescription(actionBinding, action);
                    //using refletions to internal set
                    var prop = typeof(ApiDescription).GetProperties().Where(p => p.Name == "ParameterDescriptions").SingleOrDefault();
                    prop.SetValue(api, new Collection <ApiParameterDescription>(parameterDescriptions));


                    //resopnse
                    ResponseDescription responseDescription = CreateResponseDescription(action);
                    var prop2 = typeof(ApiDescription).GetProperties().Where(p => p.Name == "ResponseDescription").SingleOrDefault();
                    prop2.SetValue(api, responseDescription);

                    api.RelativePath = "api/services/" + dynamicapiinfo.ServiceName + "/" + item.Value.ActionName;

                    apiDescriptions.Add(api);
                }
            }
            return(apiDescriptions);
        }
        public void Decorator_GetInner_Property_On_ActionValueBinderTracer_Returns_IActionValueBinder()
        {
            // Arrange
            IActionValueBinder      expectedInner    = new Mock <IActionValueBinder>().Object;
            ActionValueBinderTracer productUnderTest = new ActionValueBinderTracer(
                expectedInner,
                new TestTraceWriter()
                );

            // Act
            IActionValueBinder actualInner = Decorator.GetInner(
                productUnderTest as IActionValueBinder
                );

            // Assert
            Assert.Same(expectedInner, actualInner);
        }
        public void Get_Falls_Through_To_Global()
        {
            HttpConfiguration  config = new HttpConfiguration();
            ControllerServices cs     = new ControllerServices(config.Services);

            // Act
            IActionValueBinder localVal = (IActionValueBinder)cs.GetService(
                typeof(IActionValueBinder)
                );
            IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(
                typeof(IActionValueBinder)
                );

            // Assert
            // Local controller didn't override, should get same value as global case.
            Assert.Same(localVal, globalVal);
        }
        public void Controller_Overrides_Global()
        {
            HttpConfiguration  config = new HttpConfiguration();
            ControllerServices cs     = new ControllerServices(config.Services);

            IActionValueBinder newLocalService = new Mock <IActionValueBinder>().Object;

            cs.Replace(typeof(IActionValueBinder), newLocalService);

            // Act
            IActionValueBinder localVal  = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder));
            IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder));

            // Assert
            // Local controller didn't override, should get same value as global case.
            Assert.Same(localVal, newLocalService);
            Assert.NotSame(localVal, globalVal);
        }
        public void Controller_Clear_Single_Item()
        {
            HttpConfiguration config = new HttpConfiguration();
            ServicesContainer global = config.Services;

            ControllerServices cs = new ControllerServices(config.Services);
            IActionValueBinder newLocalService = new Mock <IActionValueBinder>().Object;

            cs.Replace(typeof(IActionValueBinder), newLocalService);

            // Act
            cs.Clear(typeof(IActionValueBinder));

            // Assert
            IActionValueBinder localVal  = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder));
            IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder));

            Assert.Same(globalVal, localVal);
        }
        /// <summary>
        /// Callback invoked to set per-controller overrides for this controllerDescriptor.
        /// </summary>
        /// <param name="controllerSettings">The controller settings to initialize.</param>
        /// <param name="controllerDescriptor">The controller descriptor. Note that the <see
        /// cref="T:System.Web.Http.Controllers.HttpControllerDescriptor" /> can be associated with the derived
        /// controller type given that <see cref="T:System.Web.Http.Controllers.IControllerConfiguration" /> is
        /// inherited.</param>
        public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
        {
            if (controllerSettings == null)
            {
                throw Error.ArgumentNull("controllerSettings");
            }

            if (controllerDescriptor == null)
            {
                throw Error.ArgumentNull("controllerDescriptor");
            }

            // If any OData formatters are registered globally, do nothing and use those instead
            MediaTypeFormatterCollection controllerFormatters = controllerSettings.Formatters;

            if (!controllerFormatters.Where(f => f != null && Decorator.GetInner(f) is ODataMediaTypeFormatter).Any())
            {
                // Remove Xml and Json formatters to avoid media type conflicts.
                RemoveFormatters(controllerFormatters,
                                 controllerFormatters.Where(f => f is XmlMediaTypeFormatter || f is JsonMediaTypeFormatter));

                // Then add our formatters.
                controllerFormatters.InsertRange(0, CreateODataFormatters());
            }

            ServicesContainer services = controllerSettings.Services;

            Contract.Assert(services != null);

            // Replace the action value binder with one that attaches the request to the formatter.
            IActionValueBinder originalActionValueBinder = services.GetActionValueBinder();
            IActionValueBinder actionValueBinder         = new PerRequestActionValueBinder(originalActionValueBinder);

            controllerSettings.Services.Replace(typeof(IActionValueBinder), actionValueBinder);

            // Replace the content negotiator with one that uses the per-request formatters.
            // This negotiator is required to allow CanReadType to have access to the model.
            IContentNegotiator originalContentNegotiator = services.GetContentNegotiator();
            IContentNegotiator contentNegotiator         = new PerRequestContentNegotiator(originalContentNegotiator);

            controllerSettings.Services.Replace(typeof(IContentNegotiator), contentNegotiator);
        }
Ejemplo n.º 11
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.º 12
0
        private void Controller_Overrides_DependencyInjection_Impl(IDependencyResolver resolver, TestActionValueBinder newDIService)
        {
            if (resolver is null)
            {
                throw new ArgumentNullException(nameof(resolver));
            }

            // Calling `.BeginScope()` here is to simulate what happens inside `DefaultHttpControllerActivator.GetInstanceOrActivator`
            using (IDependencyScope requestScope = resolver.BeginScope())
            {
                _ = requestScope.ShouldNotBeNull();

                // Setting on Controller config overrides the DI container.
                HttpConfiguration config = new HttpConfiguration();

//				IActionValueBinder newDIService = new TestActionValueBinder();

                config.DependencyResolver = resolver;

                ControllerServices cs = new ControllerServices(config.Services);

                IActionValueBinder newLocalService = new TestActionValueBinder();
                cs.Replace(typeof(IActionValueBinder), newLocalService);

                // Act
                IActionValueBinder localVal  = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder));
                IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder));

                _ = localVal.ShouldNotBeNull().ShouldBeOfType <TestActionValueBinder>();
                _ = globalVal.ShouldNotBeNull().ShouldBeOfType <TestActionValueBinder>();

                // Assert
                // Local controller didn't override, should get same value as global case.
                Object.ReferenceEquals(newDIService, globalVal).ShouldBeTrue();                   // asking the config will give back the DI service
                Object.ReferenceEquals(newLocalService, localVal).ShouldBeTrue();                 // but asking locally will get back the local service.
            }
        }
 public ActionValueBinderTracer(IActionValueBinder innerBinder, ITraceWriter traceWriter)
 {
     _innerBinder = innerBinder;
     _traceWriter = traceWriter;
 }
Ejemplo n.º 14
0
 public ActionValueBinderTracer(IActionValueBinder innerBinder, ITraceWriter traceWriter)
 {
     _innerBinder = innerBinder;
     _traceWriter = traceWriter;
 }