Skip to content

Commit

Permalink
fix(OrchardCMS#16069): InvalidOperationException thrown in `QueryAl…
Browse files Browse the repository at this point in the history
…iasIndex` when `ISession` is still in "connecting" state
  • Loading branch information
ApacheTech committed May 18, 2024
1 parent 36a8446 commit e430f6b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static async Task<string> GetContentItemIdByAliasAsync(this IOrchardHelpe
}

var session = orchardHelper.HttpContext.RequestServices.GetService<ISession>();
var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndex(session, alias);
var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndexAsync(session, alias);

return aliasPartIndex?.ContentItemId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading;
using System.Threading.Tasks;
using OrchardCore.Alias.Indexes;
using OrchardCore.ContentManagement;
using YesSql;

namespace OrchardCore.Alias.Services
{
internal sealed class AliasPartContentHandleHelper
{
private static readonly SemaphoreSlim _yesSqlLock = new(1, 1);

#pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
internal static async Task<ContentItem> QueryAliasIndexAsync(ISession session, string alias)
{
// NOTE: YesSql/Dapper does not support parallel or concurrent requests.
// Doing so can cause an `InvalidOperationException`, with the connection stuck within a "connecting" state.
await _yesSqlLock.WaitAsync();
try
{
return await session.Query<ContentItem, AliasPartIndex>(x => x.Alias == alias.ToLowerInvariant()).FirstOrDefaultAsync();
}
finally
{
_yesSqlLock?.Release();
}
}
#pragma warning restore CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using OrchardCore.Alias.Indexes;
using OrchardCore.ContentManagement;
using YesSql;

Expand All @@ -23,19 +23,11 @@ public async Task<string> GetContentItemIdAsync(string handle)
{
handle = handle[6..];

var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndex(_session, handle);
var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndexAsync(_session, handle);
return aliasPartIndex?.ContentItemId;
}

return null;
}
}

internal sealed class AliasPartContentHandleHelper
{
#pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
internal static Task<ContentItem> QueryAliasIndex(ISession session, string alias) =>
session.Query<ContentItem, AliasPartIndex>(x => x.Alias == alias.ToLowerInvariant()).FirstOrDefaultAsync();
#pragma warning restore CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
}
}

0 comments on commit e430f6b

Please sign in to comment.