gpt4 book ai didi

.net - Global.asax ASP.Net Web API 中的 GlobalConfiguration.Configuration.EnsureInitialized() 是什么

转载 作者:行者123 更新时间:2023-12-04 12:18:24 25 4
gpt4 key购买 nike

在我的WebApi项目下,我不知道这一行是什么GlobalConfiguration.Configuration.EnsureInitialized()在那里做 Global.asax Application_Start()方法。没有这个,即使每件事都运行良好。那这是为什么呢?

有必要在这里吗?如果是,那为什么?任何人都可以解释其
详细需求和目的
.

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);

//////////// What is this for ////////////////
GlobalConfiguration.Configuration.EnsureInitialized();
}

NOT A DUPLICATE : In stack overflow many people suggest to use EnsureInitialized().But Why to use it? Where it comes from? Is it part of webApi2 or something else? These are are the things I want to know. MSDN itself has no explanation for this.

最佳答案

MSDN :

HttpConfiguration.EnsureInitialized Method

Invoke the Intializer hook. It is considered immutable from this point forward. It's safe to call this multiple times.


更多信息
this answer指出,这很可能在某个时候被放入您的应用程序中,因为 Web API 应该在 Web API v1 与 Web API v2 中注册的方式发生了变化。
升级 Web API 后未进行更改的任何人都会收到错误消息:

The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.


不幸的是,错误消息中给出的解决方案具有误导性。你是什​​么 actually supposed to change while upgrading from V1是替换这一行:
WebApiConfig.Register(GlobalConfiguration.Configuration);
用这一行:
GlobalConfiguration.Configure(WebApiConfig.Register);
后一种方法在内部调用 EnsureInitialized所以你不必从你的启动代码。
我注意到您的配置中没有这些 Web Api 初始化程序中的任何一个,因此我建议您按如下方式更改启动:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

关于.net - Global.asax ASP.Net Web API 中的 GlobalConfiguration.Configuration.EnsureInitialized() 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37769348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com