简体   繁体   English

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

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

I have two web API project DLLs in one solution. 我在一个解决方案中有两个Web API项目DLL

Structure of my Project Solution: 我的项目解决方案的结构:

In my solution, the projects are located as follows: 在我的解决方案中,项目位于以下位置:

1) Base Solution 1)基础解决方案

2) Base Web API 2)Base Web API

3) Module Web API 3)模块Web API

Hence, my solution is something like a BASE solution which contains many MODULES. 因此,我的解决方案类似于包含许多模块的BASE解决方案。 Each Modules can contain its own Web APIs. 每个模块都可以包含自己的Web API。 Also, my Base Solution contains its own Web API 此外,我的Base Solution包含自己的Web API

This is our structure. 这是我们的结构。

My Problem: 我的问题:

It is working fine in my local run solution. 它在我的本地运行解决方案中运行良好。 When I host it to the IIS, it is working for few hours and then it stops working by throwing the error message "Error 404 found". 当我将它托管到IIS时,它正在工作几个小时,然后通过抛出错误消息“找到错误404”停止工作。 When I try to access through URL directly which is something like " http://127.0.0.1:51/Products/Get ", not working. 当我尝试通过URL直接访问,如“ http://127.0.0.1:51/Products/Get ”,无法正常工作。

Visual Studio version: Visual Studio版本:

Visual Studio Professional - 2015 Visual Studio Professional - 2015

IIS Version: IIS版本:

IIS - 7.5.7600 IIS - 7.5.7600

My approach: I have a simple project which simulates this scenario. 我的方法:我有一个模拟这种情况的简单项目。 It has the same problem with my original project. 它与我的原始项目有同样的问题。

Web API For Base Module: 基本模块的Web API:

WepApiConfig under App_Start: 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 }
        );
    }
}

Base API Controller: 基本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 For Module Web API: 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 }
        );
    }
}

Module API Controller: 模块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";
    }
}

NOTE from the above code: 从上面的代码中注意:

The difference between the two APIConfig files is : 两个APIConfig文件之间的区别是:

For Module Code: 对于模块代码:

routeTemplate: "api/module/{controller}/{action}/{id}"

For Base Code: 对于基本代码:

routeTemplate: "api/{controller}/{action}/{id}"

Global.asax: 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);
        }
    }
}

} }

What I tried with the above project: 我尝试了上面的项目:

After hosting in IIS, I tried to access the path which is something like this: 在IIS中托管后,我尝试访问路径,如下所示:

For Base API: 对于Base API:

http://localhost:51600/api/Values/Get HTTP://本地主机:51600 / API /价值/ GET

Returns: 返回:

value1
value2

For Module API 对于模块API

http://localhost:51600/api/Modules/Get HTTP://本地主机:51600 / API /模块/ GET

Returns: 返回:

value1
value2

My problem: 我的问题:

After sometime, when I try to access the same link, I am unable to get that. 过了一段时间,当我尝试访问相同的链接时,我无法得到它。 It says 它说

status 404. Not Found

I have been working on this issue for 3 days, and I couldn't resolve the problem. 我已经在这个问题上工作了3天,但我无法解决问题。

I have searched many articles in stackoverflow, but no luck. 我在stackoverflow中搜索了很多文章,但没有运气。

Kindly help me to get rid off from this. 请帮助我摆脱这一点。

Thanks. 谢谢。

如果您拥有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