public async Task Custom_TemplateReferences_Test()
        {
            var templateDefinition = _templateDefinitionManager.GetOrNull(RazorTestTemplates.TestTemplate);

            var assembly = await _compiledViewProvider.GetAssemblyAsync(templateDefinition);

            assembly.GetReferencedAssemblies().ShouldContain(x => x.Name == "Microsoft.Extensions.Logging.Abstractions");
        }
예제 #2
0
    protected virtual async Task <string> RenderTemplateContentWithRazorAsync(
        TemplateDefinition templateDefinition,
        string body,
        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 modelType = templateType
                            .GetInterfaces()
                            .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRazorTemplatePage <>))
                            .Select(x => x.GenericTypeArguments.FirstOrDefault())
                            .FirstOrDefault();

            if (modelType != null)
            {
                GetType().GetMethod(nameof(SetModel), BindingFlags.Instance | BindingFlags.NonPublic)
                ?.MakeGenericMethod(modelType).Invoke(this, new[] { template, model });
            }

            template.ServiceProvider   = scope.ServiceProvider;
            template.Localizer         = GetLocalizerOrNull(templateDefinition);
            template.HtmlEncoder       = scope.ServiceProvider.GetService <HtmlEncoder>();
            template.JavaScriptEncoder = scope.ServiceProvider.GetService <JavaScriptEncoder>();
            template.UrlEncoder        = scope.ServiceProvider.GetService <UrlEncoder>();
            template.Body          = body;
            template.GlobalContext = globalContext;

            await template.ExecuteAsync();

            return(await template.GetOutputAsync());
        }
    }