/// <summary>
        ///     Initialises the configuration.
        /// </summary>
        /// <param name="config">The core configuration.</param>
        /// <param name="serviceConfig">The service configuration.</param>
        private void InitialiseConfiguration(RazorEngineConfigurationSection config, TemplateServiceConfigurationElement serviceConfig)
        {
            // Add the global namespaces.
            AddNamespaces(config.Namespaces);

            // Add the specific namespaces.
            AddNamespaces(serviceConfig.Namespaces);

            // Sets the activator.
            SetActivator(config.ActivatorType);

            // Sets the base template type.
            SetBaseTemplateType(serviceConfig.BaseTemplateType);

            // Sets the compiler service factory.
            SetCompilerServiceFactory(config.CompilerServiceFactoryType);

            Debug = serviceConfig.Debug;

            // Sets the encoded string factory.
            SetEncodedStringFactory(serviceConfig.EncodedStringFactoryType);

            Language = serviceConfig.Language;

            // Sets the tempalte resolver.
            SetTemplateResolver(config.TemplateResolverType);
        }
        /// <summary>
        /// Creates an instance of a <see cref="TemplateService"/>.
        /// </summary>
        /// <param name="configuration">The named configuration.</param>
        /// <returns>A new instance of <see cref="TemplateService"/>.</returns>
        public static TemplateService CreateTemplateService(string configuration)
        {
            if (string.IsNullOrEmpty(configuration))
            {
                throw new ArgumentException("The argument 'configuration' cannot be null or empty.");
            }

            var config = RazorEngineConfigurationSection.GetConfiguration();

            if (config == null)
            {
                throw new ConfigurationErrorsException("The configuration section <razorEngine /> is not defined.");
            }

            var templateServiceConfig = config.TemplateServices
                                        .Cast <TemplateServiceConfigurationElement>()
                                        .Where(t => t.Name.Equals(configuration, StringComparison.InvariantCultureIgnoreCase))
                                        .SingleOrDefault();

            if (templateServiceConfig == null)
            {
                throw new ConfigurationErrorsException(string.Format("No configuration for template service '{0}' is defined", configuration));
            }

            return(CreateTemplateService(templateServiceConfig, config.Namespaces.Cast <NamespaceConfigurationElement>().Select(n => n.Namespace)));
        }
        public EmailTemplateConfiguration(string baseDir)
        {
            string configFile = Path.Combine(baseDir, "razor.config");

            RazorEngineConfigurationSection config = null;

            if (File.Exists(configFile))
            {
                using (var stream = File.OpenRead(configFile))
                {
                    var doc = XDocument.Load(stream);

                    var xElement = doc.Element("razorEngine");
                    if (xElement == null)
                    {
                        throw new ApplicationException("There should be a root element called razorEngine");
                    }
                    config = GetSection <RazorEngineConfigurationSection>(xElement.ToString());
                }
            }
            else
            {
                Trace.WriteLine(String.Format("Razor email configuration file not found at: {0}. using defaults", configFile));
            }

            innerConfig.BaseTemplateType = innerConfig.BaseTemplateType ?? typeof(RazorEmail.Templating.TemplateBase);
            innerConfig.Language         = (config == null)
                                       ? Language.CSharp
                                       : config.DefaultLanguage;
        }
예제 #4
0
        /// <summary>
        /// Configures the engine.
        /// </summary>
        private static void Configure()
        {
            var config = RazorEngineConfigurationSection.GetConfiguration();

            if (config == null)
            {
                return;
            }

            defaultNamespaces = config.Namespaces
                                .Cast <NamespaceConfigurationElement>()
                                .Select(n => n.Namespace);

            config.TemplateServices
            .Cast <TemplateServiceConfigurationElement>()
            .ToList()
            .ForEach(t => Services.Add(t.Name, TemplateServiceFactory.CreateTemplateService(t, defaultNamespaces)));

            if (!string.IsNullOrEmpty(config.TemplateServices.Default))
            {
                TemplateService templateService = null;
                if (!Services.TryGetValue(config.TemplateServices.Default, out templateService))
                {
                    throw new ConfigurationErrorsException(
                              string.Format(
                                  "No template service is configured with name '{0}' and could not be set as default.",
                                  config.TemplateServices.Default));
                }

                service        = templateService;
                createdService = true;
            }
        }
예제 #5
0
        /// <summary>
        /// Initialises the configuration.
        /// </summary>
        /// <param name="config">The core configuration.</param>
        /// <param name="serviceConfig">The service configuration.</param>
        private void InitialiseConfiguration(RazorEngineConfigurationSection config, TemplateServiceConfigurationElement serviceConfig)
        {
            // Set whether we are allowing missing properties on dynamic.
            AllowMissingPropertiesOnDynamic = config.AllowMissingPropertiesOnDynamic;

            // Set whether we load templates with Assembly.Load(byte[]).
            DisableTempFileLocking = config.DisableTempFileLocking;

            // Add the global namespaces.
            AddNamespaces(config.Namespaces);

            // Add the specific namespaces.
            AddNamespaces(serviceConfig.Namespaces);

            // Sets the activator.
            SetActivator(config.ActivatorType);

            // Sets the base template type.
            SetBaseTemplateType(serviceConfig.BaseTemplateType);

            // Sets the compiler service factory.
            SetCompilerServiceFactory(config.CompilerServiceFactoryType);

            Debug = serviceConfig.Debug;

            // Sets the encoded string factory.
            SetEncodedStringFactory(serviceConfig.EncodedStringFactoryType);

            // Sets the reference resolver.
            SetReferenceResolver(config.ReferenceResolverType);

#pragma warning disable 0618 // Backwards Compat.
            // Sets the template resolver.
            SetTemplateResolver(config.TemplateResolverType);

            if (Resolver != null)
            {
                TemplateManager = new WrapperTemplateManager(Resolver);
            }
#pragma warning restore 0618 // Backwards Compat.

            // Sets the template manager.
            SetTemplateManager(config.TemplateManagerType);

            // Set the language.
            Language = serviceConfig.Language;

            // Sets the activator.
            SetActivator(config.ActivatorType);

            // Sets the compiler service factory.
            SetCompilerServiceFactory(config.CompilerServiceFactoryType);

#pragma warning disable 0618 // Backwards Compat.
            // Sets the template resolver.
            SetTemplateResolver(config.TemplateResolverType);
#pragma warning restore 0618 // Backwards Compat.
        }
        public static ICompilerService GetDefaultCompilerService()
        {
            var config = RazorEngineConfigurationSection.GetConfiguration();

            if (config == null)
            {
                return(GetCompilerService(Language.CSharp));
            }

            return(GetCompilerService(config.DefaultLanguage));
        }
예제 #7
0
        /// <summary>
        /// Configures the templating engine.
        /// </summary>
        private static void Configure()
        {
            var config = RazorEngineConfigurationSection.GetConfiguration();

            if (config != null)
            {
                if (!string.IsNullOrWhiteSpace(config.Factory))
                {
                    SetCompilerServiceFactory(config.Factory);
                }
                else
                {
                    CompilerServiceFactory = new DefaultCompilerServiceFactory();
                }

                if (config.TemplateServices.Count > 0)
                {
                    string @default = string.IsNullOrWhiteSpace(config.TemplateServices.Default)
                                          ? null
                                          : config.TemplateServices.Default;

                    foreach (TemplateServiceConfigurationElement serviceConfig in config.TemplateServices)
                    {
                        string name    = serviceConfig.Name;
                        var    service = ConfigurationServices.CreateTemplateService(serviceConfig);;
                        ConfigurationServices.AddNamespaces(service, config.Namespaces);

                        if (name == @default)
                        {
                            DefaultTemplateService = service;
                        }

                        Services.Add(name, service);
                    }
                }

                if (DefaultTemplateService == null)
                {
                    DefaultTemplateService = new TemplateService(CompilerServiceFactory.CreateCompilerService());
                    ConfigurationServices.AddNamespaces(DefaultTemplateService, config.Namespaces);
                }

                if (!string.IsNullOrWhiteSpace(config.Activator))
                {
                    DefaultTemplateService.SetActivator(ConfigurationServices.CreateInstance <IActivator>(config.Activator));
                }
            }
            else
            {
                ConfigureDefault();
            }
        }
예제 #8
0
        public static ICompilerService GetDefaultCompilerService()
        {
#if NO_CONFIGURATION
            return(GetCompilerService(Language.CSharp));
#else
            var config = RazorEngineConfigurationSection.GetConfiguration();
            if (config == null)
            {
                return(GetCompilerService(Language.CSharp));
            }
            return(GetCompilerService(config.DefaultLanguage));
#endif
        }
        /// <summary>
        /// Initialises the configuration.
        /// </summary>
        /// <param name="config">The core configuration.</param>
        /// <param name="serviceConfig">The service configuration or null if there is no service configuration.</param>
        private void InitialiseConfiguration(RazorEngineConfigurationSection config, TemplateServiceConfigurationElement serviceConfig)
        {
            // Set whether we are allowing missing properties on dynamic.
            AllowMissingPropertiesOnDynamic = config.AllowMissingPropertiesOnDynamic;

            // Add the global namespaces.
            AddNamespaces(config.Namespaces);

            // Sets the activator.
            SetActivator(config.ActivatorType);

            // Sets the compiler service factory.
            SetCompilerServiceFactory(config.CompilerServiceFactoryType);

            // Sets the reference resolver.
            SetReferenceResolver(config.ReferenceResolverType);

            // Sets the template resolver.
            SetTemplateResolver(config.TemplateResolverType);

            if (Resolver != null)
            {
                TemplateManager = new WrapperTemplateManager(Resolver);
            }

            // Sets the template manager.
            SetTemplateManager(config.TemplateManagerType);


            // Set the language.
            Language = config.DefaultLanguage;

            if (serviceConfig != null)
            {
                // Add the specific namespaces.
                AddNamespaces(serviceConfig.Namespaces);

                // Sets the base template type.
                SetBaseTemplateType(serviceConfig.BaseTemplateType);

                Debug = serviceConfig.Debug;

                // Sets the encoded string factory.
                SetEncodedStringFactory(serviceConfig.EncodedStringFactoryType);

                // Override the default language.
                Language = serviceConfig.Language;
            }
        }
예제 #10
0
        /// <summary>
        /// Initialises the configuration.
        /// </summary>
        /// <param name="name">The name of the template service configuration.</param>
        private void InitialiseConfiguration(string name)
        {
            var config = RazorEngineConfigurationSection.GetConfiguration();

            if (config == null)
            {
                return;
            }

            var serviceConfig = (name == null) ? null : config.TemplateServices
                                .OfType <TemplateServiceConfigurationElement>()
                                .Where(t => t.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                                .SingleOrDefault();

            InitialiseConfiguration(config, serviceConfig);
        }
        /// <summary>
        ///     Initialises the configuration.
        /// </summary>
        /// <param name="name">The name of the template service configuration.</param>
        private void InitialiseConfiguration(string name)
        {
            var config = RazorEngineConfigurationSection.GetConfiguration();

            if (config == null)
            {
                throw new ConfigurationErrorsException("No <razorEngine> configuration section has been defined.");
            }

            var serviceConfig = config.TemplateServices
                                .OfType <TemplateServiceConfigurationElement>()
                                .Where(t => t.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                                .SingleOrDefault();

            if (serviceConfig == null)
            {
                throw new ConfigurationErrorsException("No <templateService> configuration element defined with name = '" + name + "'");
            }

            InitialiseConfiguration(config, serviceConfig);
        }
        public RazorViewServiceConfiguration(Registration <IRazorViewLoader> razorViewLoaderRegistration)
        {
            if (razorViewLoaderRegistration == null)
            {
                throw new ArgumentNullException(nameof(razorViewLoaderRegistration));
            }

            RazorViewLoader        = razorViewLoaderRegistration;
            CompilerServiceFactory = new DefaultCompilerServiceFactory();
            EncodedStringFactory   = new HtmlEncodedStringFactory();
            CachingProvider        = new DefaultCachingProvider();
            Namespaces             = new HashSet <string>()
            {
                "System",
                "System.Collections.Generic",
                "System.Linq"
            };
            RazorEngineConfigurationSection configuration = RazorEngineConfigurationSection.GetConfiguration();

            Language = configuration?.DefaultLanguage ?? Language.CSharp;
        }
예제 #13
0
 /// <summary>
 /// Initialises the <see cref="TemplateServiceFactory"/> type.
 /// </summary>
 static TemplateServiceFactory()
 {
     Configuration = RazorEngineConfigurationSection.GetConfiguration();
 }
예제 #14
0
 /// <summary>
 /// Initialises the <see cref="TemplateServiceFactory"/> type.
 /// </summary>
 static TemplateServiceFactory()
 {
     Configuration = RazorEngineConfigurationSection.GetConfiguration();
 }