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

Alba + Razor #40

Open
joemcbride opened this issue Sep 13, 2017 · 0 comments
Open

Alba + Razor #40

joemcbride opened this issue Sep 13, 2017 · 0 comments

Comments

@joemcbride
Copy link
Contributor

joemcbride commented Sep 13, 2017

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);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant