Closed nourBerro closed 7 years ago
I can't recreate your bug. When debugging, only one EventStore is created. I have checked that the constructor of the event store is only run once. Can you please give some more details?
1- Add guid id property to inmemoryeventstore and let the constructor assign new Guid to it.
2- Add to HomeController constructor IEventStore prameter and put breakpoint 3 - go to Repository and put breakpoint to the constructor so u can check injected IEventStore And put on Save method a breakPoint
Run the application. Check your EventStore in homeController save the Id Then try to add new inventoryItem so the save breakpoint will puse your debug check again eventStore instanse There you will find new Guid.
Ok. I got to reproduce the bug. Do you have any idea why this happens? I don't want to add a dependency to any asp.net code to the cqrslite dll, so the files needs to be placed in the cqrsweb project. I also like to get a deeper understanding of the problem, so if you have any resources to how you fixed the bug, that would be great.
I solved the problem by creating middleware. check the code i posted eailer
If u dont want to use asp.net code in Cqrs dll its easy You can register the registrar in configure method in startup file instead of using the middleware
I'm curious to know what the source of this bug is as well as I've not experienced it.
I think the bug in asp.net dependancy injector. Because when i tried to create an instanse of IEventStore in ConfigureService method the service provider gave me the new instance of a singleton while the instance in the controller was a new one.
Are you using outdated versions of the asp.net dependencies by chance?
No i cloned your repo as it is.
The problem seems to be that the following code from Startup.cs creates a new and different serviceProvider than the one used to resolve controllers. And for that reason, one singleton is created in each of the different providers. I'll look more into a solution later today.
var serviceProvider = services.BuildServiceProvider();
var registrar = new BusRegistrar(new DependencyResolver(serviceProvider));
registrar.Register(typeof(InventoryCommandHandlers));
Found solution where I changed ConfigureServices signature to return IServiceProvider and returned the one I built. That way only one was created, and the problem was avoided.
the reason behind this change is the CQRSLite Dpandancy Resolver creates new instance of Singleton object like IEventStore and IRepository different than the asp.net core injected instances. i mean there is new diffrent instance from a singleton Object in the same Application LifeTime. you can check my theory by debugging the Id property i added in InMemoryEventStore. inject the IEventStore in HomeController and Compare the Id in HomeController instance and the Instance in IRepository when Dependacy resolver inject the IEventStroe when you Add new inventory Item. you will find them diffrent, but after my changes there will be one instance of singlton objects.
i couldnt add new pull so i will put my changes here:
1- UseCQrsLiteMiddleWare
2- Startup Class in Web project