forked from Antaris/RazorEngine
/
IsolatedTemplateService.cs
482 lines (424 loc) · 22.9 KB
/
IsolatedTemplateService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
namespace RazorEngine.Templating
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Linq;
using Compilation;
using Text;
/// <summary>
/// Provides template parsing and compilation in an isolated application domain.
/// </summary>
public class IsolatedTemplateService : ITemplateService
{
#region Fields
private static readonly Type TemplateServiceType = typeof(TemplateService);
private readonly ITemplateService _proxy;
private readonly AppDomain _appDomain;
private bool disposed;
#endregion
#region Constructor
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>
/// </summary>
public IsolatedTemplateService()
: this(Language.CSharp, Encoding.Html, (IAppDomainFactory)null) { }
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>
/// </summary>
/// <param name="language">The code language.</param>
public IsolatedTemplateService(Language language)
: this(language, Encoding.Html, (IAppDomainFactory)null) { }
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>
/// </summary>
/// <param name="encoding">The encoding.</param>
public IsolatedTemplateService(Encoding encoding)
: this(Language.CSharp, encoding, (IAppDomainFactory)null) { }
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>
/// </summary>
/// <param name="appDomainFactory">The application domain factory.</param>
public IsolatedTemplateService(IAppDomainFactory appDomainFactory)
: this(Language.CSharp, Encoding.Html, appDomainFactory) { }
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>.
/// </summary>
/// <param name="appDomainFactory">The delegate used to create an application domain.</param>
public IsolatedTemplateService(Func<AppDomain> appDomainFactory)
: this(Language.CSharp, Encoding.Html, appDomainFactory) { }
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>
/// </summary>
/// <param name="language">The code language.</param>
/// <param name="encoding">The encoding.</param>
/// <param name="appDomainFactory">The application domain factory.</param>
public IsolatedTemplateService(Language language, Encoding encoding, IAppDomainFactory appDomainFactory)
{
_appDomain = CreateAppDomain(appDomainFactory ?? new DefaultAppDomainFactory());
string assemblyName = TemplateServiceType.Assembly.FullName;
string typeName = TemplateServiceType.FullName;
_proxy = (ITemplateService)_appDomain.CreateInstance(
assemblyName, typeName, false, BindingFlags.NonPublic | BindingFlags.Instance,
null, new object[] { language, encoding }, CultureInfo.CurrentCulture, null).Unwrap();
}
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>.
/// </summary>
/// <param name="language">The code language.</param>
/// <param name="appDomainFactory">The delegate used to create an application domain.</param>
public IsolatedTemplateService(Language language, Func<AppDomain> appDomainFactory)
: this(language, Encoding.Html, new DelegateAppDomainFactory(appDomainFactory)) { }
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>.
/// </summary>
/// <param name="language">The code language.</param>
/// <param name="encoding">The encoding.</param>
/// <param name="appDomainFactory">The delegate used to create an application domain.</param>
public IsolatedTemplateService(Language language, Encoding encoding, Func<AppDomain> appDomainFactory)
: this(language, encoding, new DelegateAppDomainFactory(appDomainFactory)) { }
/// <summary>
/// Initialises a new instance of <see cref="IsolatedTemplateService"/>.
/// </summary>
/// <param name="encoding">The encoding.</param>
/// <param name="appDomainFactory">The delegate used to create an application domain.</param>
public IsolatedTemplateService(Encoding encoding, Func<AppDomain> appDomainFactory)
: this(Language.CSharp, encoding, new DelegateAppDomainFactory(appDomainFactory)) { }
#endregion
#region Properties
/// <summary>
/// Gets the encoded string factory.
/// </summary>
IEncodedStringFactory ITemplateService.EncodedStringFactory { get { return null; } }
#endregion
#region Methods
/// <summary>
/// Adds a namespace that will be imported into the template.
/// </summary>
/// <param name="ns">The namespace to be imported.</param>
public void AddNamespace(string ns)
{
_proxy.AddNamespace(ns);
}
/// <summary>
/// Compiles the specified template.
/// </summary>
/// <param name="razorTemplate">The string template.</param>
/// <param name="modelType">The model type.</param>
/// <param name="cacheName">The name of the template type in the cache.</param>
public void Compile(string razorTemplate, Type modelType, string cacheName)
{
_proxy.Compile(razorTemplate, modelType, cacheName);
}
/// <summary>
/// Creates an application domain.
/// </summary>
/// <param name="factory">The application domain factory.</param>
/// <returns>An instance of <see cref="AppDomain"/>.</returns>
private static AppDomain CreateAppDomain(IAppDomainFactory factory)
{
var domain = factory.CreateAppDomain();
if (domain == null)
throw new InvalidOperationException("The application domain factory did not create an application domain.");
if (domain == AppDomain.CurrentDomain)
throw new InvalidOperationException("The application domain factory returned the current application domain which cannot be used for isolation.");
return domain;
}
/// <summary>
/// Creates an instance of <see cref="ITemplate{T}"/> from the specified string template.
/// </summary>
/// <param name="razorTemplate">
/// The string template.
/// If templateType is not NULL, this parameter may be NULL (unused).
/// </param>
/// <param name="templateType">
/// The template type or NULL if the template type should be dynamically created.
/// If razorTemplate is not NULL, this parameter may be NULL (unused).
/// </param>
/// <param name="model">The model instance or NULL if no model exists.</param>
/// <returns>An instance of <see cref="ITemplate{T}"/>.</returns>
public ITemplate CreateTemplate(string razorTemplate, Type templateType, object model)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if (model != null)
{
if (CompilerServicesUtility.IsDynamicType(model.GetType()))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
}
return _proxy.CreateTemplate(razorTemplate, templateType, model);
}
/// <summary>
/// Creates a set of templates from the specified string templates and models.
/// </summary>
/// <param name="razorTemplates">
/// The set of templates to create or NULL if all template types are already created (see templateTypes).
/// If this parameter is NULL, the the templateTypes parameter may not be NULL.
/// Individual elements in this set may be NULL if the corresponding templateTypes[i] is not NULL (precompiled template).
/// </param>
/// <param name="models">
/// The set of models or NULL if no models exist for all templates.
/// Individual elements in this set may be NULL if no model exists for a specific template.
/// </param>
/// <param name="templateTypes">
/// The set of template types or NULL to dynamically create template types for each template.
/// If this parameter is NULL, the the razorTemplates parameter may not be NULL.
/// Individual elements in this set may be NULL to dynamically create the template if the corresponding razorTemplates[i] is not NULL (dynamically compile template).
/// </param>
/// <param name="parallel">Flag to determine whether to create templates in parallel.</param>
/// <returns>The enumerable set of template instances.</returns>
public IEnumerable<ITemplate> CreateTemplates(IEnumerable<string> razorTemplates, IEnumerable<Type> templateTypes, IEnumerable<object> models, bool parallel = false)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if (models != null)
{
foreach (object model in models)
{
if (model != null)
{
if (CompilerServicesUtility.IsDynamicType(model.GetType()))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
}
}
}
return _proxy.CreateTemplates(razorTemplates, templateTypes, models, parallel);
}
/// <summary>
/// Creates a <see cref="Type"/> that can be used to instantiate an instance of a template.
/// </summary>
/// <param name="razorTemplate">The string template.</param>
/// <param name="modelType">The model type or NULL if no model exists.</param>
/// <returns>An instance of <see cref="Type"/>.</returns>
public Type CreateTemplateType(string razorTemplate, Type modelType)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if (CompilerServicesUtility.IsDynamicType(modelType))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
return _proxy.CreateTemplateType(razorTemplate, modelType);
}
/// <summary>
/// Creates a set of template types from the specfied string templates.
/// </summary>
/// <param name="razorTemplates">The set of templates to create <see cref="Type"/> instances for.</param>
/// <param name="modelTypes">
/// The set of model types or NULL if no models exist for all templates.
/// Individual elements in this set may be NULL if no model exists for a specific template.
/// </param>
/// <param name="parallel">Flag to determine whether to create template types in parallel.</param>
/// <returns>The set of <see cref="Type"/> instances.</returns>
public IEnumerable<Type> CreateTemplateTypes(IEnumerable<string> razorTemplates, IEnumerable<Type> modelTypes, bool parallel = false)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if (modelTypes != null)
{
foreach (Type modelType in modelTypes)
{
if ((modelType != null) && CompilerServicesUtility.IsDynamicType(modelType))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
}
}
return _proxy.CreateTemplateTypes(razorTemplates, modelTypes, parallel);
}
/// <summary>
/// Releases resources used by this instance.
/// </summary>
/// <remarks>
/// This method ensures the AppDomain is unloaded and any template assemblies are unloaded with it.
/// </remarks>
/// <param name="disposing">Flag to determine whether the instance is being disposed explicitly.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing && !disposed)
{
_proxy.Dispose();
AppDomain.Unload(_appDomain);
disposed = true;
}
}
/// <summary>
/// Releases resources used by this instance.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Gets an instance of the template using the cached compiled type, or compiles the template type
/// if it does not exist in the cache.
/// </summary>
/// <param name="razorTemplate">The string template.</param>
/// <param name="model">The model or NULL if there is no model for this template.</param>
/// <param name="cacheName">The name of the template type in the cache.</param>
/// <returns>An instance of <see cref="ITemplate"/>.</returns>
public ITemplate GetTemplate(string razorTemplate, object model, string cacheName)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
return _proxy.GetTemplate(razorTemplate, model, cacheName);
}
/// <summary>
/// Gets the set of template instances for the specified string templates. Cached templates will be considered
/// and if they do not exist, new types will be created and instantiated.
/// </summary>
/// <param name="razorTemplates">The set of templates to create.</param>
/// <param name="models">
/// The set of models or NULL if no models exist for all templates.
/// Individual elements in this set may be NULL if no model exists for a specific template.
/// </param>
/// <param name="cacheNames">The set of cache names.</param>
/// <param name="parallel">Flag to determine whether to get the templates in parallel.</param>
/// <returns>The set of <see cref="ITemplate"/> instances.</returns>
public IEnumerable<ITemplate> GetTemplates(IEnumerable<string> razorTemplates, IEnumerable<object> models, IEnumerable<string> cacheNames, bool parallel = false)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if (models != null)
{
foreach (object model in models)
{
if (model != null)
{
if (CompilerServicesUtility.IsDynamicType(model.GetType()))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
}
}
}
return _proxy.GetTemplates(razorTemplates, models, cacheNames, parallel);
}
/// <summary>
/// Returns whether or not a template by the specified name has been created already.
/// </summary>
/// <param name="cacheName">The name of the template type in cache.</param>
/// <returns>Whether or not the template has been created.</returns>
public bool HasTemplate(string cacheName)
{
return _proxy.HasTemplate(cacheName);
}
/// <summary>
/// Remove a template by the specified name from the cache.
/// </summary>
/// <param name="cacheName">The name of the template type in cache.</param>
/// <returns>Whether or not the template has been removed.</returns>
public bool RemoveTemplate(string cacheName)
{
return _proxy.RemoveTemplate(cacheName);
}
public bool LinkTemplate(string cacheName, string existingTemplateCacheName)
{
return _proxy.LinkTemplate(cacheName, existingTemplateCacheName);
}
/// <summary>
/// Parses and returns the result of the specified string template.
/// </summary>
/// <param name="razorTemplate">The string template.</param>
/// <param name="model">The model instance or NULL if no model exists.</param>
/// <param name="viewBag">The ViewBag contents or NULL for an initially empty ViewBag.</param>
/// <param name="cacheName">The name of the template type in the cache or NULL if no caching is desired.</param>
/// <returns>The string result of the template.</returns>
public string Parse(string razorTemplate, object model, DynamicViewBag viewBag, string cacheName)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if (model != null)
{
if (CompilerServicesUtility.IsDynamicType(model.GetType()))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
}
return _proxy.Parse(razorTemplate, model, viewBag, cacheName);
}
/// <summary>
/// Parses and returns the result of the specified string template.
/// </summary>
/// <param name="razorTemplate">The string template.</param>
/// <param name="model">The model instance or NULL if no model exists.</param>
/// <param name="viewBag">The ViewBag contents or NULL for an initially empty ViewBag.</param>
/// <param name="cacheName">The name of the template type in the cache or NULL if no caching is desired.</param>
/// <returns>The string result of the template.</returns>
public string Parse<T>(string razorTemplate, object model, DynamicViewBag viewBag, string cacheName)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if (model != null)
{
if (CompilerServicesUtility.IsDynamicType(model.GetType()))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
}
return _proxy.Parse<T>(razorTemplate, model, viewBag, cacheName);
}
/// <summary>
/// Parses the specified set of templates.
/// </summary>
/// <param name="razorTemplates">The set of string templates to parse.</param>
/// <param name="models">
/// The set of models or NULL if no models exist for all templates.
/// Individual elements in this set may be NULL if no model exists for a specific template.
/// </param>
/// <param name="viewBags">
/// The set of initial ViewBag contents or NULL for an initially empty ViewBag for all templates.
/// Individual elements in this set may be NULL if an initially empty ViewBag is desired for a specific template.
/// </param>
/// <param name="cacheNames">
/// The set of cache names or NULL if no caching is desired for all templates.
/// Individual elements in this set may be NULL if caching is not desired for specific template.
/// </param>
/// <param name="parallel">Flag to determine whether parsing in templates.</param>
/// <returns>The set of parsed template results.</returns>
public IEnumerable<string> ParseMany(IEnumerable<string> razorTemplates, IEnumerable<object> models, IEnumerable<DynamicViewBag> viewBags, IEnumerable<string> cacheNames, bool parallel)
{
if (disposed)
throw new ObjectDisposedException("IsolatedTemplateService");
if ((models != null) && (models.Count() != razorTemplates.Count()))
throw new ArgumentException("Expected same number of models contents as templates to be processed.");
if ((viewBags != null) && (viewBags.Count() != razorTemplates.Count()))
throw new ArgumentException("Expected same number of ViewBag contents as templates to be processed.");
if ((cacheNames != null) && (cacheNames.Count() != razorTemplates.Count()))
throw new ArgumentException("Expected same number of cache names as templates to be processed.");
if (models != null)
{
foreach (object model in models)
{
if ((model != null) && CompilerServicesUtility.IsDynamicType(model.GetType()))
throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
}
}
return _proxy.ParseMany(razorTemplates, models, viewBags, cacheNames, parallel).ToList();
}
/// <summary>
/// Resolves the template with the specified name.
/// </summary>
/// <param name="cacheName">The name of the template type in cache.</param>
/// <param name="model">The model or NULL if there is no model for the template.</param>
/// <returns>The resolved template.</returns>
public ITemplate Resolve(string cacheName, object model)
{
return _proxy.Resolve(cacheName, model);
}
/// <summary>
/// Runs the template with the specified cacheName.
/// </summary>
/// <param name="cacheName">The name of the template in cache. The template must be in cache.</param>
/// <param name="model">The model for the template or NULL if there is no model.</param>
/// <param name="viewBag">The initial ViewBag contents NULL for an empty ViewBag.</param>
/// <returns>The string result of the template.</returns>
public string Run(string cacheName, object model, DynamicViewBag viewBag)
{
return _proxy.Run(cacheName, model, viewBag);
}
/// <summary>
/// Runs the template with the specified name.
/// </summary>
/// <param name="template">The template to run.</param>
/// <param name="viewBag">The ViewBag contents or NULL for an initially empty ViewBag.</param>
/// <returns>The string result of the template.</returns>
public string Run(ITemplate template, DynamicViewBag viewBag)
{
return _proxy.Run(template, viewBag);
}
#endregion
}
}