Westwind.RazorHosting icon indicating copy to clipboard operation
Westwind.RazorHosting copied to clipboard

IndexOutOfRangeException throw from GetAssemblyFromStringAndCache()

Open kirajhpang opened this issue 6 years ago • 2 comments

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.

kirajhpang avatar Nov 12 '18 09:11 kirajhpang

Not sure what to make of this. If an assembly fails to load you would get a different exception. The error here looks like its failing on the actual add to the dictionary which is odd. Even if the value was null that should work so not sure what's going on there especially given the Index out of Range error.

RickStrahl avatar Dec 27 '18 21:12 RickStrahl

@RickStrahl ya, I still can't figure out where is the part possible have IndexOutOfRangeException

Following is the latest exception detail if anyone experiences some error with me under Azure Function, welcome to discuss further.

screenshot 2019-01-08 at 10 43 53 am

kirajhpang avatar Jan 08 '19 02:01 kirajhpang