2016-03-31 404 views
1

我一直试图解决这个过去的4个小时。错误500 Web API

我有一个新的Web API项目 - 该工程对发展精细100%,

但活的服务器上,我收到了500 Internal Server Error

当我部署新版本并直接向http://URL/Action发送请求时,出现错误。 但是 如果我先去http://URL/然后发送POST请求到http://URL/Action,它的工作原理。

同样是这样,当存在于API没有请求12,13 hours

所以为它工作第一我都开:http://URL/Action,然后发送POST请求。

因此该项目使用Ninject,这是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; 
     } 

    } 
} 

而且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; 
     } 
    } 
} 

我曾尝试加入这行代码来获取整个错误消息的看法:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; 

并且还将其添加到第Ëweb.config文件

<customErrors mode="Off"/> 

但我在邮递员得到的错误是:

enter image description here

后,我浏览到URL.com在浏览器:

enter image description here

然后如果我发送POST请求:

enter image description here

编辑:

这是我的异常处理程序属性:

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); 
    } 

} 

和控制器装饰有属性:

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

} 

那么这个属性应该将异常记录到日志文件 - 正在测试在开发和部署版本上。

但没有记录从500 Internal Server Error

控制器代码例外,我没有看到控制器代码将如何帮助这里,因为一切都逛API的基础URL之后的工作原理:URL.com后,我可以调用这个POST方法,一切正常。

[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); 
...... 
+0

请发布记录的错误的适当的异常详细信息(名称,消息,堆栈跟踪)。没有这些信息,你的问题就模糊不清。 – Steven

+0

您可以添加全局ExceptionLogger来记录实时服务器上发生的实际异常吗?你可以找到有关如何在这里做的信息:http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling – Pedro

+0

@Steven这是我的问题之一我有我不能得到完整的异常 –

回答

0

问题出在Startup.cs类,这是什么让它工作

[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; 
     } 

    } 
} 
0

我会说它的路由/命名空间问题。我会改变你正在使用的控制器和命名空间的名称。更改API名称空间和控制器名称。我的猜测是,它在某个地方导致了导致500错误的冲突。

最近,我有同样的问题。它让我疯狂,在我的情况下导致错误的原因是我有一个名为Reports的控制器,导致与SSRS发生路由问题/冲突。就像您的应用程序一样,它可以在开发环境中工作,因为应用程序资源在部署时不会暂停,具体取决于设置是IIS还是Azure,应用程序资源会在一段时间后暂停。由于某种原因,应用程序在处于暂停状态后尝试恢复时,会在启动时发生错误。

+0

但是,当我进入家庭网址后,它确实有效 - 在此之后,一切正常 –

1

为了得到你可以在Visual Studio调试器附加到使用远程调试IIS进程之外。如果您没有对远程计算机的必需访问权限,则可能在开发工作站上运行IIS,并尝试在本地部署应用程序,而不是在VS的开发模式下运行。至于<customErrors>我已经有过更改这种情况的场合,因此如果可以,请尝试使用本地部署的浏览器。

为了排除路由,命名空间或控制器问题,您也可以尝试在没有外部依赖同一个控制器增加一个虚拟方法,看看是否调用该URL的工作方式。

至于您所遇到的问题的方式;这听起来像你通过http://URL/Action执行控制器行为可能有一定的相关性,目前尚未初始化,并通过访问http://URL/第一,你给应用程序时初始化该相关性。这种情况发生在一段时间后会显示它与应用程序是否已在IIS中运行有关,或者应用程序池已关闭它,并且应用程序需要在您点击时执行完整的启动http://URL/