Skip to content

Commit

Permalink
Fix error with possible null TypeFinder in NopEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanovM committed Jul 3, 2017
1 parent 184c3d3 commit 2f6fd87
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/Libraries/Nop.Core/Infrastructure/NopEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public class NopEngine : IEngine

protected IServiceProvider GetServiceProvider()
{
var accessor = _serviceProvider.GetService<IHttpContextAccessor>();
var context = accessor.HttpContext;
return context != null ? context.RequestServices : _serviceProvider;
return _serviceProvider?.GetService<IHttpContextAccessor>()?.HttpContext?.RequestServices ?? _serviceProvider;
}

/// <summary>
Expand Down Expand Up @@ -79,7 +77,7 @@ protected virtual void RegisterDependencies(NopConfig nopConfiguration, IService

//create and sort instances of dependency registrars
var instances = dependencyRegistrars
//.Where(dependencyRegistrar => PluginManager.FindPlugin(dependencyRegistrar).Return(plugin => plugin.Installed, true)) //ignore not installed plugins
.Where(dependencyRegistrar => PluginManager.FindPlugin(dependencyRegistrar).Return(plugin => plugin.Installed, true)) //ignore not installed plugins
.Select(dependencyRegistrar => (IDependencyRegistrar)Activator.CreateInstance(dependencyRegistrar))
.OrderBy(dependencyRegistrar => dependencyRegistrar.Order);

Expand All @@ -100,8 +98,7 @@ protected virtual void AddAutoMapper(IServiceCollection services, ITypeFinder ty

//create and sort instances of mapper configurations
var instances = mapperConfigurations
.Where(mapperConfiguration => PluginManager.FindPlugin(mapperConfiguration)
.Return(plugin => plugin.Installed, true)) //ignore not installed plugins
.Where(mapperConfiguration => PluginManager.FindPlugin(mapperConfiguration).Return(plugin => plugin.Installed, true)) //ignore not installed plugins
.Select(mapperConfiguration => (IMapperProfile)Activator.CreateInstance(mapperConfiguration))
.OrderBy(mapperConfiguration => mapperConfiguration.Order);

Expand Down Expand Up @@ -140,6 +137,7 @@ public void Initialize(IServiceCollection services)
//initialize plugins
var mvcCoreBuilder = services.AddMvcCore();
PluginManager.Initialize(mvcCoreBuilder.PartManager);

//resolve assemblies here. otherwise, plugins can thrown exceptions when rendering views
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
Expand All @@ -151,10 +149,10 @@ private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs a
if (assembly != null)
return assembly;

//get assembly fron TypeFinder
var tf = Resolve<ITypeFinder>();
//get assembly from TypeFinder
var tf = Resolve<ITypeFinder>() ?? new WebAppTypeFinder();
assembly = tf.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name);
return assembly;
return assembly;
}

/// <summary>
Expand Down Expand Up @@ -221,8 +219,8 @@ public void ConfigureRequestPipeline(IApplicationBuilder application)
/// <typeparam name="T">Type of resolved service</typeparam>
/// <returns>Resolved service</returns>
public T Resolve<T>() where T : class
{
return (T)GetServiceProvider().GetRequiredService(typeof(T));
{
return (T)GetServiceProvider()?.GetRequiredService(typeof(T));
}

/// <summary>
Expand All @@ -232,7 +230,7 @@ public T Resolve<T>() where T : class
/// <returns>Resolved service</returns>
public object Resolve(Type type)
{
return GetServiceProvider().GetRequiredService(type);
return GetServiceProvider()?.GetRequiredService(type);
}

/// <summary>
Expand All @@ -242,7 +240,7 @@ public object Resolve(Type type)
/// <returns>Collection of resolved services</returns>
public IEnumerable<T> ResolveAll<T>()
{
return (IEnumerable<T>)GetServiceProvider().GetServices(typeof(T));
return (IEnumerable<T>)GetServiceProvider()?.GetServices(typeof(T));
}

/// <summary>
Expand Down

0 comments on commit 2f6fd87

Please sign in to comment.