如何以其依赖性初始化Singleton?

发布于 2025-02-07 20:02:08 字数 1512 浏览 1 评论 0 原文

我试图在.NET 6中强制立即实例化或单身人士。我想这样做的原因是因为我想在应用程序启动的那一刻加载一些数据。我不知道我的方法是否正确,并且是否可能。建议非常欢迎。

这是我到目前为止尝试的。

我的program.cs配置,

builder.Services.AddScoped<IIpLocationService, IpLocationService>();

var serviceProvider = builder.Services.BuildServiceProvider();
var memoryCacheProvider = new MemoryCacheProvider(
    serviceProvider.CreateScope().ServiceProvider
        .GetService<IIpLocationService>());
builder.Services.AddSingleton<IMemoryCacheProvider>(memoryCacheProvider)

但我会收到此错误。

system.invalidoperationException:“无法解析type'auth.in_memorydatabase.imemorycacheprovider'的服务时,试图激活'auth.services.services.iplocationservice'。'

'

Iplocationservice也有其自己的依赖性。即,负责从数据库和内存cacheprovider类中检索信息以存储信息中的信息的单位工程。

这是我的内存量表代码,

public interface IMemoryCacheProvider
{
    List<IpLocation> IpLocationList { get; set; }
}

public class MemoryCacheProvider : IMemoryCacheProvider
{
    private readonly IIpLocationService _ipLocationService;
    public List<IpLocation> IpLocationList { get; set; } = new List<IpLocation>();


    public MemoryCacheProvider(IIpLocationService ipLocationService)
    {
        _ipLocationService = ipLocationService;
        Init();
    }

    private void Init()
    {
        // do something with it
        _ipLocationService.Init();

    }
}

有什么方法可以做到这项工作或类似的方法?我只需要数据即可开始加载应用程序启动的那一刻,因为它是很多数据。因此,我必须能够调用iplocationservice的初始方法。

I am trying to Force the immediate instantiation or a Singleton in .Net 6. The reason I want to do this is because I want to load some data the moment that the application starts. I don't know if my approach is correct and if it is possible. Suggestions are more than welcome.

This is what I have tried so far.

My program.cs configuration

builder.Services.AddScoped<IIpLocationService, IpLocationService>();

var serviceProvider = builder.Services.BuildServiceProvider();
var memoryCacheProvider = new MemoryCacheProvider(
    serviceProvider.CreateScope().ServiceProvider
        .GetService<IIpLocationService>());
builder.Services.AddSingleton<IMemoryCacheProvider>(memoryCacheProvider)

But I get this error.

System.InvalidOperationException: 'Unable to resolve service for type 'Auth.In_MemoryDatabase.IMemoryCacheProvider' while attempting to activate 'Auth.Services.IpLocationService'.'

IIpLocationService has its own dependencies too. Namely a unitofwork in charge of retrieving the information from a database and the MemoryCacheProvider class to store the information in-memory.

This is my code of the MemoryCacheProvider

public interface IMemoryCacheProvider
{
    List<IpLocation> IpLocationList { get; set; }
}

public class MemoryCacheProvider : IMemoryCacheProvider
{
    private readonly IIpLocationService _ipLocationService;
    public List<IpLocation> IpLocationList { get; set; } = new List<IpLocation>();


    public MemoryCacheProvider(IIpLocationService ipLocationService)
    {
        _ipLocationService = ipLocationService;
        Init();
    }

    private void Init()
    {
        // do something with it
        _ipLocationService.Init();

    }
}

Is there a way I can make this work or something similar? I just need the data to start loading the moment the application starts because it's a lot of data. For this reason, I must be able to call the Init method of the IpLocationService.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2025-02-14 20:02:08

我不太确定您要完成什么

有什么方法可以做这项工作还是类似的方法?我只需要数据即可开始加载应用程序启动的那一刻,因为它是很多数据。因此,我必须能够调用iplocationservice的初始方法。

也许您可以查看托管服务(iHostedService)。该代码将在收到请求之前运行。这样,您可以将依赖项注入托管服务,并获取数据。
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/host/hosted-services?view = ateaspnetcore-6.0&amp = vis = visual-studio studio studio#startasync

其他可以在您的program.cs中运行它
构建主机后,您可以在运行主机后获得服务。

var host = CreateHostBuilder(args).Build();
host.Services.GetRequiredService<...>(...);
host.Run();

我希望这能为您提供有关如何解决问题的一些想法。

I am not quite sure what you are trying to accomplish

Is there a way I can make this work or something similar? I just need the data to start loading the moment the application starts because it's a lot of data. For this reason, I must be able to call the Init method of the IpLocationService.

Maybe you can take a look in a Hosted service (IHostedService). This code will run before requests can be received. This way you can inject your dependancies in the hosted service, and fetch fetch your data.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio#startasync

Alternatively, you can run it in your Program.cs
You can get your services after building the Host, but before you run it.

var host = CreateHostBuilder(args).Build();
host.Services.GetRequiredService<...>(...);
host.Run();

I hope this gives you some ideas on how to solve your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文