错误 500 网站 API
Error 500 Web API
过去 4 小时我一直在努力解决这个问题。
我有一个新的 Web API 项目 - 在开发中 100% 正常,
但是在实时服务器上我得到了 500 Internal Server Error
。
当我部署新版本并直接向 http://URL/Action
发送请求时,出现错误。 但是
如果我先转到 http://URL/
,然后将 POST 请求发送到 http://URL/Action
,就可以了。
12,13 hours
没有请求到API时也是如此
所以为了让它工作首先我必须打开: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"/>
但是我在 PostMan 中得到的错误是:
在浏览器中导航到 URL.com 后:
然后如果我发送 POST 请求:
编辑:
这是我的异常处理程序属性:
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);
......
我会说这是一个 routing/namespace 问题。我会更改您正在使用的控制器和名称空间的名称。更改 API 命名空间和控制器名称。我的猜测是它在某处引起了导致 500 错误的冲突。
我最近遇到了同样的问题。这让我发疯,在我的案例中导致错误的原因是我有一个名为 Reports 的控制器导致路由 issue/conflict 使用 SSRS。就像您的应用程序一样,它在开发中工作,因为应用程序资源永远不会在部署中暂停,具体取决于 IIS 或 Azure 的设置,应用程序资源会在一定时间后暂停。当应用程序在处于挂起状态后尝试恢复时,由于某种原因在启动时发生错误。
要获得异常,您可以使用远程调试器将 Visual Studio 调试器附加到 IIS 进程。如果您没有所需的远程计算机访问权限,可能 运行 IIS 在您的开发工作站上并尝试在本地部署应用程序,而不是 运行 在 VS 下以开发模式部署。至于 <customErrors>
我不止一次更改它没有效果,所以如果可以的话,请尝试使用部署的本地浏览器。
要排除路由、命名空间或控制器问题,您还可以尝试在同一个控制器中添加一个没有外部依赖项的虚拟方法,看看调用该方法是否 URL 的行为方式相同。
关于您遇到问题的方式;这听起来像是您通过 http://URL/Action possibly has some dependency that has not initialized yet, and by visiting http://URL/ first, you are giving the application time to initialize that dependency. The fact that this happens after a period of time would indicate that it's related to whether the application is already running in IIS, or the application pool has shut it down and the app needs to do a full start when you hit http://URL/
执行的控制器操作
问题出在 Startup.cs class,向配置方法添加默认值 MapHttpRoute
是它起作用的原因
[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;
}
}
}
过去 4 小时我一直在努力解决这个问题。
我有一个新的 Web API 项目 - 在开发中 100% 正常,
但是在实时服务器上我得到了 500 Internal Server Error
。
当我部署新版本并直接向 http://URL/Action
发送请求时,出现错误。 但是
如果我先转到 http://URL/
,然后将 POST 请求发送到 http://URL/Action
,就可以了。
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"/>
但是我在 PostMan 中得到的错误是:
在浏览器中导航到 URL.com 后:
然后如果我发送 POST 请求:
编辑:
这是我的异常处理程序属性:
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);
......
我会说这是一个 routing/namespace 问题。我会更改您正在使用的控制器和名称空间的名称。更改 API 命名空间和控制器名称。我的猜测是它在某处引起了导致 500 错误的冲突。
我最近遇到了同样的问题。这让我发疯,在我的案例中导致错误的原因是我有一个名为 Reports 的控制器导致路由 issue/conflict 使用 SSRS。就像您的应用程序一样,它在开发中工作,因为应用程序资源永远不会在部署中暂停,具体取决于 IIS 或 Azure 的设置,应用程序资源会在一定时间后暂停。当应用程序在处于挂起状态后尝试恢复时,由于某种原因在启动时发生错误。
要获得异常,您可以使用远程调试器将 Visual Studio 调试器附加到 IIS 进程。如果您没有所需的远程计算机访问权限,可能 运行 IIS 在您的开发工作站上并尝试在本地部署应用程序,而不是 运行 在 VS 下以开发模式部署。至于 <customErrors>
我不止一次更改它没有效果,所以如果可以的话,请尝试使用部署的本地浏览器。
要排除路由、命名空间或控制器问题,您还可以尝试在同一个控制器中添加一个没有外部依赖项的虚拟方法,看看调用该方法是否 URL 的行为方式相同。
关于您遇到问题的方式;这听起来像是您通过 http://URL/Action possibly has some dependency that has not initialized yet, and by visiting http://URL/ first, you are giving the application time to initialize that dependency. The fact that this happens after a period of time would indicate that it's related to whether the application is already running in IIS, or the application pool has shut it down and the app needs to do a full start when you hit http://URL/
执行的控制器操作问题出在 Startup.cs class,向配置方法添加默认值 MapHttpRoute
是它起作用的原因
[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;
}
}
}