I am using entity framework core to persist data to a sql database and our endpoint registered the dbcontext to be UnitOfWork and I think this is causing issues because the connection is being shared across many different handlers asynchronously. I came across this sample as a way to register my dbcontext and still use UnitOfWork, https://docs.particular.net/samples/entity-framework-core/.
endpointConfiguration.RegisterComponents(c =>
{
c.ConfigureComponent(b =>
{
var session = b.Build<ISqlStorageSession>();
var context = new ReceiverDataContext(new DbContextOptionsBuilder<ReceiverDataContext>()
.UseSqlServer(session.Connection)
.Options);
//Use the same underlying ADO.NET transaction
context.Database.UseTransaction(session.Transaction);
//Ensure context is flushed before the transaction is committed
session.OnSaveChanges(s => context.SaveChangesAsync());
return context;
}, DependencyLifecycle.InstancePerUnitOfWork);
});
So in my endpointconfig this is how I registered my dbcontext, but when I attempt to test after deploying to our development server I am receiving the following error:
System.InvalidOperationException: Unable to resolve type: NServiceBus.Persistence.Sql.ISqlStorageSession, service name:
at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError)
at LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError)
at LightInject.ServiceContainer.GetInstance(Type serviceType)
at LightInject.Scope.WithThisScope[T](Func1 function) at NServiceBus.CommonObjectBuilder.Build[T]() at mwa.CA.Life.Endpoints.BeneficiaryProcessing.EndpointConfig.<>c__DisplayClass0_0.<GetConfiguration>b__1(IBuilder b) at LightInject.PerScopeLifetime.CreateScopedInstance(Scope scope, Func
1 createInstance)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func
2 valueFactory)
at LightInject.ServiceContainer.GetInstance(Type serviceType)
at LightInject.Scope.WithThisScope[T](Func1 function) at NServiceBus.CommonObjectBuilder.Build[T]() at mwa.CA.Life.Endpoints.BeneficiaryProcessing.EndpointConfig.<>c.<GetConfiguration>b__0_4(IBuilder x) at LightInject.PerScopeLifetime.CreateScopedInstance(Scope scope, Func
1 createInstance)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func
2 valueFactory)
at DynamicMethod(Object[] )
at LightInject.PerScopeLifetime.CreateScopedInstance(Scope scope, Func1 createInstance) at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func2 valueFactory) at LightInject.ServiceContainer.GetInstance(Type serviceType) at LightInject.Scope.WithThisScope[T](Func
1 function)
at NServiceBus.LoadHandlersConnector.d__1.MoveNext()
It appears that the container can’t resolve the ISqlStorageSession dependency. Any ideas how I might get this to work?
Thanks,
David