ScopeValidation of DI when in Production Mode
galenp opened this issue · comments
Hi Charles
Noticed that when Agg.Net is run in Produciton mode then scope validation does not happen. However running an endpoint with:
"ASPNETCORE_ENVIRONMENT": "Development"
environmnet varliable
Will result in:
Cannot resolve scoped service 'Aggregates.UnitOfWork.IUnitOfWork' from root provider.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.OnResolve(Type serviceType, IServiceScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Aggregates.Configuration.<Start>d__10.MoveNext()
at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__12.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at Program.<<Main>$>d__0.MoveNext() in C:\development\sas-services\src\Endpoints\SAS.Endpoint.Domain\Endpoint.cs:line 135
I think the triggering code is:
Thanks I'll take a look at this.
Are you using the latest build from nuget? @galenp
@galenp I can't reproduce the exception using my asp net project. Can you confirm which version you are using?
HI @charlessolar - latest build from Nuget, make sure the "ASPNETCORE_ENVIRONMENT" is set to "Development" which turns on Scope validation.
var host = Host.CreateDefaultBuilder(args)
.UseConsoleLifetime()
.AddAggregatesNet(c => c
.EventStore(es => es.AddClient(EndpointHelper.GetEventStoreConnectionString(configuration), endpointName))
.Domain()
.AddMutator<Mutator>(factory => factory.GetRequiredService<Mutator>())
.NewtonsoftJson()
.NServiceBus(nsb)
.SetUniqueAddress(Environment.MachineName)
.SetDevelopmentMode(true)
.SetCommandDestination(endpointName)).Build();
Aha - @galenp do you not have a IUnitOfWork implementation for your domain? Even though its the domain endpoint I still have a UnitOfWork to communicate and save certain things to mongo..
You're missing a
.Application<IUnitOfWork, UnitOfWork>()
in your setup.
But I suppose if you have no use for IUnitOfWork in the domain then it should still work
I made a change 4a86c36 which may fix this issue for you, please let me know!
Aha - @galenp do you not have a IUnitOfWork implementation for your domain? Even though its the domain endpoint I still have a UnitOfWork to communicate and save certain things to mongo..
You're missing a
.Application<IUnitOfWork, UnitOfWork>()
in your setup.But I suppose if you have no use for IUnitOfWork in the domain then it should still work
- Domain endpoint just handles commands > updates entity > emits events
- Projections endpoint handles the "Application Unit Of Work" by creating the read models into Raven
So far I've not needed a .UoW() or .App() in the domain endpoint.
Did you try with the latest nuget yet? Seems to work for me
Taking a look at this with fresh eyes, if this is even still an issue..
I need to know which object specifically its trying to validate that's causing the issue.
Something is trying to create the IUnitOfWork outside a scope and I just don't see where it might be.
Im going to close this feel free to reopen if its still an issue.