RenderTemplate() as part of API errors on subsequent calls
I'm going to be using Umbraco as the back end of an API that our main web app will be calling. Currently I want to return a few fields as well as the rendered html of a node using RenderTemplate().
The first request to the API works fine (ie after a rebuild), but subsequent calls to the API endpoint give me the following error:
<!-- Error rendering template with id 1195: 'System.ArgumentException: Value does not fall within the expected range. at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name) at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name) at System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress() at System.Web.HttpWorkerRequest.IsLocal() at System.Web.HttpRequestWrapper.get_IsLocal() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.RunPage() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) at Umbraco.Web.Mvc.UmbracoMvcHandler.ExecuteUmbracoRequest() at Umbraco.Web.Templates.TemplateRenderer.RenderUmbracoRequestToString(RequestContext requestContext) at Umbraco.Web.Templates.TemplateRenderer.ExecuteTemplateRendering(TextWriter sw, PublishedContentRequest contentRequest) at Umbraco.Web.Templates.TemplateRenderer.Render(StringWriter writer) at Umbraco.Web.UmbracoComponentRenderer.RenderTemplate(Int32 pageId, Nullable`1 altTemplateId)' -->
The issue is definitely with RenderTemplate(), and since the first request always works I imagine it's some sort of weird caching or locking issue. I can't seem to figure out the problem, does anyone have any ideas? I'm still fairly new to .NET, so don't rule out the obvious :P
Here's my ApiController
And the service
A point that may be important - this doesn't actually break the request. The API returns the appropriate JSON, but the "Html" field of the JSON is populated with that error.
Update - I discovered that it has something to do with injecting my UmbracoHelper. If I manually new up a new UmbracoHelper with the current UmbracoContext in the GetBlog() method it works fine.
I'm not sure why RenderTemplate() seems to be the only functionality (I've used) that takes issue with this. When injecting the UmbracoHelper I'm able to get other properties/values from the node, but RenderTemplate() seems to break after the initial request.
I doubt I'll get the injection to work, but if I do I'll update this thread. Maybe someone who has a better understanding of how UmbracoHelper works under the hood has a better understanding?
This can be marked as solved. Turns out it's something weird with my dependency injection. I was registering my UmbracoHelper with the default lifetime in Autofac, which turns out to be InstancePerDependency. I changed that to InstancePerRequest and it works fine.
I'm still not sure how it's working under the nood, nor how RenderTemplate() is the only thing that has problems, but hopefully this thread can save someone some time and pain in the future!
RenderTemplate() as part of API errors on subsequent calls
I'm going to be using Umbraco as the back end of an API that our main web app will be calling. Currently I want to return a few fields as well as the rendered html of a node using
RenderTemplate()
.The first request to the API works fine (ie after a rebuild), but subsequent calls to the API endpoint give me the following error:
The issue is definitely with
RenderTemplate()
, and since the first request always works I imagine it's some sort of weird caching or locking issue. I can't seem to figure out the problem, does anyone have any ideas? I'm still fairly new to .NET, so don't rule out the obvious :PHere's my ApiController
And the service
A point that may be important - this doesn't actually break the request. The API returns the appropriate JSON, but the "Html" field of the JSON is populated with that error.
Also I'm using Autofac for my dependencies.
Update - I discovered that it has something to do with injecting my
UmbracoHelper
. If I manually new up a newUmbracoHelper
with the current UmbracoContext in theGetBlog()
method it works fine.I'm not sure why
RenderTemplate()
seems to be the only functionality (I've used) that takes issue with this. When injecting the UmbracoHelper I'm able to get other properties/values from the node, butRenderTemplate()
seems to break after the initial request.I doubt I'll get the injection to work, but if I do I'll update this thread. Maybe someone who has a better understanding of how UmbracoHelper works under the hood has a better understanding?
This can be marked as solved. Turns out it's something weird with my dependency injection. I was registering my UmbracoHelper with the default lifetime in Autofac, which turns out to be
InstancePerDependency
. I changed that toInstancePerRequest
and it works fine.I'm still not sure how it's working under the nood, nor how
RenderTemplate()
is the only thing that has problems, but hopefully this thread can save someone some time and pain in the future!is working on a reply...