Fix lazy container resolver

This commit is contained in:
Stephan
2016-07-21 14:54:43 +02:00
parent 86968f218c
commit 3fb2b97069
4 changed files with 23 additions and 24 deletions

View File

@@ -13,13 +13,14 @@ namespace Umbraco.Web.HealthCheck
{
protected HealthCheck(HealthCheckContext healthCheckContext)
{
if (healthCheckContext == null) throw new ArgumentNullException(nameof(healthCheckContext));
HealthCheckContext = healthCheckContext;
//Fill in the metadata
var thisType = this.GetType();
var thisType = GetType();
var meta = thisType.GetCustomAttribute<HealthCheckAttribute>(false);
if (meta == null)
throw new InvalidOperationException(
string.Format("The health check {0} requires a {1}", thisType, typeof(HealthCheckAttribute)));
throw new InvalidOperationException($"The health check {thisType} requires a {typeof (HealthCheckAttribute)}");
Name = meta.Name;
Description = meta.Description;
Group = meta.Group;

View File

@@ -11,16 +11,16 @@ namespace Umbraco.Web.HealthCheck
{
public HealthCheckContext(HttpContextBase httpContext, UmbracoContext umbracoContext)
{
if (httpContext == null) throw new ArgumentNullException("httpContext");
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
if (httpContext == null) throw new ArgumentNullException(nameof(httpContext));
if (umbracoContext == null) throw new ArgumentNullException(nameof(umbracoContext));
HttpContext = httpContext;
UmbracoContext = umbracoContext;
ApplicationContext = UmbracoContext.Application;
}
public HttpContextBase HttpContext { get; private set; }
public UmbracoContext UmbracoContext { get; private set; }
public ApplicationContext ApplicationContext { get; private set; }
public HttpContextBase HttpContext { get; }
public UmbracoContext UmbracoContext { get; }
public ApplicationContext ApplicationContext => UmbracoContext.Application;
//TODO: Do we need any more info/service exposed here?
}

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LightInject;
using Umbraco.Core.Logging;
using Umbraco.Core.ObjectResolution;
@@ -17,11 +16,13 @@ namespace Umbraco.Web.HealthCheck
internal class HealthCheckResolver : ContainerLazyManyObjectsResolver<HealthCheckResolver, HealthCheck>, IHealthCheckResolver
{
public HealthCheckResolver(IServiceContainer container, ILogger logger, Func<IEnumerable<Type>> types)
: base(container, logger, types, ObjectLifetimeScope.HttpRequest)
: base(container, logger, types, ObjectLifetimeScope.Transient) // do NOT change .Transient, see CreateValues below
{ }
protected override IEnumerable<HealthCheck> CreateValues(ObjectLifetimeScope scope)
{
// note: constructor dependencies do NOT work with lifetimes other than transient
// see https://github.com/seesharper/LightInject/issues/294
EnsureTypesRegisterred(scope, container =>
{
// resolve ctor dependency from GetInstance() runtimeArguments, if possible - 'factory' is
@@ -32,7 +33,8 @@ namespace Umbraco.Web.HealthCheck
container.RegisterConstructorDependency((factory, info, args) => args.Length > 0 ? args[0] as HealthCheckContext : null);
});
return InstanceTypes.Select(x => (HealthCheck) Container.GetInstance(x, new object[] { _healthCheckContext }));
var arg = new object[] { _healthCheckContext };
return InstanceTypes.Select(x => (HealthCheck) Container.GetInstance(x, arg));
}
private HealthCheckContext _healthCheckContext;