From dffd91455dec689ced9f79cde7259a96b8cc9340 Mon Sep 17 00:00:00 2001 From: klopov Date: Thu, 20 Aug 2020 15:03:57 +0500 Subject: [PATCH 1/2] Better use GetInstance because TryGetInstance doesn't resolve instances of concrete types --- .../StructureMapServiceProvider.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs b/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs index 0b211bf..f143d85 100644 --- a/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs +++ b/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs @@ -19,14 +19,8 @@ public StructureMapServiceProvider(IContainer container) public object GetService(Type serviceType) { - if (serviceType.IsGenericEnumerable()) - { - // Ideally we'd like to call TryGetInstance here as well, - // but StructureMap does't like it for some weird reason. - return GetRequiredService(serviceType); - } - - return Container.TryGetInstance(serviceType); + // TryGetInstance doesn't resolve instances of concrete types + return Container.GetInstance(serviceType); } public object GetRequiredService(Type serviceType) From 61edaf6c8aa135090ad4cee175f4d829c6d3f100 Mon Sep 17 00:00:00 2001 From: klopov Date: Thu, 20 Aug 2020 18:53:17 +0500 Subject: [PATCH 2/2] - when got structureMapConfigurationException we want to return null because we replace TryGetInstance - add test --- .../StructureMapServiceProvider.cs | 9 ++++++++- .../StructureMapServiceProviderTests.cs | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs b/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs index f143d85..c0aca25 100644 --- a/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs +++ b/src/StructureMap.Microsoft.DependencyInjection/StructureMapServiceProvider.cs @@ -20,7 +20,14 @@ public StructureMapServiceProvider(IContainer container) public object GetService(Type serviceType) { // TryGetInstance doesn't resolve instances of concrete types - return Container.GetInstance(serviceType); + try + { + return Container.GetInstance(serviceType); + } + catch (StructureMapConfigurationException) + { + return null; + } } public object GetRequiredService(Type serviceType) diff --git a/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapServiceProviderTests.cs b/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapServiceProviderTests.cs index 6d2e5da..26c9c28 100644 --- a/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapServiceProviderTests.cs +++ b/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapServiceProviderTests.cs @@ -24,6 +24,16 @@ public void can_start_and_tear_down_a_scope_from_the_provider() Assert.IsType(provider.GetRequiredService(typeof(IWidget))); } + [Fact] + public void get_service_without_registered_types_return_null() + { + var root = new Container(); + + var provider = new StructureMapServiceProvider(root); + Assert.Same(provider.Container, root); + Assert.Null(provider.GetService(typeof(IWidget))); + } + public interface IWidget { } public class BlueWidget : IWidget { }