diff --git a/src/Umbraco.Core/Persistence/Migrations/MigrationResolver.cs b/src/Umbraco.Core/Persistence/Migrations/MigrationResolver.cs index e038129e46..0512fe14c6 100644 --- a/src/Umbraco.Core/Persistence/Migrations/MigrationResolver.cs +++ b/src/Umbraco.Core/Persistence/Migrations/MigrationResolver.cs @@ -4,9 +4,6 @@ using System.Linq; using LightInject; using Umbraco.Core.Logging; using Umbraco.Core.ObjectResolution; -using Umbraco.Core.Persistence.SqlSyntax; -using System.Threading; -using Umbraco.Core.DependencyInjection; namespace Umbraco.Core.Persistence.Migrations { @@ -15,9 +12,9 @@ namespace Umbraco.Core.Persistence.Migrations /// internal class MigrationResolver : ContainerLazyManyObjectsResolver, IMigrationResolver { - + public MigrationResolver(IServiceContainer container, ILogger logger, Func> migrations) - : base(container, logger, migrations, ObjectLifetimeScope.Transient) + : base(container, logger, migrations, ObjectLifetimeScope.Transient) // do NOT change .Transient, see CreateValues below { } @@ -32,9 +29,11 @@ namespace Umbraco.Core.Persistence.Migrations /// protected override IEnumerable 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 + // resolve ctor dependency from GetInstance() runtimeArguments, if possible - 'factory' is // the container, 'info' describes the ctor argument, and 'args' contains the args that // were passed to GetInstance() - use first arg if it is the right type, // @@ -42,11 +41,8 @@ namespace Umbraco.Core.Persistence.Migrations container.RegisterConstructorDependency((factory, info, args) => args.Length > 0 ? args[0] as IMigrationContext : null); }); - foreach (var type in InstanceTypes) - { - //create each instance with the provided constructor argument - yield return (IMigration)Container.GetInstance(type, new object[] {_migrationContext}); - } + var arg = new object[] { _migrationContext }; + return InstanceTypes.Select(x => (IMigration) Container.GetInstance(x, arg)); } private IMigrationContext _migrationContext; @@ -58,7 +54,7 @@ namespace Umbraco.Core.Persistence.Migrations { //set the current context to use to create the values _migrationContext = migrationContext; - + return Values; } } diff --git a/src/Umbraco.Web/HealthCheck/HealthCheck.cs b/src/Umbraco.Web/HealthCheck/HealthCheck.cs index ec1358947a..b90cfc5c82 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheck.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheck.cs @@ -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(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; diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckContext.cs b/src/Umbraco.Web/HealthCheck/HealthCheckContext.cs index 4ec02a2386..f554b812e8 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckContext.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckContext.cs @@ -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? } diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs index 7377de41f8..1b7868432c 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs @@ -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, IHealthCheckResolver { public HealthCheckResolver(IServiceContainer container, ILogger logger, Func> types) - : base(container, logger, types, ObjectLifetimeScope.HttpRequest) + : base(container, logger, types, ObjectLifetimeScope.Transient) // do NOT change .Transient, see CreateValues below { } protected override IEnumerable 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;