4

I'm working on a web application where pages need to be populated from other projects. I was hoping to use a Razor Class Library (RCL) for that. The documentation states that a RCL can be referenced only as a project reference or a nuget package. I was hoping it would just be a shortcut to make sure developers went the recommended way. Unfortunately, if I directly reference the two generated assemblies from my RCL project (MyProject.dll and MyProject.Views.dll), it doesn't work. It works only if I add a project reference to the RCL project.

The company I work for has a custom build pipeline, and for reasons outside of my control I can only directly reference assemblies.

I've spent some time comparing the output of the compilation when using direct references and project references, but I couldn't find a difference. Does anybody know why direct references don't work for RCL? If I understand what the difference is, I should be able to work my way from there.

As a bonus, is somebody aware of a workaround? Creating a CompiledRazorAssemblyPart works (as described in Loading Razor Class Libraries as plugins), but I'd rather not have to manually search for the assemblies.

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94

1 Answers1

1

Put below code on Startup.cs

var assemblies = new string[] { "APP.Plugins.Test" };

        services
           .AddMvc()
           .ConfigureApplicationPartManager(apm =>
           {

               foreach (var assemblyFile in assemblies)
               {

                   //main assembly
                   var assembly = Assembly.Load(assemblyFile);
                   apm.ApplicationParts.Add(new AssemblyPart(assembly));

                   //view assembly
                   var assemblyView = Assembly.Load(assemblyFile+".Views");
                   apm.ApplicationParts.Add(new CompiledRazorAssemblyPart(assemblyView));                     
               }
           });
tobias
  • 1,502
  • 3
  • 22
  • 47
  • Microsoft typically has great documentation, but they are completely missing a section on dynamically loading RCL projects. No mention of CompiledRazorAssemblyPart. Thank you for this!!! – Daniel Oct 06 '21 at 18:43