/// <inheritdoc />
            /// <summary>
            /// Executes an action.
            /// </summary>
            /// <param name="controllerContext">Controller context.</param>
            /// <param name="cancellationToken">Cancellation token.</param>
            /// <returns>Response message.</returns>
            public Task <HttpResponseMessage> ExecuteAsync(
                HttpControllerContext controllerContext,
                CancellationToken cancellationToken)
            {
                if (_controller == null)
                {
                    var request = controllerContext.Request;

                    if (request
                        .GetDependencyScope()
                        .GetService(typeof(IUnityContainer)) is IUnityContainer container)
                    {
                        container.RegisterInstance(controllerContext);
                        container.RegisterInstance(request);
                        container.RegisterInstance(cancellationToken);

                        UnityTypesRegistrator.RegisterTypes(container);
                    }

                    _controller = Activator.Create(
                        request,
                        controllerContext.ControllerDescriptor,
                        ControllerType);
                }

                controllerContext.Controller = _controller;

                return(_controller.ExecuteAsync(controllerContext, cancellationToken));
            }
Beispiel #2
0
        /// <inheritdoc />
        /// <summary>Retrieves the controller instance for the specified request context and controller type.</summary>
        /// <returns>The controller instance.</returns>
        /// <param name="requestContext">The context of the HTTP request, which includes the HTTP context and route data.</param>
        /// <param name="controllerType">The type of the controller.</param>
        /// <exception cref="T:System.Web.HttpException">
        /// <paramref name="controllerType" /> is null.</exception>
        /// <exception cref="T:System.ArgumentException">
        /// <paramref name="controllerType" /> cannot be assigned.</exception>
        /// <exception cref="T:System.InvalidOperationException">An instance of <paramref name="controllerType" /> cannot be created.</exception>
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new ArgumentNullException(nameof(controllerType));
            }

            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException($"Type requested is not a controller: {controllerType.Name}", nameof(controllerType));
            }

            UnityTypesRegistrator.RegisterTypes(_container);

            return(_container.Resolve(controllerType) as IController);
        }