示例#1
0
        private T CreateInternal <T>(Type controllerType, HttpContext requestContextHttpContext)
        {
            IDependencyResolver resolver;

            {
                if (requestContextHttpContext.Items.Contains("resolver"))
                {
                    resolver = requestContextHttpContext.Items["resolver"] as IDependencyResolver ??
                               Resolver.CreateScopedResolver();
                }
                else
                {
                    resolver = Resolver.CreateScopedResolver();
                }
            }
            if (!(requestContextHttpContext.Items["resolver"] is IDependencyResolver))
            {
                HttpContext.Current.Items.Add("resolver", resolver);
            }
            var context = resolver.GetService <HttpContextAccessor>();

            context.SetContext(requestContextHttpContext);
            OnCreatingController?.Invoke(resolver, context, controllerType);
            var controller = (T)controllerType.Activate(Scope.Context, resolver);

            OnControllerCreated?.Invoke(resolver, context, controller);
            return(controller);
        }
示例#2
0
        public override async Task <HttpResponseMessage> InvokeActionAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            IDependencyResolver resolver = null;

            try
            {
                resolver = HttpContext.Current.Items["resolver"] as IDependencyResolver;
                if (resolver == null)
                {
                    resolver = Resolver.CreateScopedResolver();
                }
                HttpContext.Current.Items.Add("logContextName",
                                              actionContext.ActionDescriptor.ControllerDescriptor.ControllerName);
                var logger = resolver.GetService <ILogging>();

                using (logger.CreateLogScope($"Invoking action {actionContext.ActionDescriptor.ActionName}"))
                {
                    var result = await base.InvokeActionAsync(actionContext, cancellationToken);

                    OnApiActionCompleted?.Invoke(resolver, actionContext);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ex.Log();
                throw;
            }
            finally
            {
                resolver.TryDispose();
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            ConfigurationManagerHelper.SetManager(new ConfigManager());
            Resolver.LoadModuleConfiguration <Dummy>();
            var resolver = Resolver.CreateScopedResolver();

            {
                var instance = resolver.GetService <IDummyService>();
                Console.WriteLine(instance.GetMessage());
            }
        }
示例#4
0
        public static IDependencyResolver GetOrCreateDependencyScope(this HttpContext context)
        {
            var resolver = context.CurrentResolver();

            if (resolver == null)
            {
                resolver = Resolver.CreateScopedResolver();
                context.Items.Add("resolver", resolver);
            }
            return(resolver);
        }
 internal Configurator(IConfigurationKernel kernel)
 {
     this.kernel = kernel;
     Items       = new Dictionary <string, object> {
         { "resolver", Resolver.CreateScopedResolver() }
     };
     ((IInternalConfigurator)this).Bind <IDependencyResolver>().To(_resolver.GetType())
     .SetRequestResponseScope();
     //((IInternalConfigurator) this).Bind<IDependencyResolver>().To(_resolver.GetType())
     //    .SetRequestResponseScope();
 }
示例#6
0
        public IDependencyScope BeginScope()
        {
            var resolver = Resolver.CreateScopedResolver();

            if (HttpContext.Current != null && !(HttpContext.Current.Items["resolver"] is IDependencyResolver))
            {
                HttpContext.Current.Items.Add("resolver", resolver);
            }
            resolver.GetService <ILogging>()?.DebugMessage("Starting dependency scope");
            return(new StardustDependencyScope(resolver));
        }
示例#7
0
        public void Resolver2Test()
        {
            ITestService item;

            using (var scope = Resolver.CreateScopedResolver())
            {
                item = scope.GetService <ITestService>("test");
                Assert.NotNull(item);
                Assert.Equal(item.Self.InstanceId, item.InstanceId);
            }
            Assert.True(item.IsDisposed);
        }
示例#8
0
        public IEnumerable <object> GetServices(Type serviceType)
        {
            IDependencyResolver resolver;

            if (HttpContext.Current.Items.Contains("resolver"))
            {
                resolver = HttpContext.Current.Items["resolver"] as IDependencyResolver ??
                           Resolver.CreateScopedResolver();
            }
            else
            {
                resolver = Resolver.CreateScopedResolver();
            }
            if (!(HttpContext.Current.Items["resolver"] is IDependencyResolver))
            {
                HttpContext.Current.Items.Add("resolver", resolver);
            }
            resolver.GetService <ILogging>()?.DebugMessage($"Resolving services {serviceType.FullName}");
            return(resolver.GetServices(serviceType));
        }
示例#9
0
 public TestContext(IGremlinLanguageConnector connector) : base(connector, Resolver.CreateScopedResolver())
 {
 }
示例#10
0
 public GremlinTests(ITestOutputHelper output)
 {
     _output = output;
     _scope  = Resolver.CreateScopedResolver();
 }
示例#11
0
        protected override ActionResult InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary <string, object> parameters)
        {
            IDependencyResolver resolver = null;

            try
            {
                resolver = HttpContext.Current.Items["resolver"] as IDependencyResolver ?? Resolver.CreateScopedResolver();
                var logger = resolver.GetService <ILogging>();
                using (logger.CreateLogScope($"Invoking action {actionDescriptor.ActionName}"))
                {
                    var result = base.InvokeActionMethod(controllerContext, actionDescriptor, parameters);
                    OnControllerActionCompleted?.Invoke(resolver, controllerContext);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ex.Log();
                throw;
            }
            finally
            {
                resolver.TryDispose();
            }
        }