Simple Injector / IoC - Windows Service and Request Cycles for a Queue Processor

I am writing a queue processor in C # as a Windows service. The backend queue mechanism is MongoDB. The goal of the queue is to run out-of-band requests that originated from our main website (Angular w Web API). For each object in the queue, a serialized instance of Command + serialized Context information, I would like

foreach request loop:
1) Create a DbContext (EF) if necessary for the current command handler
2) Deserialize the AppContext and get this information entered into the current command handler

Not sure how to handle these patterns in Simple Injector. Moreover, this is a timer cycle, not a web request, because there are helpers / classes already written for. Thoughts? I have seen other IoC containers use lambda expressions to handle this kind of thing in the past. Just not sure how to approach my scenarios number 1 and number 2.

+4
source share
1 answer

Each timer pulse can be considered a new request. Or, if you process several commands in one pulse, each command can be considered a new request.

, ASP.NET WCF, (-, WCF ..), Simple Injector . - Simple Injector Web Forms, MVC, Web API WCF. Simple Injector, - .

Windows, , ​​ . , . DI; .

, . Execution Context Scope lifestyle. , ; .

, "" , , . :

public void ProcessCommand(object command) {
    using (this.container.BeginLifetimeScope()) {
        Type handlerType = 
            typeof(ICommandHandler<>).MakeGenericType(command.GetType());

        dynamic handler = container.GetInstance(handlerType);

        handler.Handle((dynamic)command);
    }
}

, . , LifetimeScopeLifestyle:

ScopedLifestyle scopedLifestyle = new LifetimeScopeLifestyle();

container.Register<IUnitOfWork, DbContextUnitOfWork>(scopedLifestyle);

v3:

var container = new Container();
container.Options.DefaultScopedLifestyle = new LifetimeScopeLifestyle();

container.Register<IUnitOfWork, DbContextUnitOfWork>(Lifestyle.Scoped);

, , , ( ProcessCommand).

+5

All Articles