Skip to content

Alba + Razor #40

@joemcbride

Description

@joemcbride

It appears that to be able to invoke an endpoint that returns a Razor view you have to manually tell Razor where the needed Assemblies are. Yes, I just threw up a little myself. The end user may need to manually add additional assemblies.

Maybe this is the start of #28 ?

Original problem/solution: aspnet/Hosting#954 (comment)

public class SystemTestBase
{
    protected Task<IScenarioResult> run(Action<Scenario> configuration, Action<SystemUnderTest> systemConfigure = null)
    {
        using (var system = SystemUnderTest.ForStartup<Startup>())
        {
            system.ConfigureRazor<Startup>();

            system.Environment.EnvironmentName = "Local";
            systemConfigure?.Invoke(system);
            return system.Scenario(configuration);
        }
    }
}

public static class SystemUnderTestExtensions
{
    public static void ConfigureRazor<T>(this SystemUnderTest system, params string[] additionalAssemblies)
    {
        system.ConfigureServices(c =>
        {
            c.Configure((RazorViewEngineOptions options) =>
            {
                var previous = options.CompilationCallback;
                options.CompilationCallback = (context) =>
                {
                    previous?.Invoke(context);
                    context.Compilation = context.Compilation.AddReferences(GetReferences<T>(additionalAssemblies));
                };
            });
        });
    }

    private static IEnumerable<MetadataReference> GetReferences<T>(IEnumerable<string> additionalAssemblies)
    {
        var assemblyNames = new List<string>
        {
            "mscorlib",
            "System.Dynamic.Runtime",
            "System.Private.CoreLib",
            "System.Runtime",
            "Microsoft.AspNetCore.Html.Abstractions",
            "Microsoft.AspNetCore.Razor"
        };

        var assembly = typeof(T).GetTypeInfo().Assembly;
        var assemblies = assembly.GetReferencedAssemblies().Select(x => MetadataReference.CreateFromFile(Assembly.Load(x).Location))
                .ToList();

            assemblies.AddRange(CreateReferences(additionalAssemblies.Concat(assemblyNames).ToArray()));

        return assemblies;
    }

    private static IEnumerable<PortableExecutableReference> CreateReferences(params string[] assemblies)
    {
        return assemblies.Select(x =>
        {
            try
            {
                return MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName(x)).Location);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }
        }).Where(x => x != null);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions