我试图在.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.
发布评论
评论(1)
我不太确定您要完成什么
也许您可以查看托管服务(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中运行它
构建主机后,您可以在运行主机后获得服务。
我希望这能为您提供有关如何解决问题的一些想法。
I am not quite sure what you are trying to accomplish
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.
I hope this gives you some ideas on how to solve your issue.