如何在异步任务中使用依赖注入进行暂时实例循环

发布于 2025-02-11 07:22:11 字数 1729 浏览 2 评论 0原文

有一个可以实现称为somethingmanager.cs的界面的类 类似:

 public class SomethingManager : ISomethingManager

这是.NET 6中的Worker服务,并且在同一解决方案中还有另一个类库项目,其中包含某物Manager的接口和实现。 依赖项正在在Worker服务项目中注册,例如

      public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog()
            .ConfigureServices((hostContext, services) =>
            {
                //AddSingleton or Transient here?
                services.AddSingleton<ISomethingManager, SomethingManager>();
                ...

问题是,在以异步方式工作的入口点项目中,有一个循环类似:

       foreach (xml in xmls)
            {
                tasks.Add(StartProcessAsync(xml));
            }
            await Task.WhenAll(tasks);

Inside startProcessAsync它使用了以前已注册并注入构造函数的某种东西。 问题在于,类曼纳(Glass Manager)具有某些私人成员,这些私人成员应该在每个任务中都是唯一的,我注意到这样,它会在任务之间造成致命错误。实际上,此类需要共享一个sessionID,即session ind abot.connect()每次都会给出值。我们必须在每个任务内一次调用一次操作。

所以, 我的问题是,如何注册依赖项注入的某种事物,以及使用此实例(已注册为DI)的每个任务为其私人成员具有不同的值? 而且,如果不能这样做,我应该每次都为此创建新实例吗?

  public Task StartProcessAsync(xmlFileInfo xml)
    {
        return Task.Run(async () =>
        {
            //this one doesn't work inside tasks loop it cases problems because
            //the sessionId that contains has to be different for every task
            //_somethingManager.DoSomething();
            //Like this?
            var somethingManager= SomethingManager(_someSettings);
            somethingManager.DoSomething();
            var mem = somethingManager.ThePrivateMember; 
            //another object which has also private members in the same class.
         });
}

There is a class that implements an interface called SomethingManager.cs
like:

 public class SomethingManager : ISomethingManager

This is a worker service in .net 6 and there is another class library project in the same solution that contains the interface and implementation of SomethingManager.
Dependencies are being registered in the worker service project like

      public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog()
            .ConfigureServices((hostContext, services) =>
            {
                //AddSingleton or Transient here?
                services.AddSingleton<ISomethingManager, SomethingManager>();
                ...

The problem is that, in the entrypoint project that works in an async way,there is a loop like:

       foreach (xml in xmls)
            {
                tasks.Add(StartProcessAsync(xml));
            }
            await Task.WhenAll(tasks);

Inside StartProcessAsync it uses SomethingManager instance hat was previously registered and injected in the constructor.
The problem is that the class SomethingManager has some private members that are supposed to be unique for every task and I noticed that in this way it causes fatal errors between the tasks.Actualy this class needs to share a sessionId that a method .Connect() is giving the value every time.We have to call .Connect() method, one time before other actions,inside every task.

So,
My question is how can I register the SomethingManager with Dependency Injection and every task that uses this instance (which is registered with DI) to have different values for its private members?
And, if can't do it in this way, am I supposed to create new instance for this every time?

  public Task StartProcessAsync(xmlFileInfo xml)
    {
        return Task.Run(async () =>
        {
            //this one doesn't work inside tasks loop it cases problems because
            //the sessionId that contains has to be different for every task
            //_somethingManager.DoSomething();
            //Like this?
            var somethingManager= SomethingManager(_someSettings);
            somethingManager.DoSomething();
            var mem = somethingManager.ThePrivateMember; 
            //another object which has also private members in the same class.
         });
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文