Antaris / RazorEngine

Open source templating engine based on Microsoft's Razor parsing engine
http://antaris.github.io/RazorEngine
Other
2.14k stars 577 forks source link

Is there no support for C#7 tuples? #458

Open togakangaroo opened 7 years ago

togakangaroo commented 7 years ago

I seem to be having trouble using tuples and some other structures in my templates

Is there a document somewhere on how to add assemblies, debug and/or what limitations I should expect from RazorEngine?

Result StackTrace:  
at RazorEngine.Compilation.DirectCompilerServiceBase.CompileTypeImpl(TypeContext context)
   at RazorEngine.Compilation.DirectCompilerServiceBase.CompileType_Windows(TypeContext context)
   at RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(TypeContext context)
   at RazorEngine.Templating.RazorEngineCore.CreateTemplateType(ITemplateSource razorTemplate, Type modelType)
   at RazorEngine.Templating.RazorEngineCore.Compile(ITemplateKey key, Type modelType)
   at RazorEngine.Templating.RazorEngineService.CompileAndCacheInternal(ITemplateKey key, Type modelType)
   at RazorEngine.Templating.RazorEngineService.GetCompiledTemplate(ITemplateKey key, Type modelType, Boolean compileOnCacheMiss)
   at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.DynamicWrapperService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
   at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
   at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String name, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String templateSource, String name, Type modelType, Object model, DynamicViewBag viewBag)
   at MyProject.Tests.TimeEntrySpecs.InvoiceGenerationTests.play_with_razor() in Y:\code\Surge\myProject\MyProject.Tests\TimeEntrySpecs\InvoiceGenerationTests.cs:line 23
Result Message: 
RazorEngine.Templating.TemplateCompilationException : Errors while compiling a Template.
Please try the following to solve the situation:
  * If the problem is about missing/invalid references or multiple defines either try to load 
    the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
    Currently all references have to be available as files!
  * If you get 'class' does not contain a definition for 'member': 
        try another modelType (for example 'null' to make the model dynamic).
        NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    Or try to use static instead of anonymous/dynamic types.
More details about the error:
 - error: (108, 8) The type 'System.ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
     - error: (108, 8) The type 'System.IEquatable`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
     - error: (108, 8) The type 'System.Collections.IStructuralEquatable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
     - error: (108, 8) The type 'System.Collections.IStructuralComparable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
     - error: (108, 8) The type 'System.IComparable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
     - error: (108, 8) The type 'System.IComparable`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
     - error: (116, 25) The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
     - error: (116, 33) 'System.ValueTuple<decimal,string>' does not contain a definition for 'Amount' and no extension method 'Amount' accepting a first argument of type 'System.ValueTuple<decimal,string>' could be found (are you missing a using directive or an assembly reference?)
     - error: (122, 16) 'System.ValueTuple<decimal,string>' does not contain a definition for 'Notes' and no extension method 'Notes' accepting a first argument of type 'System.ValueTuple<decimal,string>' could be found (are you missing a using directive or an assembly reference?)
     - error: (138, 59) 'System.ValueTuple<decimal,string>' does not contain a definition for 'Amount' and no extension method 'Amount' accepting a first argument of type 'System.ValueTuple<decimal,string>' could be found (are you missing a using directive or an assembly reference?)
Temporary files of the compilation can be found in (please delete the folder): C:\Users\gmaue\AppData\Local\Temp\RazorEngine_zds5ar2i.i5z
The template we tried to compile is: 
------------- START -----------
@using System.Globalization;
@model MyProject.Web.Controllers.InvoiceController.InvoiceModel

@{
  Layout = null;
  var culture = CultureInfo.CurrentCulture;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Invoice @Model.User.FirstName @Model.User.LastName @Model.StartDate.ToShortDateString() - @Model.EndDate.ToShortDateString()</title>
</head>
<body>
  <main>
    <header>
      <aside class="from-whom">
        <pre>@Model.FromHeader</pre>
      </aside>
      <aside class="to-whom">
        <pre>@Model.ToHeader</pre>
      </aside>
      <h1>
        <span class="user-name">@Model.User.FirstName @Model.User.LastName</span>
        <span class="date-range">
          <span class="start-date">@Model.StartDate.ToShortDateString()</span>
          <span class="end-date">@Model.EndDate.ToShortDateString()</span>
        </span>
      </h1>
    </header>
    <article>
      <dl class="lines">
      @foreach(var l in Model.Lines) {
        <dt class="currency">@l.Amount.ToString("C", culture)</dt>
        <dd>@l.Notes</dd>
      }
      </dl>
      <div class="total">
        <span class="currency">@Model.Lines.Select(l => l.Amount).Sum().ToString("C", culture)</span>
      </div>
    </article>
  </main>
</body>
</html>

------------- END -----------

The generated source code is: 
------------- START -----------
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CompiledRazorTemplates.Dynamic {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Globalization;

    [RazorEngine.Compilation.HasDynamicModelAttribute()]
    public class RazorEngine_7464fdb8b8634a68bf9f8309166927b6 : RazorEngine.Templating.TemplateBase<MyProject.Web.Controllers.InvoiceController.InvoiceModel> {

        public RazorEngine_7464fdb8b8634a68bf9f8309166927b6() {
        }

        public override void Execute() {

  Layout = null;
  var culture = CultureInfo.CurrentCulture;

WriteLiteral("\r\n\r\n<!DOCTYPE html>\r\n<html>\r\n<head>\r\n    <meta");

WriteLiteral(" name=\"viewport\"");

WriteLiteral(" content=\"width=device-width\"");

WriteLiteral(" />\r\n    <title>Invoice ");

              Write(Model.User.FirstName);

WriteLiteral(" ");

                                    Write(Model.User.LastName);

WriteLiteral(" ");

                                                         Write(Model.StartDate.ToShortDateString());

WriteLiteral(" - ");

                                                                                                Write(Model.EndDate.ToShortDateString());

WriteLiteral("</title>\r\n</head>\r\n<body>\r\n  <main>\r\n    <header>\r\n      <aside");

WriteLiteral(" class=\"from-whom\"");

WriteLiteral(">\r\n        <pre>");

        Write(Model.FromHeader);

WriteLiteral("</pre>\r\n      </aside>\r\n      <aside");

WriteLiteral(" class=\"to-whom\"");

WriteLiteral(">\r\n        <pre>");

        Write(Model.ToHeader);

WriteLiteral("</pre>\r\n      </aside>\r\n      <h1>\r\n        <span");

WriteLiteral(" class=\"user-name\"");

WriteLiteral(">");

                           Write(Model.User.FirstName);

WriteLiteral(" ");

                                                 Write(Model.User.LastName);

WriteLiteral("</span>\r\n        <span");

WriteLiteral(" class=\"date-range\"");

WriteLiteral(">\r\n          <span");

WriteLiteral(" class=\"start-date\"");

WriteLiteral(">");

                              Write(Model.StartDate.ToShortDateString());

WriteLiteral("</span>\r\n          <span");

WriteLiteral(" class=\"end-date\"");

WriteLiteral(">");

                            Write(Model.EndDate.ToShortDateString());

WriteLiteral("</span>\r\n        </span>\r\n      </h1>\r\n    </header>\r\n    <article>\r\n      <dl");

WriteLiteral(" class=\"lines\"");

WriteLiteral(">\r\n");

       foreach(var l in Model.Lines) {

WriteLiteral("        <dt");

WriteLiteral(" class=\"currency\"");

WriteLiteral(">");

                        Write(l.Amount.ToString("C", culture));

WriteLiteral("</dt>\r\n");

WriteLiteral("        <dd>");

       Write(l.Notes);

WriteLiteral("</dd>\r\n");

      }

WriteLiteral("      </dl>\r\n      <div");

WriteLiteral(" class=\"total\"");

WriteLiteral(">\r\n        <span");

WriteLiteral(" class=\"currency\"");

WriteLiteral(">");

                          Write(Model.Lines.Select(l => l.Amount).Sum().ToString("C", culture));

WriteLiteral("</span>\r\n      </div>\r\n    </article>\r\n  </main>\r\n</body>\r\n</html>\r\n");

        }
    }
}

------------- END -----------

List of loaded Assemblies:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
    Loaded Assembly: Y:\code\Surge\myProject\MyProject.Tests\bin\Debug\nunit.framework.dll
    Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
    Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll
    Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll
    Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll
    Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll
    Loaded Assembly: Y:\cod
togakangaroo commented 7 years ago

Ok, got the correct references added and still

     - error: (116, 33) 'System.ValueTuple<decimal,string>' does not contain a definition for 'Amount' and no extension method 'Amount' accepting a first argument of type 'System.ValueTuple<decimal,string>' could be found (are you missing a using directive or an assembly reference?)
     - error: (122, 16) 'System.ValueTuple<decimal,string>' does not contain a definition for 'Notes' and no extension method 'Notes' accepting a first argument of type 'System.ValueTuple<decimal,string>' could be found (are you missing a using directive or an assembly reference?)
     - error: (138, 59) 'System.ValueTuple<decimal,string>' does not contain a definition for 'Amount' and no extension method 'Amount' accepting a first argument of type 'System.ValueTuple<decimal,string>' could be found (are you missing a using directive or an assembly reference?)

So it seems like the answer is no? Should be noted somewhere

togakangaroo commented 7 years ago

Apparently all of razor doesn't work with tuples, but they're working on it