0
votes

I am experiencing a very strange behavior when deploying the ASP.Net website to Azure. The Custom Filter behaves differently when deployed to Azure. The issue is that the JsonResult that is generated in the Custom Exception Filter is not serializing the JSON Object or something in Azure.

This is the result of the custom filter printed in Browser Console in Development:

Object {data: Object, status: 400, config: Object, statusText: "Bad Request"} I can see that the "data" is an object and has the following properties: data: Object details: " stack details ..." message: "Website is required!"

In Azure the same thing is looking as:

Object {data: "Bad Request", status: 400, config: Object, statusText: "Bad Request"} config: Object data: "Bad Request"

I can see that the data is only string!

My custom filter is as below:

public class CustomErrorFilter : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        //No further processing when the exception is handled 
        if (filterContext.ExceptionHandled)
        {
            return;
        }

        if (!(filterContext.Exception is BusinessException)) // Non business exceptions
        {
            //Logging the actual exception
            ElmahLogger.LogError(filterContext.Exception);
            System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);

            //Changing exception type to a generic exception
            filterContext.Exception = new SystemException("An error occurred and logged during processing of this application. We appologise for any in-convieneces.");

            HandleAjaxException(filterContext, HttpStatusCode.InternalServerError);

            filterContext.ExceptionHandled = true;
        }
        else
        {
            System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);

            HandleAjaxException(filterContext, HttpStatusCode.BadRequest);

            filterContext.ExceptionHandled = true;
        }

    }

    private static void HandleAjaxException(ExceptionContext filterContext, HttpStatusCode httpStatusCode)
    {

        filterContext.HttpContext.Response.StatusCode = (int)httpStatusCode;

**// Seems like this is not working in Azure somehow????**
        filterContext.Result = new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = new
            {
                message = filterContext.Exception.Message,
                details = filterContext.Exception.StackTrace
            }
        };

        System.Diagnostics.Trace.TraceError("HandleAjaxException" + filterContext.Exception.Message);

    }

}

Can anyone point out what is going on please?

1
try attached debug and see what is wrong and how data will flow differently compare to your local environment.Xiaomin Wu
Can you please elaborate on "try attached debug". Do you mean I can debug my azure websites locally? Thanks for reply and much appreciated.user1829319
Also, as you can see there is not much code there. I can see that my traces getting printed.user1829319
here is how you can do remote debugging on an Azure Web Site blogs.msdn.com/b/webdev/archive/2013/11/05/…Xiaomin Wu

1 Answers

0
votes

Ok, I finally figured it out. Needed to change the web.config file as IIS8 is not showing the detailed errors by default. This fixed it: adding <httpErrors existingResponse="PassThrough"/> line inside the <system.webServer>.

<configuration>
    <system.webServer>
        <httpErrors existingResponse="PassThrough"/>
    </system.webServer>
</configuration>

This will ensure upstream errors are rendered to the browser.