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); }
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(); } }
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()); } }
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(); }
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)); }
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); }
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)); }
public TestContext(IGremlinLanguageConnector connector) : base(connector, Resolver.CreateScopedResolver()) { }
public GremlinTests(ITestOutputHelper output) { _output = output; _scope = Resolver.CreateScopedResolver(); }
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(); } }