I am instantiating an IDocumentSession object as a singleton, using ASP.NET Core DependencyInjection Provider.
I am also setting in UseMarten lazyStoreInitialization on true and in UseSessions the ServiceLifetime as Singleton.
This doesn't seem to be enough. I have inspected the code and fount out that the Connection object is being disposed and set to null on every commit, since The CommandRunnerMode is no external! Setting CommandRunnerMode to external means: The Transactions will not be comitted.
Managing the transactions on my side would be too complicated. All I needed is a singleton object, comitting on every saveChange call. Is that possible or do you have any demo for that case?
@mhabibal13 you should not use IDocumentSession as a singleton, it should be scoped or transient. DocumentStore should be Singleton.
That comes from the stuff like the timeouts, transactions etc. It's practically not possible to maintain such long open connection/transaction.
Underneath we're using NpgsqlConnection that officially is recommended to use as Transient, and never as a singleton.
@mhabibal13 you should not use
IDocumentSessionas a singleton, it should be scoped or transient.DocumentStoreshould be Singleton.That comes from the stuff like the timeouts, transactions etc. It's practically not possible to maintain such long open connection/transaction.
Underneath we're using
NpgsqlConnectionthat officially is recommended to use as Transient, and never as a singleton.
Yes, I agree on that. Still, I don't get the point of having UseSessions with the following override
IServiceCollection UseSessions(ServiceLifetime sessionLifetime = ServiceLifetime.Scoped, SessionOptions options = null). Doesnt that mean, that Session could be instantiated as a singleton.
Still, setting it to Singleton doesn't make it a one.
So I was confused of that behavior.
I think that you're referring to https://github.com/jokokko/marten.aspnetcore. You should file an issue there. This is not yet official Marten plugin.
It's almost official, as @jokokko is one of the Marten maintainers, but still, it's not yet in the Marten project official umbrella.
Currently, we don't have any code in Marten directly related to IoC.
I believe that the intention was to not block User to register Session in different scope if the user needs it. Technically you can register it as a singleton, but in most of the cases that practically doesn't make much sense.
I see. Thanks
@mhabibal13 see also my sample of Marten IoC configuration https://github.com/oskardudycz/EventSourcing.NetCore/blob/master/Sample/Tickets/Core/Storage/MartenConfig.cs#L39. I usually do it like that and I didn't have any issues.