Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a request scope if it wasn't created #5770

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,86 @@ namespace DotNetNuke.Common.Extensions
/// <summary>Dependency injection extensions for HttpContext.</summary>
public static class HttpContextDependencyInjectionExtensions
{
/// <summary>Sets the service scope for the http context base.</summary>
/// <param name="httpContext">The http context base.</param>
/// <param name="scope">The service scope.</param>
public static void SetScope(this HttpContextBase httpContext, IServiceScope scope)
{
httpContext.Items[typeof(IServiceScope)] = scope;
}

/// <summary>Sets the service scope for the http context.</summary>
/// <param name="httpContext">The http context.</param>
/// <param name="scope">The service scope.</param>
public static void SetScope(this HttpContext httpContext, IServiceScope scope)
=> SetScope(new HttpContextWrapper(httpContext), scope);

/// <summary>Sets the service scope for the http context base.</summary>
/// <param name="httpContext">The http context base.</param>
/// <param name="scope">The service scope.</param>
public static void SetScope(this HttpContextBase httpContext, IServiceScope scope)
{
httpContext.Items[typeof(IServiceScope)] = scope;
if (!httpContext.Items.Contains(typeof(IServiceScope)))
{
httpContext.Items[typeof(IServiceScope)] = scope;
}
}

/// <summary>Clears the service scope for the http context.</summary>
/// <param name="httpContext">The http context on which to clear the service scope.</param>
public static void ClearScope(this HttpContext httpContext)
=> ClearScope(new HttpContextWrapper(httpContext));

/// <summary>Clears the service scope for the http context.</summary>
/// <param name="httpContext">The http context on which to clear the service scope.</param>
public static void ClearScope(this HttpContextBase httpContext)
{
httpContext.Items.Remove(typeof(IServiceScope));
}

/// <summary>Gets the http context service scope.</summary>
/// <param name="httpContext">The http context from which to get the scope from.</param>
/// <returns>A service scope.</returns>
public static IServiceScope GetScope(this HttpContext httpContext)
=> GetScope(new HttpContextWrapper(httpContext));

/// <summary>Gets the http context base service scope.</summary>
/// <param name="httpContext">The http context base from which to get the scope from.</param>
/// <returns>A service scope.</returns>
public static IServiceScope GetScope(this HttpContextBase httpContext)
{
return GetScope(httpContext.Items);
}
if (httpContext.Items.Contains(typeof(IServiceScope)))
{
return httpContext.Items[typeof(IServiceScope)] as IServiceScope;
}

/// <summary>Gets the http context service scope.</summary>
/// <param name="httpContext">The http context from which to get the scope from.</param>
/// <returns>A service scope.</returns>
public static IServiceScope GetScope(this HttpContext httpContext)
{
return GetScope(httpContext.Items);
}
var scopeLock = new object();
const string ScopeLockName = "GetScope_lock";
if (httpContext.Items.Contains(ScopeLockName))
{
// another thread is adding the scope, try again
return GetScope(httpContext);
}

/// <summary>Gets the service scope from a collection of context items.</summary>
/// <param name="contextItems">A dictionary of context items.</param>
/// <returns>The found service scope.</returns>
internal static IServiceScope GetScope(System.Collections.IDictionary contextItems)
{
if (!contextItems.Contains(typeof(IServiceScope)))
IServiceScope scope = null;
httpContext.Items.Add(ScopeLockName, scopeLock);
lock (httpContext.Items[ScopeLockName])
{
if (httpContext.Items[ScopeLockName] == scopeLock)
{
if (!httpContext.Items.Contains(typeof(IServiceScope)))
{
scope = Globals.DependencyProvider.CreateScope();
httpContext.Items[typeof(IServiceScope)] = scope;
}
}
}

if (scope is not null)
{
return null;
httpContext.AddOnRequestCompleted(DisposeScope);
return scope;
}

return contextItems[typeof(IServiceScope)] is IServiceScope scope ? scope : null;
return GetScope(httpContext);
}

private static void DisposeScope(HttpContextBase httpContext)
{
httpContext.GetScope()?.Dispose();
httpContext.ClearScope();
}
}
}
1 change: 1 addition & 0 deletions DNN Platform/Library/DotNetNuke.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@
<Compile Include="Services\Cryptography\ICryptographyProvider.cs" />
<Compile Include="Services\Authentication\UserAuthenticationInfo.cs" />
<Compile Include="Services\DependencyInjection\IScopeAccessor.cs" />
<Compile Include="Services\DependencyInjection\ScopeAccessor.cs" />
<Compile Include="Services\EventQueue\EventMessageProcessorBase.cs" />
<Compile Include="Services\Exceptions\ExceptionExtensions.cs" />
<Compile Include="Services\Exceptions\SearchIndexEmptyException.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,13 @@

namespace DotNetNuke.Services.DependencyInjection
{
using System;
using System.Collections;
using System.Web;

using DotNetNuke.Common.Extensions;
using Microsoft.Extensions.DependencyInjection;

/// <summary>A contract specifying the ability to access an <see cref="IServiceScope"/> instance.</summary>
public interface IScopeAccessor
{
{
/// <summary>Gets the scope.</summary>
/// <returns>The scope, or <see langword="null"/> if there is no scope to get.</returns>
IServiceScope GetScope();
}

public class ScopeAccessor : IScopeAccessor
{
private static Func<IDictionary> fallbackGetContextItems = () => HttpContext.Current?.Items;

private Func<IDictionary> getContextItems;

/// <summary>Initializes a new instance of the <see cref="ScopeAccessor"/> class.</summary>
public ScopeAccessor()
{
this.getContextItems = fallbackGetContextItems;
}

/// <inheritdoc/>
public IServiceScope GetScope()
{
return HttpContextDependencyInjectionExtensions.GetScope(this.getContextItems());
}

internal void SetContextItemsFunc(Func<IDictionary> getContextItems)
{
this.getContextItems = getContextItems ?? fallbackGetContextItems;
}
}
}
19 changes: 19 additions & 0 deletions DNN Platform/Library/Services/DependencyInjection/ScopeAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information

namespace DotNetNuke.Services.DependencyInjection;

using DotNetNuke.Common;
using DotNetNuke.Common.Extensions;
using Microsoft.Extensions.DependencyInjection;

/// <summary>An <see cref="IScopeAccessor"/> implementation using <see cref="HttpContextSource.Current"/>.</summary>
public class ScopeAccessor : IScopeAccessor
{
/// <inheritdoc/>
public IServiceScope GetScope()
{
return HttpContextSource.Current.GetScope();
}
}