Fix threading issue

Work Item: 17154

--HG--
branch : 1.x
This commit is contained in:
Renaud Paquay
2011-01-05 17:28:48 -08:00
parent 68a2f6dc51
commit cd9445930c
2 changed files with 6 additions and 19 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using Autofac;
using Autofac.Core;
@@ -8,7 +7,7 @@ using Module = Autofac.Module;
namespace Orchard.Localization {
public class LocalizationModule : Module {
private readonly IDictionary<string, Localizer> _localizerCache;
private readonly ConcurrentDictionary<string, Localizer> _localizerCache;
public LocalizationModule() {
_localizerCache = new ConcurrentDictionary<string, Localizer>();
@@ -26,14 +25,8 @@ namespace Orchard.Localization {
var scope = registration.Activator.LimitType.FullName;
registration.Activated += (sender, e) => {
if (_localizerCache.ContainsKey(scope)) {
userProperty.SetValue(e.Instance, _localizerCache[scope], null);
}
else {
var localizer = LocalizationUtilities.Resolve(e.Context, scope);
_localizerCache.Add(scope, localizer);
var localizer = _localizerCache.GetOrAdd(scope, key => LocalizationUtilities.Resolve(e.Context, scope));
userProperty.SetValue(e.Instance, localizer, null);
}
};
}
}

View File

@@ -11,7 +11,7 @@ using Module = Autofac.Module;
namespace Orchard.Logging {
public class LoggingModule : Module {
private readonly IDictionary<string, ILogger> _loggerCache;
private readonly ConcurrentDictionary<string, ILogger> _loggerCache;
public LoggingModule() {
_loggerCache = new ConcurrentDictionary<string, ILogger>();
@@ -63,14 +63,8 @@ namespace Orchard.Logging {
yield return (ctx, instance) => {
string component = componentType.ToString();
if (_loggerCache.ContainsKey(component)) {
propertyInfo.SetValue(instance, _loggerCache[component], null);
}
else {
var propertyValue = ctx.Resolve<ILogger>(new TypedParameter(typeof(Type), componentType));
_loggerCache.Add(component, propertyValue);
propertyInfo.SetValue(instance, propertyValue, null);
}
var logger = _loggerCache.GetOrAdd(component, key => ctx.Resolve<ILogger>(new TypedParameter(typeof(Type), componentType)));
propertyInfo.SetValue(instance, logger, null);
};
}
}