Exemple #1
0
 public static IRazorLightEngine Create(RazorLightProject project)
 {
     return(new RazorLightEngineBuilder()
            .UseProject(project)
            .UseMemoryCachingProvider()
            .Build());
 }
 public RazorTemplateCompiler(
     RazorSourceGenerator sourceGenerator,
     ICompilationService compilationService,
     RazorLightProject razorLightProject,
     IOptions <RazorLightOptions> options) : this(sourceGenerator, compilationService, razorLightProject, options.Value)
 {
 }
Exemple #3
0
 public RazorRenderer(RazorLightProject project)
 {
     _engine = new RazorLightEngineBuilder()
               .UseProject(project)
               .UseMemoryCachingProvider()
               .Build();
 }
 public TestRazorTemplateCompiler(
     RazorSourceGenerator sourceGenerator,
     RoslynCompilationService roslynCompilationService,
     RazorLightProject razorLightProject,
     RazorLightOptions razorLightOptions) : base(sourceGenerator, roslynCompilationService, razorLightProject, razorLightOptions)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TemplateService"/> class.
 /// </summary>
 public TemplateService()
 {
     this.Project        = new TemplateCacheRazorLightProject();
     this.TemplateEngine = new RazorLightEngineBuilder()
                           .UseProject(this.Project)
                           .Build();
 }
        public RazorTemplateCompiler(
            RazorSourceGenerator sourceGenerator,
            RoslynCompilationService roslynCompilationService,
            RazorLightProject razorLightProject,
            RazorLightOptions razorLightOptions)
        {
            _razorSourceGenerator = sourceGenerator ?? throw new ArgumentNullException(nameof(sourceGenerator));
            _compiler             = roslynCompilationService ?? throw new ArgumentNullException(nameof(roslynCompilationService));
            _razorProject         = razorLightProject ?? throw new ArgumentNullException(nameof(razorLightProject));
            _razorLightOptions    = razorLightOptions ?? throw new ArgumentNullException(nameof(razorLightOptions));

            // This is our L0 cache, and is a durable store. Views migrate into the cache as they are requested
            // from either the set of known precompiled views, or by being compiled.
            var cacheOptions = Options.Create(new MemoryCacheOptions());

            _cache = new MemoryCache(cacheOptions);

            _normalizedKeysCache = new ConcurrentDictionary <string, string>(StringComparer.Ordinal);

            // We need to validate that the all of the precompiled views are unique by path (case-insensitive).
            // We do this because there's no good way to canonicalize paths on windows, and it will create
            // problems when deploying to linux. Rather than deal with these issues, we just don't support
            // views that differ only by case.
            _precompiledViews = new Dictionary <string, CompiledTemplateDescriptor>(
                5,                 //Change capacity when precompiled views are arrived
                StringComparer.OrdinalIgnoreCase);
        }
Exemple #7
0
        /// <summary>
        /// Configures RazorLight to use a project whose persistent store an assembly manifest resource stream.
        /// </summary>
        /// <param name="rootType">Any type in the root namespace (prefix) for your assembly manifest resource stream.</param>
        /// <returns><see cref="EmbeddedRazorProject"/></returns>
        public RazorLightEngineBuilder UseEmbeddedResourcesProject(Type rootType)
        {
            if (rootType == null)
            {
                throw new ArgumentNullException(nameof(rootType));
            }

            project = new EmbeddedRazorProject(rootType);

            return(this);
        }
        public virtual RazorLightEngineBuilder UseProject(RazorLightProject project)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            this.project = project;

            return(this);
        }
Exemple #9
0
        public RazorSourceGenerator(RazorEngine projectEngine, RazorLightProject project = null, ISet <string> namespaces = null)
        {
            if (projectEngine == null)
            {
                throw new ArgumentNullException(nameof(projectEngine));
            }

            Namespaces = namespaces ?? new HashSet <string>();

            ProjectEngine  = projectEngine;
            Project        = project;
            DefaultImports = GetDefaultImports();
        }
Exemple #10
0
        /// <summary>
        ///Creates RazorLightEngine with a custom RazorLightProject
        /// </summary>
        /// <param name="project">The project</param>
        /// <returns>Instance of RazorLightEngine</returns>
        public virtual RazorLightEngine Create(RazorLightProject project, RazorLightOptions options = null)
        {
            var razorOptions = options ?? new RazorLightOptions();

            var sourceGenerator = new RazorSourceGenerator(DefaultRazorEngine.Instance, project, razorOptions.Namespaces);

            var metadataReferenceManager = new DefaultMetadataReferenceManager(razorOptions.AdditionalMetadataReferences);
            var compiler = new RoslynCompilationService(metadataReferenceManager, Assembly.GetEntryAssembly());
            var templateFactoryProvider = new TemplateFactoryProvider(sourceGenerator, compiler, razorOptions);

            ICachingProvider cacheProvider = new MemoryCachingProvider();

            return(new RazorLightEngine(razorOptions, templateFactoryProvider, cacheProvider));
        }
Exemple #11
0
 /// <summary>
 /// Add razor renderer with a RazorLightProject to support views and layouts
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="razorLightProject"></param>
 /// <returns></returns>
 public static FluentEmailServicesBuilder AddRazorRenderer(this FluentEmailServicesBuilder builder, RazorLightProject razorLightProject)
 {
     builder.Services.AddRazorLight(() => RazorLightEngineFactory.Create(razorLightProject));
     builder.Services.AddSingleton <ITemplateRenderer, RazorRenderer>();
     return(builder);
 }
Exemple #12
0
        /// <summary>
        /// Configures RazorLight to use a project whose persistent store an assembly manifest resource stream.
        /// </summary>
        /// <param name="assembly">Assembly containing embedded resources</param>
        /// <param name="rootNamespace">The root namespace (prefix) for your assembly manifest resource stream.</param>
        /// <returns></returns>
        public RazorLightEngineBuilder UseEmbeddedResourcesProject(Assembly assembly, string rootNamespace = null)
        {
            project = new EmbeddedRazorProject(assembly, rootNamespace);

            return(this);
        }
        public RazorLightEngineBuilder UseFilesystemProject(string root)
        {
            project = new FileSystemRazorProject(root);

            return(this);
        }
            public static TestRazorTemplateCompiler Create(RazorLightOptions options = null, RazorLightProject project = null)
            {
                var razorOptions    = options ?? new RazorLightOptions();
                var metatadaManager = new DefaultMetadataReferenceManager();
                var assembly        = Assembly.GetCallingAssembly();
                var razorProject    = project ?? new EmbeddedRazorProject(assembly);
                var compilerService = new RoslynCompilationService(metatadaManager, assembly);
                var generator       = new RazorSourceGenerator(DefaultRazorEngine.Instance, razorProject);

                return(new TestRazorTemplateCompiler(generator, compilerService, razorProject, razorOptions));
            }
        public RazorLightEngineBuilder UseEmbeddedResourcesProject(Type rootType)
        {
            project = new EmbeddedRazorProject(rootType);

            return(this);
        }
 /// <summary>
 /// Add razor renderer with a RazorLightProject to support views and layouts
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="razorLightProject"></param>
 /// <returns></returns>
 public static FluentEmailServicesBuilder AddRazorRenderer(this FluentEmailServicesBuilder builder, RazorLightProject razorLightProject)
 {
     builder.Services.TryAdd(ServiceDescriptor.Singleton <ITemplateRenderer, RazorRenderer>(sp => new RazorRenderer(razorLightProject)));
     return(builder);
 }
Exemple #17
0
        /// <summary>
        /// Configures RazorLight to use a project whose persistent store is the file system.
        /// </summary>
        /// <param name="root">Directory path to the root folder containing your Razor markup files.</param>
        /// <param name="extension">If you wish, you can use a different extension than .cshtml.</param>
        /// <returns><see cref="RazorLightEngineBuilder"/></returns>
        public RazorLightEngineBuilder UseFileSystemProject(string root, string extension)
        {
            project = new FileSystemRazorProject(root, extension);

            return(this);
        }
Exemple #18
0
        /// <summary>
        /// Configures RazorLight to use a project whose persistent store is a "null device".
        /// </summary>
        public RazorLightEngineBuilder UseNoProject()
        {
            project = new NoRazorProject();

            return(this);
        }
Exemple #19
0
        /// <summary>
        /// Configures RazorLight to use a project.
        /// </summary>
        /// <remarks>
        /// Use this if implementing a custom <see cref="RazorLightProject"/>.
        /// </remarks>
        /// <param name="razorLightProject"></param>
        /// <returns></returns>
        public virtual RazorLightEngineBuilder UseProject(RazorLightProject razorLightProject)
        {
            project = razorLightProject ?? throw new ArgumentNullException(nameof(razorLightProject), $"Use {nameof(NoRazorProject)} instead of null.  See also {nameof(UseNoProject)}.");

            return(this);
        }
Exemple #20
0
        public virtual RazorLightEngine Build()
        {
            options = options ?? new RazorLightOptions();
            project = project ?? new NoRazorProject();

            if (namespaces != null)
            {
                if (namespaces.Count > 0 && options.Namespaces.Count > 0)
                {
                    ThrowIfHasBeenSetExplicitly(nameof(namespaces));
                }

                options.Namespaces = namespaces;
            }

            if (dynamicTemplates != null)
            {
                if (dynamicTemplates.Count > 0 && options.DynamicTemplates.Count > 0)
                {
                    ThrowIfHasBeenSetExplicitly(nameof(dynamicTemplates));
                }

                options.DynamicTemplates = dynamicTemplates;
            }

            if (metadataReferences != null)
            {
                if (metadataReferences.Count > 0 && options.AdditionalMetadataReferences.Count > 0)
                {
                    ThrowIfHasBeenSetExplicitly(nameof(metadataReferences));
                }

                options.AdditionalMetadataReferences = metadataReferences;
            }

            if (excludedAssemblies != null)
            {
                if (excludedAssemblies.Count > 0 && options.ExcludedAssemblies.Count > 0)
                {
                    ThrowIfHasBeenSetExplicitly(nameof(excludedAssemblies));
                }

                options.ExcludedAssemblies = excludedAssemblies;
            }

            if (prerenderCallbacks != null)
            {
                if (prerenderCallbacks.Count > 0 && options.PreRenderCallbacks.Count > 0)
                {
                    ThrowIfHasBeenSetExplicitly(nameof(prerenderCallbacks));
                }

                options.PreRenderCallbacks = prerenderCallbacks;
            }

            if (cachingProvider != null)
            {
                if (options.CachingProvider != null)
                {
                    ThrowIfHasBeenSetExplicitly(nameof(cachingProvider));
                }

                options.CachingProvider = cachingProvider;
            }

            if (disableEncoding.HasValue)
            {
                if (options.DisableEncoding != null)
                {
                    ThrowIfHasBeenSetExplicitly(nameof(disableEncoding));
                }

                options.DisableEncoding = options.DisableEncoding ?? disableEncoding ?? false;
            }
            else
            {
                if (!options.DisableEncoding.HasValue)
                {
                    options.DisableEncoding = false;
                }
            }

            if (enableDebugMode.HasValue && options.EnableDebugMode.HasValue)
            {
                ThrowIfHasBeenSetExplicitly(nameof(enableDebugMode));
            }
            else
            {
                options.EnableDebugMode = options.EnableDebugMode ?? enableDebugMode ?? false;
            }

            var metadataReferenceManager = new DefaultMetadataReferenceManager(options.AdditionalMetadataReferences, options.ExcludedAssemblies);
            var assembly = operatingAssembly ?? Assembly.GetEntryAssembly();
            var compiler = new RoslynCompilationService(metadataReferenceManager, assembly);

            var sourceGenerator         = new RazorSourceGenerator(DefaultRazorEngine.Instance, project, options.Namespaces);
            var templateCompiler        = new RazorTemplateCompiler(sourceGenerator, compiler, project, options);
            var templateFactoryProvider = new TemplateFactoryProvider();

            var engineHandler = new EngineHandler(options, templateCompiler, templateFactoryProvider, cachingProvider);

            return(new RazorLightEngine(engineHandler));
        }
 /// <summary>
 /// Creates RazorLightEngine with a custom RazorLightProject
 /// </summary>
 /// <param name="project">The project</param>
 /// <param name="options">Options for configuring the RazorLightEngine.</param>
 /// <returns>Instance of RazorLightEngine</returns>
 public virtual RazorLightEngine Create(RazorLightProject project, RazorLightOptions options = null)
 {
     return(null);
 }