Skip to content

Commit 8099155

Browse files
committed
Added more descriptive exception message when resolving a cross-wired dependency fails. Fixes #1
1 parent fca17ab commit 8099155

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/SimpleInjector.Integration.ServiceCollection/SimpleInjector.Integration.ServiceCollection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</Description>
66
<AssemblyTitle>Simple Injector IServiceCollection Integration</AssemblyTitle>
77
<NeutralLanguage>en-US</NeutralLanguage>
8-
<VersionPrefix>5.0.0</VersionPrefix>
8+
<VersionPrefix>5.0.1</VersionPrefix>
99
<PackageReleaseNotes>https://github.com/simpleinjector/SimpleInjector.Integration.AspNetCore/releases/tag/5.0.0</PackageReleaseNotes>
1010
<AssemblyVersion>5.0.0.0</AssemblyVersion>
1111
<Authors>Simple Injector Contributors</Authors>

src/SimpleInjector.Integration.ServiceCollection/SimpleInjectorServiceCollectionExtensions.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,9 @@ private static Registration CreateCrossWireRegistration(
492492
return registration;
493493
}
494494

495-
private static Func<object> BuildSingletonInstanceCreator(
496-
Type serviceType, IServiceProvider rootProvider)
495+
private static Func<object> BuildSingletonInstanceCreator(Type serviceType, IServiceProvider rootProvider)
497496
{
498-
return () => rootProvider.GetRequiredService(serviceType);
497+
return () => GetRequiredService(rootProvider, serviceType);
499498
}
500499

501500
private static Func<object> BuildScopedInstanceCreator(
@@ -524,10 +523,28 @@ private static Func<object> BuildScopedInstanceCreator(
524523
$"Error resolving the cross-wired {serviceType.ToFriendlyName()}. {ex.Message}", ex);
525524
}
526525

527-
return current.GetRequiredService(serviceType);
526+
return GetRequiredService(current, serviceType);
528527
};
529528
}
530529

530+
private static object GetRequiredService(IServiceProvider provider, Type serviceType)
531+
{
532+
try
533+
{
534+
return provider.GetRequiredService(serviceType);
535+
}
536+
catch (Exception ex)
537+
{
538+
throw new InvalidOperationException(
539+
$"Failed to resolve {serviceType.ToFriendlyName()}. {serviceType.ToFriendlyName()} is a " +
540+
$"cross-wired service, meaning that Simple Injector forwarded the request the framework's " +
541+
$"IServiceProvider in order to get an instance. The used {provider.GetType().FullName}, " +
542+
$"however, failed with the following message: \"{ex.Message}\". This error might indicate a " +
543+
$"misconfiguration of services in the framework's IServiceCollection.",
544+
ex);
545+
}
546+
}
547+
531548
private static ServiceDescriptor? FindServiceDescriptor(IServiceCollection services, Type serviceType)
532549
{
533550
// In case there are multiple descriptors for a given type, .NET Core will use the last

0 commit comments

Comments
 (0)