WPFアプリからCSV出力、取り込み、参照用HTML生成などの処理にRazorEngineのテンプレート機能を使っています。
出力内容を別ファイルで管理できるので、とても便利です。
c#
1var template = Path.Combine("c:\aaaa\bbb\, "Template1.cshtml"); 2var templateHtmlText = File.ReadAllText(template); 3 4var htmlBody = Engine.Razor.RunCompile(templateHtmlText, "templateKey", typeof(Models.TestModel), ModelObj); 5
おおむね問題ありませんが、以下の例外が発生することがあります。
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: (0, 0) 型 'System.Attribute' が参照されていないアセンブリで定義されています。アセンブリ 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' への参照を追加してください。 Temporary files of the compilation can be found in (please delete the folder): C:\Users\kensa_byouri\AppData\Local\Temp\RazorEngine_jdkwz34l.low
1度発生すると、何度呼び出してもこのエラーになります。
ですが、一度プログラムを終了して、再度呼び出してみるとエラーは起こらなくなります。
その後そのパソコンではほとんど発生しません。
新しいパソコンで初めて実行する際に起こりやすいような気がしています。
参照されていないのなら毎回エラーになるはずですが、たまになるのはなぜ?
タイミングが分からず対応の糸口が分かっていません。
原因や解決策など分かる方おりませんでしょうか。
※この処理では、プログラム実行後初回呼出時に毎回コンパイルが行われ、TEMPフォルダ内に「RazorEngine_xxxxxx」の名前でフォルダが作成されます。
起動後の初回が毎回遅いのですが、このコンパイルしたDLL?を参照して毎回コンパイルせずにすればよいと思うのですが、そんなことは可能でしょうか。
(cshtmlが更新されていなければ、コンパイルせずにコンパイル済みdllのテンプレートを使用する)
そうすれば、コンパイルエラーもほぼ起こらないと思うのですが。。。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/29 05:27
2018/03/29 16:42
2018/04/10 10:41