title: Error 500 Web API
body: <p>I've been trying to fix this for the last 4 hours.</p>
  
  <p>I have a new Web API Project - which works 100% fine on development,</p>
  
  <p>BUT on the live server I get a <code>500 Internal Server Error</code>. </p>
  
  <p>When I deploy a new version and send a request directly to <code>http://URL/Action</code> I get the error. <strong>BUT</strong>
  If I go to <code>http://URL/</code> first then send the POST request to <code>http://URL/Action</code>, it works.</p>
  
  <p>The same is the case when there is no request to the API for <code>12,13 hours</code></p>
  
  <p>So for it to work first I have to open: <code>http://URL/Action</code>, then send the <code>POST</code> request.</p>
  
  <p>So the Project uses <code>Ninject</code> and this is the <code>Startup.cs</code></p>
  
  <pre><code>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&lt;INinjectModule&gt;
              {
                  new BusinessBindings(),
                  new DataBindings()
              };
              kernel.Load(modules);
              return kernel;
          }
  
      }
  }
  </code></pre>
  
  <p>And the <code>WebAPI Config</code></p>
  
  <pre><code>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;
          }
      }
  }
  </code></pre>
  
  <p>I have tried adding this line of code to get a view of the entire error message:</p>
  
  <pre><code>GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
  </code></pre>
  
  <p>and also adding this to the <code>web.config</code> file</p>
  
  <pre><code>&lt;customErrors mode="Off"/&gt;
  </code></pre>
  
  <p>But the error I get in PostMan is:</p>
  
  <p><a href="https://i.stack.imgur.com/w5ThZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w5ThZ.png" alt="enter image description here"></a></p>
  
  <p>After I navigate to URL.com in the Browser:</p>
  
  <p><a href="https://i.stack.imgur.com/vfn4o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vfn4o.png" alt="enter image description here"></a></p>
  
  <p>Then If I send the POST request:</p>
  
  <p><a href="https://i.stack.imgur.com/dKWOZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dKWOZ.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This is my exception handler attribute:</p>
  
  <pre><code>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);
      }
  
  }
  </code></pre>
  
  <p>and the Controller is decorated with the attribute:</p>
  
  <pre><code>[ExceptionHandler]
  public class SmartOneCController : BaseApiController
  {
      public SmartOneCController(CustomLogger logger)
      {
      }
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>But is not logging the Exception from the <code>500 Internal Server Error</code></p>
  
  <p>Controller Code, I don't see how the controller code will help here, as  everything works after visiting the Base URL of the API: <code>URL.com</code> after that I can call this POST method and everything works.</p>
  
  <pre><code>[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&lt;trackermessages&gt;(str);
   ......
  </code></pre>