PerRequestLifetimeManager and Task.Factory.StartNew - Dependency Injection with Unity

不打扰是莪最后的温柔 提交于 2020-07-18 07:11:30

问题


How to manage new tasks with PerRequestLifeTimeManager? Should I create another container inside a new task?(I wouldn't like to change PerRequestLifeTimeManager to PerResolveLifetimeManager/HierarchicalLifetimeManager)

    [HttpPost]
    public ActionResult UploadFile(FileUploadViewModel viewModel)
    {
        var cts = new CancellationTokenSource();
        CancellationToken cancellationToken = cts.Token;
        Task.Factory.StartNew(() =>
        {
            // _fileService =  DependencyResolver.Current.GetService<IFileService>();
            _fileService.ProcessFile(viewModel.FileContent);

        }, cancellationToken);
    }

回答1:


You should read this article about DI in multi-threaded applications. Although it is written for a different DI library, you'll find most of the information applicable to the concept of DI in general. To quote a few important parts:

Dependency injection forces you to wire all dependencies together in a single place in the application: the Composition Root. This means that there is a single place in the application that knows about how services behave, whether they are thread-safe, and how they should be wired. Without this centralization, this knowledge would be scattered throughout the code base, making it very hard to change the behavior of a service.

In a multi-threaded application, each thread should get its own object graph. This means that you should typically call [Resolve<T>()] once at the beginning of the thread’s execution to get the root object for processing that thread (or request). The container will build an object graph with all root object’s dependencies. Some of those dependencies will be singletons; shared between all threads. Other dependencies might be transient; a new instance is created per dependency. Other dependencies might be thread-specific, request-specific, or with some other lifestyle. The application code itself is unaware of the way the dependencies are registered and that’s the way it is supposed to be.

The advice of building a new object graph at the beginning of a thread, also holds when manually starting a new (background) thread. Although you can pass on data to other threads, you should not pass on container-controlled dependencies to other threads. On each new thread, you should ask the container again for the dependencies. When you start passing dependencies from one thread to the other, those parts of the code have to know whether it is safe to pass those dependencies on. For instance, are those dependencies thread-safe? This might be trivial to analyze in some situations, but prevents you to change those dependencies with other implementations, since now you have to remember that there is a place in your code where this is happening and you need to know which dependencies are passed on. You are decentralizing this knowledge again, making it harder to reason about the correctness of your DI configuration and making it easier to misconfigure the container in a way that causes concurrency problems.

So you should not spin of new threads from within your application code itself. And you should definitely not create a new container instance, since this can cause all sorts of performance problems; you should typically have just one container instance per application.

Instead, you should pull this infrastructure logic into your Composition Root, which allows your controller's code to be simplified. Your controller code should not be more than this:

[HttpPost]
public ActionResult UploadFile(FileUploadViewModel viewModel)
{
    _fileService.ProcessFile(viewModel.FileContent);
}

On the other hand, you don't want to change the IFileService implementation, because it shouldn't its concern to do multi-threading. Instead we need some infrastructural logic that we can place in between the controller and the file service, without them having to know about this. They way to do this is by implementing a proxy class for the file service and place it in your Composition Root:

private sealed class AsyncFileServiceProxy : IFileService {
    private readonly ILogger logger;
    private readonly Func<IFileService> fileServiceFactory;
    public AsyncFileServiceProxy(ILogger logger, Func<IFileService> fileServiceFactory)
    {
        this.logger = logger;
        this.fileServiceFactory = fileServiceFactory;
    }

    void IFileService.ProcessFile(FileContent content) {
        // Run on a new thread
        Task.Factory.StartNew(() => {
            this.BackgroundThreadProcessFile(content);
        });
    }

    private void BackgroundThreadProcessFile(FileContent content) {
        // Here we run on a different thread and the
        // services should be requested on this thread.
        var fileService = this.fileServiceFactory.Invoke();

        try {
            fileService.ProcessFile(content);
        } 
        catch (Exception ex) {
            // logging is important, since we run on a
            // different thread.
            this.logger.Log(ex);
        }
    }
}

This class is a small peace of infrastructural logic that allows processing files on a background thread. The only thing left is to configure the container to inject our AsyncFileServiceProxy instead of the real file service implementation. There are multiple ways to do this. Here's an example:

container.RegisterType<ILogger, YourLogger>();
container.RegisterType<RealFileService>();
container.RegisterType<Func<IFileService>>(() => container.Resolve<RealFileService>(),
    new ContainerControlledLifetimeManager());
container.RegisterType<IFileService, AsyncFileServiceProxy>();

One part however is missing here from the equation, and this is how to deal with scoped lifestyles, such as the per-request lifestyle. Since you are running stuff on a background thread, there is no HTTPContext and this basically means that you need to start some 'scope' to simulate a request (since your background thread is basically its own new request). This is however where my knowledge about Unity stops. I'm very familiar with Simple Injector and with Simple Injector you would solve this using a hybrid lifestyle (that mixes a per-request lifestyle with a lifetime-scope lifestyle) and you explicitly wrap the call to BackgroundThreadProcessFile in such scope. I imagine the solution in Unity to be very close to this, but unfortunately I don't have enough knowledge of Unity to show you how. Hopefully somebody else can comment on this, or add an extra answer to explain how to do this in Unity.



来源:https://stackoverflow.com/questions/26698177/perrequestlifetimemanager-and-task-factory-startnew-dependency-injection-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!