简体   繁体   English

如何在 ASP.NET Web API v2 中自动实例化对象(单例)?

[英]How do I auto instantiate an object (singleton) in ASP.NET Web API v2?

I'm loading a large object in memory into a singleton for reuse between GET requests.我正在将内存中的一个大对象加载到一个单例中,以便在 GET 请求之间重用。 However when the application starts I want the singleton to initialize automatically, currently I have something like this但是,当应用程序启动时,我希望单例自动初始化,目前我有这样的事情

    public HttpResponseMessage GetData([FromBody]string text)
    {
        var spc = MySingleton.Instance; //runs some long instantiation process on the first call
        …
    }

So I have to wait until a request comes in before the singleton gets initialized.所以我必须等到请求进来后,才初始化单例。 Is there a way to make the singleton object initialize on its own when I start the application?当我启动应用程序时,有没有办法让单例对象自行初始化?

I tried putting this line我试着把这条线

var spc = MySingleton.Instance;

In the Application_Start() method of global.asax.cs but it didn't do anything.在 global.asax.cs 的 Application_Start() 方法中,但它没有做任何事情。

Trying use Dependency Injection, hopefully the IOC container will create your configured instance of the singleton object before your first request.尝试使用依赖注入,希望 IOC 容器会在您的第一个请求之前创建您配置的单例对象实例。

In Startup.cs , in the ConfigureServices method you will add something like:Startup.csConfigureServices方法中,您将添加如下内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ISingleton, Singleton>();
}

From this Singleton pattern tutorial here .来自这里的单例模式教程

In your controller that has the method GetData you will change the default constructor from parameter-less to having the Singleton object be a required argument to instantiate that controller.在具有GetData方法的控制器中,您将默认构造函数GetData参数更改为让 Singleton 对象成为实例化该控制器的必需参数。 The .NET Core IOC container will take care of instantiating the controller passing your singleton instance configured in in Startup.ConfigureServices . .NET Core IOC 容器将负责实例化控制器,传递在Startup.ConfigureServices配置的单例实例。

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

相关问题 使用ASP.NET WebAPI v2,如何在WebAPI方法中POST PDF数据? - Using ASP.NET WebAPI v2, how do I POST PDF data in a WebAPI method? 如何在 ASP.NET 中创建 OData v2 端点? - How do I create an OData v2 endpoint in ASP.NET? 在 ASP.NET 中,如何实例化一个在启动期间使用 DI 的单例对象? - In ASP.NET, how to instantiate a singleton object that uses DI during Startup? 如何验证 Azure AD v2 在 ASP.NET 核心 WEB ZDB97Z42387108CA81463 中生成的 OpenID Connect 访问令牌 - How to Validate OpenID Connect Access Token generated by Azure AD v2 in ASP.NET core WEB API? 如何将ASP.NET Web API属性路由与复杂对象参数一起使用? - How do I use ASP.NET Web API Attribute Routing with a complex object parameter? 在ASP.NET Web API 2中,如何在JSON序列化中保留对象引用? - In ASP.NET Web API 2, how do I preserve object references across JSON serialization? 如何在C#中使用HttpClient将对象正确发布到ASP.NET Web API? - How do I correctly POST an object to ASP.NET Web API using HttpClient in C#? ASP.NET Core 2.0 Web API Azure Ad v2令牌授权无效 - ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working 将Magento API V2集成到ASP.NET - Integrate Magento API V2 to ASP.NET 我是否需要在 ASP.NET 中处理 web 服务参考? 我可以使用 singleton 吗? - Do I need to dispose a web service reference in ASP.NET? Can I use a singleton?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM