toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.51k stars 261 forks source link

Quickstart sample throws exception with 4.7.2 #366

Closed krmr closed 3 years ago

krmr commented 4 years ago

Describe the bug A basic sample following the Quickstart throws the following exception on Build(): System.TypeLoadException: Method 'GetItem' in type 'NullRazorProjectFileSystem' from assembly 'RazorLight, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation. thrown at RazorLight.DefaultRazorEngine.get_Instance()

To Reproduce Steps to reproduce the behavior:

Expected behavior The application runs without problems and prints "Hello, John Doe. Welcome to RazorLight repository" to the console.

Information (please complete the following information):

Additional context The best solution right now if of course not to update the NuGet packages but stick to the original versions. The downside is that depending on the context this could cause NuGet to show packages to consolidate.

So if the code does not work with the more recent versions I wonder if there is a way to restrict the versions e.g. setting a maximum version of <3.0 on the dependency in the package for .NET Standard

jzabroski commented 4 years ago

Can you share the csproj

On Fri, Aug 28, 2020 at 7:44 AM krmr notifications@github.com wrote:

Describe the bug A basic sample following the Quickstart throws the following exception on Build(): System.TypeLoadException: Method 'GetItem' in type 'NullRazorProjectFileSystem' from assembly 'RazorLight, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation. thrown at RazorLight.DefaultRazorEngine.get_Instance()

To Reproduce Steps to reproduce the behavior:

  • Create new project for console app using 4.7.2
  • Add code based on quickstart in main

namespace RazorLightTest { using System; using System.Globalization; using System.Threading.Tasks;

using RazorLight;

public class Program
{
    static async Task Main(string[] args)
    {
        CultureInfo.CurrentUICulture = new CultureInfo("en-us");

        var engine = new RazorLightEngineBuilder()
            .UseEmbeddedResourcesProject(typeof(Program))
            .UseMemoryCachingProvider()
            .Build();

        var template = "Hello, @Model.Name. Welcome to RazorLight repository";
        var model = new ViewModel { Name = "John Doe" };

        var result = await engine.CompileRenderStringAsync("templateKey", template, model);
        Console.WriteLine(result);
    }

    public class ViewModel
    {
        public string Name { get; set; }
    }
}

}

  • Add RazorLight via "Manage NuGet packages..."
  • Update all NuGet packages to latest version (make sure to uncheck "Include prelease")
  • Compile & Run

Expected behavior The application runs without problems and prints "Hello, John Doe. Welcome to RazorLight repository" to the console.

Information (please complete the following information):

  • Windows 10
  • .Net Framework 4.7.2
  • 2.0.0-beta9
  • package from NuGet
  • Visual Studio Enterprise 2019

Additional context The best solution right now if of course not to update the NuGet packages but stick to the original versions. The downside is that depending on the context this could cause NuGet to show packages to consolidate.

So if the code does not work with the more recent versions I wonder if there is a way to restrict the versions e.g. setting a maximum version of <3.0 on the dependency in the package for .NET Standard

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/toddams/RazorLight/issues/366, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADNH7JWTMFBUH6BZUN6H73SC6KCHANCNFSM4QOBJHMQ .

jzabroski commented 4 years ago

@krmr I tried to reproduce the issue and could not. I don't think the code by itself is enough to reproduce the issue.

  1. Went into Visual Studio 2019. Clicked Create New project.
  2. Selected Language: C#, Project Type: Console, Target: .NET Framework
  3. Named Project RazorLightTest
  4. Right-clicked on Solution, selected Manage Nuget packages
  5. Clicked Include Prelease, clicked on Browse tab, searched for RazorLight
  6. Installed RazorLight into the solution, which generated a packages.config file a mile long
  7. Copy-pasted your example code, (and added Console.Read() at the end so i can see the output)
  8. Got the expected hello world message
krmr commented 4 years ago

Thanks for trying, from you description i think the critical part might be missing: 6a. Still in Manage Nuget packages got to Updates, unselect Include prerelease, and update everything.

This pulls in version 3.17 for packages that seem to cause the issue. After manually downgrading the Razor packages to 2.20 the exception goes away.

It's just that on my first try I updated all packages as I always do and that resulted in the exception. It took me a while to understand what the problem was and how to resolve it.

krmr commented 4 years ago

For a full repro solution please see krmr/RazorLight366Repro

jzabroski commented 4 years ago

Got it. So the issue is we don't put version ranges on the RazorLight.csproj PackageReferences, to bound things. In particular, if using legacy .NET Framework, we shouldn't allow any netstandard2.1/netcoreapp3.0 dependencies, as those may contain Span and other data types that simply won't work on legacy .NET Framework. I had tested the install path, but did not think to test the upgrade path you mentioned.

That makes a ton of sense and would likely solve some other bugs. Would you be open to submitting a PR?

jzabroski commented 3 years ago

@krmr I reached out to someone who is interested in helping maintain the project if they'd like to push the packages to nuget.org. If I don't hear back from them, I'll update beta10 tomorrow. Thanks for your help.

timbze commented 3 years ago

Also see https://github.com/toddams/RazorLight/discussions/421#discussioncomment-957911

jzabroski commented 3 years ago

Yes, if people are using packages.config, they are likely to have issues consuming .NET Core libraries.