Fix the problem of cache `Assembly`.

pull/17872/head
maliming 1 year ago
parent 83f7930a89
commit 21ea3eeb7c

@ -4,17 +4,20 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.TextTemplating.Razor;
public class DefaultAbpCompiledViewProvider : IAbpCompiledViewProvider, ITransientDependency
{
private static readonly ConcurrentDictionary<string, Assembly> CachedAssembles = new ConcurrentDictionary<string, Assembly>();
private readonly static ConcurrentDictionary<string, Assembly> CachedAssembles = new ConcurrentDictionary<string, Assembly>();
private readonly static SemaphoreSlim SemaphoreSlim = new SemaphoreSlim(1, 1);
private readonly AbpCompiledViewProviderOptions _options;
private readonly AbpRazorTemplateCSharpCompiler _razorTemplateCSharpCompiler;
@ -50,7 +53,17 @@ public class DefaultAbpCompiledViewProvider : IAbpCompiledViewProvider, ITransie
throw new AbpException($"Razor template content of {templateDefinition.Name} is null!");
}
return CachedAssembles.GetOrAdd((templateDefinition.Name + templateContent).ToMd5(), await CreateAssembly(templateContent));
using (await SemaphoreSlim.LockAsync())
{
var cacheKey = (templateDefinition.Name + templateContent).ToMd5();
if (CachedAssembles.TryGetValue(cacheKey, out var cachedAssemble))
{
return cachedAssemble;
}
var assemble = await CreateAssembly(templateContent);
CachedAssembles.TryAdd(cacheKey, assemble);
return assemble;
}
}
protected virtual async Task<Stream> GetAssemblyStreamAsync(TemplateDefinition templateDefinition, string templateContent)

@ -18,18 +18,15 @@ public class RazorTemplateRenderingEngine : TemplateRenderingEngineBase, ITransi
public override string Name => EngineName;
protected readonly IServiceScopeFactory ServiceScopeFactory;
protected readonly IAbpCompiledViewProvider AbpCompiledViewProvider;
public RazorTemplateRenderingEngine(
IServiceScopeFactory serviceScopeFactory,
IAbpCompiledViewProvider abpCompiledViewProvider,
ITemplateDefinitionManager templateDefinitionManager,
ITemplateContentProvider templateContentProvider,
IStringLocalizerFactory stringLocalizerFactory)
: base(templateDefinitionManager, templateContentProvider, stringLocalizerFactory)
{
ServiceScopeFactory = serviceScopeFactory;
AbpCompiledViewProvider = abpCompiledViewProvider;
}
public override async Task<string> RenderAsync(
@ -115,12 +112,13 @@ public class RazorTemplateRenderingEngine : TemplateRenderingEngineBase, ITransi
Dictionary<string, object> globalContext,
object model = null)
{
var assembly = await AbpCompiledViewProvider.GetAssemblyAsync(templateDefinition);
var templateType = assembly.GetType(AbpRazorTemplateConsts.TypeName);
var template = (IRazorTemplatePage)Activator.CreateInstance(templateType);
using (var scope = ServiceScopeFactory.CreateScope())
{
var compiledViewProvider = scope.ServiceProvider.GetRequiredService<IAbpCompiledViewProvider>();
var assembly = await compiledViewProvider.GetAssemblyAsync(templateDefinition);
var templateType = assembly.GetType(AbpRazorTemplateConsts.TypeName);
var template = (IRazorTemplatePage)Activator.CreateInstance(templateType);
var modelType = templateType
.GetInterfaces()
.Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRazorTemplatePage<>))

Loading…
Cancel
Save