.NET Web api 2在IIS中运行一段时间后停止工作
本文关键字:运行 一段时间 停止工作 IIS Web api NET | 更新日期: 2023-09-27 18:16:44
我在一个解决方案中有两个web API项目DLL。
我的项目解决方案结构:
在我的解决方案中,项目的位置如下:
1( 基本解决方案
2( 基础Web API
3( 模块Web API
因此,我的解决方案有点像包含许多模块的BASE解决方案。每个模块都可以包含自己的Web API。此外,我的基本解决方案包含自己的Web API
这就是我们的结构。
我的问题:
它在我的本地运行解决方案中运行良好。当我将它托管到IIS时,它会工作几个小时,然后通过抛出错误消息"找到错误404"而停止工作。当我尝试直接通过URL访问时,类似于"http://127.0.0.1:51/Products/Get",不工作。
Visual Studio版本:
Visual Studio Professional-2015
IIS版本:
IIS-7.5.7600
我的方法:我有一个简单的项目来模拟这个场景。它和我原来的项目有同样的问题。
基本模块的Web API:
App_Start:下的WepApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
基础API控制器:
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
WebApiConfig.cs用于模块Web API:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultModuleApi",
routeTemplate: "api/module/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
模块API控制器:
public class ModulesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
上述代码中的注释:
两个APIConfig文件之间的区别是:
对于模块代码:
routeTemplate: "api/module/{controller}/{action}/{id}"
对于基本代码:
routeTemplate: "api/{controller}/{action}/{id}"
全球.asax:
namespace BaseSln
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//GlobalConfiguration.Configure(WebApiConfig.Register);
var typesWithMyAttribute =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetLoadableTypes().Where(t1 => t1.Name == "WebApiConfig"
&& t1.GetMethod("Register") != null
&& t1.GetMethod("Register").IsStatic)
select new { Type = t, MethodInfo = t.GetMethod("Register") };
//register all the Routes
foreach (var type in typesWithMyAttribute)
{
var mi = type.MethodInfo;
Action<HttpConfiguration> action = null;
try
{
action = Delegate.CreateDelegate(typeof(Action<HttpConfiguration>), mi) as Action<HttpConfiguration>;
}
catch
{
//ignore the errors for now... should be handled somewhere else or logged.
}
if (action != null) GlobalConfiguration.Configure(action);
}
}
}
}
我在上面的项目中尝试了什么:
在IIS中托管后,我尝试访问如下路径:
对于基础API:
http://localhost:51600/api/Values/Get
退货:
value1
value2
对于模块API
http://localhost:51600/api/Modules/Get
退货:
value1
value2
我的问题:
过了一段时间,当我试图访问同一个链接时,我无法获得。上面写着
status 404. Not Found
我已经为这个问题工作了三天,但我无法解决这个问题。
我在stackoverflow上搜索了很多文章,但没有找到。
请帮我摆脱这一切。
谢谢。
如果您拥有Base Web API和Module Web API的所有路由,您是否可以检查Base解决方案中的GlobalConfiguration.Configuration.Routes
?