diff --git a/src/Ninject.Test/Integration/StandardKernelTests.cs b/src/Ninject.Test/Integration/StandardKernelTests.cs index 6b47d5ef..c00fefcb 100644 --- a/src/Ninject.Test/Integration/StandardKernelTests.cs +++ b/src/Ninject.Test/Integration/StandardKernelTests.cs @@ -567,7 +567,40 @@ public void ItCanBeResolved() } } } - + + public class WhenServiceIsResolvedThroughServiceProviderInterface : StandardKernelContext + { + [Fact] + public void ItResolvesBoundService() + { + this.kernel.Bind().To(); + + var provider = this.kernel as IServiceProvider; + provider.GetService(typeof(IWeapon)).Should().NotBeNull(); + } + + [Fact] + public void ItReturnsNullWhenServiceIsNotConfigured() + { + var provider = this.kernel as IServiceProvider; + provider.GetService(typeof(Samurai)).Should().BeNull(); + } + + [Fact] + public void ItThrowsWhenServiceIsNotConfiguredAndSettingThrowOnGetServiceNotFound() + { + using (var kernel = new StandardKernel(new NinjectSettings + { + ThrowOnGetServiceNotFound = true + })) + { + var provider = kernel as IServiceProvider; + Action resolveAction = () => provider.GetService(typeof(Samurai)); + resolveAction.Should().Throw(); + } + } + } + public class InitializableA : IInitializable { public static int Count = 0; diff --git a/src/Ninject/INinjectSettings.cs b/src/Ninject/INinjectSettings.cs index 1de51594..2a0831f7 100644 --- a/src/Ninject/INinjectSettings.cs +++ b/src/Ninject/INinjectSettings.cs @@ -123,5 +123,13 @@ public interface INinjectSettings /// is . /// bool PropertyInjection { get; set; } + + /// + /// Gets or sets a value indicating whether the old (<= 3.3.4) behavior of + /// should be used which thorows an exception if the requested service cannot be found. Note that the documentation + /// of that method https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider.getservice?view=netframework-4.6.2 + /// states that the method should return if there is no such service. + /// + bool ThrowOnGetServiceNotFound { get; set; } } } \ No newline at end of file diff --git a/src/Ninject/NinjectSettings.cs b/src/Ninject/NinjectSettings.cs index 8439ccc3..9163fbab 100644 --- a/src/Ninject/NinjectSettings.cs +++ b/src/Ninject/NinjectSettings.cs @@ -166,5 +166,13 @@ public NinjectSettings() /// is . /// public bool PropertyInjection { get; set; } + + /// + /// Gets or sets a value indicating whether the old (<= 3.3.4) behavior of + /// should be used which thorows an exception if the requested service cannot be found. Note that the documentation + /// of that method https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider.getservice?view=netframework-4.6.2 + /// states that the method should return if there is no such service. + /// + public bool ThrowOnGetServiceNotFound { get; set; } } } \ No newline at end of file diff --git a/src/Ninject/ReadOnlyKernel.cs b/src/Ninject/ReadOnlyKernel.cs index 4c8f9a0c..334e50fc 100644 --- a/src/Ninject/ReadOnlyKernel.cs +++ b/src/Ninject/ReadOnlyKernel.cs @@ -246,7 +246,9 @@ public object GetService(Type serviceType) { Ensure.ArgumentNotNull(serviceType, nameof(serviceType)); - return this.Get(serviceType); + return this.settings.ThrowOnGetServiceNotFound + ? this.Get(serviceType) + : this.TryGet(serviceType); } ///