Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

NullReferenceException that only happens when glimpse is on #1013

Open
Jojoshua opened this issue Apr 10, 2018 · 0 comments
Open

NullReferenceException that only happens when glimpse is on #1013

Jojoshua opened this issue Apr 10, 2018 · 0 comments

Comments

@Jojoshua
Copy link

I have some background jobs in Hangfire which build URLs. There is no HttpContext in this case. I installed Glimpse and let it be On and I am getting the following exception building these. The error does not occur when Glimpse is Off.

Here is what I have installed. The Asp.NET MVC5 project is targeting .NET 4.7.1 framework.




System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Glimpse.AspNet
StackTrace:
at Glimpse.AspNet.AspNetFrameworkProvider.get_HttpRequestStore()
at Glimpse.Core.Framework.Factory.<>c__DisplayClass5.b__4()
at Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult)
at Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.RouteProxy.GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
at System.Web.Mvc.RouteCollectionExtensions.GetVirtualPathForArea(RouteCollection routes, RequestContext requestContext, String name, RouteValueDictionary values, Boolean& usingAreas)
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues)

Here is my code. The error is thrown on the last line of GetFullUrlToAction

public static string GetFullUrlToAction(string actionName, string controllerName, object routeValues)
{
UrlHelper helper = getUrlHelper();
return string.Format("{0}://{1}/", helper.RequestContext.HttpContext.Request.Url.Scheme, helper.RequestContext.HttpContext.Request.Url.Authority).TrimEnd('/') + helper.Action(actionName, controllerName, routeValues); ---<Error here
}

public static UrlHelper getUrlHelper()
{
UrlHelper helper = null;

        if (HttpContext.Current != null)
        {
            helper = new UrlHelper(HttpContext.Current.Request.RequestContext);
        }
        else
        {
            //Background job will not have HttpContext

            HttpContext httpContext = HttpContext.Current;
            if (httpContext == null)
            {
                httpContext = getBackgroundHttpContext();
            }
            HttpContextWrapper httpContextBase = new HttpContextWrapper(httpContext);
            RouteData routeData = new RouteData();
            var myHostContext = new RequestContext(httpContextBase, routeData);
            helper = new UrlHelper(myHostContext);
        }

        return helper;
    }

public static HttpContext getBackgroundHttpContext()
    {
        HttpRequest request = new HttpRequest("/", "https://" + GetFQDN(), "");
        HttpResponse response = new HttpResponse(new StringWriter());
        return new HttpContext(request, response);
    }

public static string GetFQDN()
{
string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();

        domainName = "." + domainName;
        if (!hostName.EndsWith(domainName))  // if hostname does not already include domain name
        {
            hostName += domainName;   // add the domain name part
        }

        return hostName;                    // return the fully qualified name
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant