Skip to content

IndexOutOfRangeException throw from GetAssemblyFromStringAndCache() #32

@kirajhpang

Description

@kirajhpang

Hi, I encounter an error that throw from GetAssemblyFromStringAndCache().

I have setup razorhost in this way.

public class RazorTemplateHelper
    {
        public static RazorStringHostContainer RazorHost { get; set; }

        public static string RenderTemplate(string template, object model/*, out string error*/)
        {
            if (RazorHost == null)
                StartRazorHost(); <-- exception throw from here

            //error = "";
            string result = RazorHost.RenderTemplate(template, model);
            if (result == null)
            {
                Debug.WriteLine(RazorHost.ErrorMessage);
                throw new System.Exception(RazorHost.ErrorMessage);
                //error = RazorHost.ErrorMessage;
            }

            return result;
        }

        public static void StartRazorHost()
        {
            var host = new RazorStringHostContainer()
            {
                //// *** Set your Folder Path here - physical or relative ***
                //TemplatePath = Path.GetFullPath(@".\templates\"),
                //// *** Path to the Assembly path of your application
                //BaseBinaryFolder = Environment.CurrentDirectory
            };

            // Add any assemblies that are referenced in your templates
            host.AddAssemblyFromType(typeof(Microsoft.WindowsAzure.Storage.Table.TableEntity));
           <all assembly>
            host.AddAssemblyFromType(typeof(Softinn.Razor.EmailAppSetting));

            // Always must start the host
            host.Start();

            RazorHost = host;
        }

        public static void StopRazorHost()
        {
            RazorHost?.Stop();
        }
    }
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: BookingNotification ---> System.IndexOutOfRangeException : Index was outside the bounds of the array.
   at System.Collections.Generic.Dictionary`2.Insert(TKey key,TValue value,Boolean add)
   at Westwind.RazorHosting.RazorStringHostContainer`1.GetAssemblyFromStringAndCache(String templateText)
   at Westwind.RazorHosting.RazorStringHostContainer`1.RenderTemplate(String templateText,Object model,TextWriter writer,Boolean inferModelType)
   at Softinn.Razor.RazorTemplateHelper.RenderTemplate(String template,Object model) at C:\<path to project>\RazorTemplateHelper.cs : 13
...

What I mess up from this configuration? I seem like Razorhost doesn't load assembly correctly and causing this unexpected error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions