Changed dependency resolution.

This ensures dependency resolution works even when there's no work context (which happens when the code executes at the EndRequest event for example).
This commit is contained in:
Sipke Schoorstra
2015-08-10 18:06:57 +01:00
parent 01de9ef646
commit 0a9b05314e

View File

@@ -7,11 +7,11 @@ using Orchard.Logging;
namespace Orchard.Tasks.Locking {
public class DistributedLockService : IDistributedLockService {
private readonly ReaderWriterLockSlim _rwl = new ReaderWriterLockSlim();
private readonly Work<IDistributedLock> _lock;
private readonly IWorkContextAccessor _wca;
private readonly IMachineNameProvider _machineNameProvider;
public DistributedLockService(Work<IDistributedLock> @lock, IMachineNameProvider machineNameProvider) {
_lock = @lock;
public DistributedLockService(IWorkContextAccessor wca, IMachineNameProvider machineNameProvider) {
_wca = wca;
_machineNameProvider = machineNameProvider;
Logger = NullLogger.Instance;
}
@@ -20,7 +20,7 @@ namespace Orchard.Tasks.Locking {
public bool TryAcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout, out IDistributedLock @lock) {
var machineName = _machineNameProvider.GetMachineName();
@lock = _lock.Value;
@lock = Resolve<IDistributedLock>();
if (_rwl.TryEnterWriteLock(0)) {
try {
@@ -52,6 +52,11 @@ namespace Orchard.Tasks.Locking {
return false;
}
private T Resolve<T>() {
var workContext = _wca.GetContext() ?? _wca.CreateWorkContextScope().WorkContext; // In case this is invoked at the end of the request.
return workContext.Resolve<T>();
}
public IDistributedLock AcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout) {
IDistributedLock lockResult;
return TryAcquireLock(name, maxLifetime, timeout, out lockResult) ? lockResult : null;