繁体   English   中英

.NET Web api 2在IIS中停止工作

[英].NET Web api 2 stops working after sometime in IIS

我在一个解决方案中有两个Web API项目DLL

我的项目解决方案的结构:

在我的解决方案中,项目位于以下位置:

1)基础解决方案

2)Base Web API

3)模块Web API

因此,我的解决方案类似于包含许多模块的BASE解决方案。 每个模块都可以包含自己的Web API。 此外,我的Base Solution包含自己的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对于Module 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}"

Global.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中托管后,我尝试访问路径,如下所示:

对于Base API:

HTTP://本地主机:51600 / API /价值/ GET

返回:

value1
value2

对于模块API

HTTP://本地主机:51600 / API /模块/ GET

返回:

value1
value2

我的问题:

过了一段时间,当我尝试访问相同的链接时,我无法得到它。 它说

status 404. Not Found

我已经在这个问题上工作了3天,但我无法解决问题。

我在stackoverflow中搜索了很多文章,但没有运气。

请帮助我摆脱这一点。

谢谢。

如果您拥有Base Web API和Module Web API的所有路由,是否可以检查Base Solution中的GlobalConfiguration.Configuration.Routes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM