Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Returning a RedirectResult with a URL causes NullReferenceException #3986

Closed
rob-hendrickx opened this issue Jan 21, 2019 · 8 comments
Closed
Assignees

Comments

@rob-hendrickx
Copy link

rob-hendrickx commented Jan 21, 2019

Investigative information

Please provide the following:

  • Function App version (1.0 or 2.0): 2.0

Repro steps

  1. Create a function app with a HTTP triggered function that returns a RedirectResult.
  2. Run it on runtime version 2.0.12275.0

Expected behavior

Receive an HTTP redirect.
(it still works like this on version 2.0.12246.0)

Actual behavior

Receive an HTTP InternalServerError caused by a NullReferenceException in the UrlHelperBase.
(issue on 2.0.12275.0)

Known workarounds

Return a custom ActionResult like this:

public class CustomRedirectResult : StatusCodeResult
    {
        public string Url { get; set; }

        public CustomRedirectResult(string url) : base(307)
        {
            Url = url;
        }

        public override void ExecuteResult(ActionContext context)
        {
            base.ExecuteResult(context);
            context.HttpContext.Response.Redirect(Url, false);
        }
    }

Related information

Exception details:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.AspNetCore.Mvc.Core
StackTrace:
at Microsoft.AspNetCore.Mvc.Routing.UrlHelperBase..ctor(ActionContext actionContext) in //src/Microsoft.AspNetCore.Mvc.Core/Routing/UrlHelperBase.cs:line 31
at Microsoft.AspNetCore.Mvc.Routing.UrlHelperFactory.GetUrlHelper(ActionContext context) in /
/src/Microsoft.AspNetCore.Mvc.Core/Routing/UrlHelperFactory.cs:line 64
at Microsoft.AspNetCore.Mvc.Infrastructure.RedirectResultExecutor.ExecuteAsync(ActionContext context, RedirectResult result) in //src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/RedirectResultExecutor.cs:line 48
at Microsoft.AspNetCore.Mvc.RedirectResult.ExecuteResultAsync(ActionContext context) in /
/src/Microsoft.AspNetCore.Mvc.Core/RedirectResult.cs:line 110
at Microsoft.Azure.WebJobs.Script.WebHost.Middleware.FunctionInvocationMiddleware.d__2.MoveNext() in C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\FunctionInvocationMiddleware.cs:line 70
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in E:\A_work\243\s\src\mscorlib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs:line 132

@jsmith50-sc
Copy link

Getting the same here as well after the 2.16 update possibly.

@trydis
Copy link

trydis commented Jan 22, 2019

We have a similar issue when returning a RedirectResult with a string, ends up with an internal server error. It works when pinning to 2.0.12246.0.

@tjgalama
Copy link

yep, also are running into this problem.

@tjgalama
Copy link

it looks like Type UrlHelperBase in Microsoft.AspNetCore.Mvc.Core,
Version=2.2.0.0
(previous runtime used 2.1.0.0) requires the ActionContext.RouteData to be set.
Since this property is null the NullReferenceException occurs.

This ActionContext instance is passed within the call-stack, but is created within method:
Microsoft.Azure.WebJobs.Script.WebHost.Middleware.FunctionInvocationMiddleware.Invoke(HttpContext context) within Microsoft.Azure.WebJobs.Script.WebHost.dll (File V. 2.0.12265.0). In this method the property RouteData is clearly not set.

For me adjusting the Function Runtime version is disturbing for our CI process.
Therefore, I added the following quick fix within my RedirectResult derived type;

public override Task ExecuteResultAsync(ActionContext context)
{
   ....
   if (context.RouteData == null)
      context.RouteData = new RouteData();
   return base.ExecuteResultAsync(context);
}

@rob-hendrickx
Copy link
Author

@tjgalama Indeed, we noticed that that was the origin of the exception aswell. We created a workaround by creating a derived type of StatusCodeResult and generating a Redirect there. But your workaround is cleaner IMO. Well it works, so we'll leave it as is.

@rob-hendrickx
Copy link
Author

Closed by accident

@brettsam
Copy link
Member

@mathewc / @fabiocav -- could one of you look at this? Seems to be hitting a few people. @tjgalama's investigation above indicates it's a breaking change in ASP.NET 2.2. Is there any way we can work around this?

@fabiocav fabiocav modified the milestones: Triaged, Functions Sprint 42 Jan 23, 2019
@pakrym
Copy link

pakrym commented Jan 24, 2019

cc @pranavkm

alrod added a commit to alrod/azure-webjobs-sdk-script that referenced this issue Jan 25, 2019
@alrod alrod closed this as completed in 986f6fe Jan 25, 2019
alrod added a commit to alrod/azure-webjobs-sdk-script that referenced this issue Jan 25, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 31, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants