Вопрос

I'm trying to use async actions in MonoRail but when the view is rendered I get an NullReference exception, also tested with emtpy view file.

I also tried to call RenderView("uploadTags.vm") in EndUploadTags. When I call RenderText(s) in EndUploadTags I don't get the exception.

Stacktrace:

   [NullReferenceException: Object reference not set to an instance of an object.]
   Castle.MonoRail.Framework.Services.DefaultCacheProvider.Get(String key) +163
   Castle.MonoRail.Framework.Views.NVelocity.CustomResourceManager.GetResource(String resourceName, ResourceType resourceType, String encoding) +68
   NVelocity.Runtime.RuntimeInstance.GetTemplate(String name, String encoding) +57
   NVelocity.Runtime.RuntimeInstance.GetTemplate(String name) +82
   NVelocity.App.VelocityEngine.GetTemplate(String name) +47
   Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine.Process(String viewName, TextWriter output, IEngineContext context, IController controller, IControllerContext controllerContext) +564
   Castle.MonoRail.Framework.Services.DefaultViewEngineManager.Process(String templateName, TextWriter output, IEngineContext context, IController controller, IControllerContext controllerContext) +237
   Castle.MonoRail.Framework.Controller.ProcessView() +146
   Castle.MonoRail.Framework.Controller.EndProcess() +1579
   Castle.MonoRail.Framework.BaseAsyncHttpHandler.EndProcessRequest(IAsyncResult result) +141

[MonoRailException: Error processing MonoRail request. Action uploadtags on asyncController vendor]
   Castle.MonoRail.Framework.BaseAsyncHttpHandler.EndProcessRequest(IAsyncResult result) +461
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +86

This is my test code:

        private Output output;
        public delegate string Output();

        private string DoNothing()
        {
            return "nothing";
        }

        private string Upload()
        {
            return "upload";
        }

        public IAsyncResult BeginUploadTags(HttpPostedFile xmlFile, Boolean doUpload)
        {
            if (IsPost)
            {
                output = Upload;
                return output.BeginInvoke(ControllerContext.Async.Callback, null);
            }
            output = DoNothing;
            return output.BeginInvoke(ControllerContext.Async.Callback, null);
        }

        public void EndUploadTags()
        {
            var s = output.EndInvoke(ControllerContext.Async.Result);
            PropertyBag["logging"] = s;
        }
Это было полезно?

Решение

This is a bug in old versions of MonoRail. It works in MonoRail 2.1 RC, but not in an old version I just tried, I got the same null ref exception.

This is what revision 5688 looked like in Subversion, which is where the NullReferenceException is coming from. The code no longer uses the HttpContext for the cache.

public object Get(String key)
{
    if (logger.IsDebugEnabled)
    {
        logger.DebugFormat("Getting entry with key {0}", key);
    }

    return GetCurrentContext().Cache.Get(key);
}

private static HttpContext GetCurrentContext()
{
    return HttpContext.Current;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top