Error 500 Web API

16,161

Solution 1

The Problem was with the Startup.cs class, Adding a default MapHttpRoute to the Configuration Method is what got it working

[assembly: OwinStartup(typeof(Startup))]
namespace API.MyApi
{

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var webApiConfiguration = new HttpConfiguration();

            webApiConfiguration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );

            app.UseNinjectMiddleware(CreateKernel);
            app.UseNinjectWebApi(webApiConfiguration);
        }

        private static StandardKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());

            var modules = new List<INinjectModule>
            {
                new BusinessBindings(),
                new DataBindings()
            };
            kernel.Load(modules);
            return kernel;
        }

    }
}

Solution 2

I would say its a routing/namespace issue. I would change the name of the controller and namespaces that you are using. Change API namespace and controller name. My guess is that somewhere it is causing a conflict which leads to the 500 error.

I was having the same issue recently. It drove me crazy, what caused the error in my case is that I had a controller named Reports that was causing a routing issue/conflict with SSRS. Just like your application, it works in development because the applications resources are never suspended where as in deployment, depending on settings whether it be IIS or Azure, application resources are suspended after a certain amount of time. The error occured on startup for some reason when the application would try to recover after being in a suspended state.

Share:
16,161
Dawood Awan
Author by

Dawood Awan

Updated on June 04, 2022

Comments

  • Dawood Awan
    Dawood Awan almost 2 years

    I've been trying to fix this for the last 4 hours.

    I have a new Web API Project - which works 100% fine on development,

    BUT on the live server I get a 500 Internal Server Error.

    When I deploy a new version and send a request directly to http://URL/Action I get the error. BUT If I go to http://URL/ first then send the POST request to http://URL/Action, it works.

    The same is the case when there is no request to the API for 12,13 hours

    So for it to work first I have to open: http://URL/Action, then send the POST request.

    So the Project uses Ninject and this is the Startup.cs

    using System.Collections.Generic;
    using System.Reflection;
    using System.Web.Http;
    using API.MyApi;
    using Business.Bindings;
    using Microsoft.Owin;
    using Ninject;
    using Ninject.Modules;
    using Ninject.Web.Common.OwinHost;
    using Ninject.Web.WebApi.OwinHost;
    using Owin;
    
    [assembly: OwinStartup(typeof(Startup))]
    namespace API.MyApi
    {
    
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                var config = new HttpConfiguration();
                WebApiConfig.Register(config);
                app.UseWebApi(config);
    
                app.UseNinjectMiddleware(CreateKernel);
                app.UseNinjectWebApi(config);
    
                GlobalConfiguration.Configuration.IncludeErrorDetailPolicy =  IncludeErrorDetailPolicy.Always;
            }
    
            private static StandardKernel CreateKernel()
            {
                var kernel = new StandardKernel();
                kernel.Load(Assembly.GetExecutingAssembly());
    
                var modules = new List<INinjectModule>
                {
                    new BusinessBindings(),
                    new DataBindings()
                };
                kernel.Load(modules);
                return kernel;
            }
    
        }
    }
    

    And the WebAPI Config

    using System.Web.Http;
    
    namespace API.MyApi
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
                config.Formatters.XmlFormatter.UseXmlSerializer = true;
                GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            }
        }
    }
    

    I have tried adding this line of code to get a view of the entire error message:

    GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    

    and also adding this to the web.config file

    <customErrors mode="Off"/>
    

    But the error I get in PostMan is:

    enter image description here

    After I navigate to URL.com in the Browser:

    enter image description here

    Then If I send the POST request:

    enter image description here

    EDIT:

    This is my exception handler attribute:

    public class ExceptionHandlerAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var exception = actionExecutedContext.Exception;
    
            using (var exceptionLogger = new CustomLogger("Exceptions"))
            {
                exceptionLogger.LogExceptionMessage(exception.Message, exception.InnerException, exception.StackTrace);
            }
    
            base.OnException(actionExecutedContext);
        }
    
    }
    

    and the Controller is decorated with the attribute:

    [ExceptionHandler]
    public class SmartOneCController : BaseApiController
    {
        public SmartOneCController(CustomLogger logger)
        {
        }
    
    }
    

    So what this Attribute should do is Log the exception to a log file - Which is working tested on both the Development and the deployed version.

    But is not logging the Exception from the 500 Internal Server Error

    Controller Code, I don't see how the controller code will help here, as everything works after visiting the Base URL of the API: URL.com after that I can call this POST method and everything works.

    [ExceptionHandler]
    public class SmartOneCController : BaseApiController
    {
        public SmartOneCController(CustomLogger logger, IAssetsProvider assetsProvider, ISatelliteTrackerProvider satelliteTrackerProvider) : base(logger, assetsProvider, satelliteTrackerProvider)
        {
        }
    
        public void Post(HttpRequestMessage request)
        {
            try
            {
    
                // Reading data as XML string to log to files - In case message structure is changed
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(request.Content.ReadAsStreamAsync().Result);
                var str = xmlDoc.InnerXml;
    
                _logger.LogMessage(MessageType.Information, string.Format("RAW XML Message: {0}", str));
    
                // Convert to model
    
                var model = XMLHelper.FromXml<trackermessages>(str);
     ......
    
  • Dawood Awan
    Dawood Awan about 8 years
    But it does work after I go to the home URL - after that everything works
  • Bogdan Daniel
    Bogdan Daniel over 4 years
    Have you figured out why the issue was happening? And what exactly is the fix? this is what got it working doesn't truly help anyone