Skip to content

Commit

Permalink
Fixing IServiceProvider.GetService implementation (fixes #376) (#377)
Browse files Browse the repository at this point in the history
* Fixing IServiceProvider.GetService implementation (fixes #376)

* Added a configuration option to restore the old behavior if necessary
  • Loading branch information
lord-executor authored and scott-xu committed Apr 17, 2022
1 parent 80ce0dc commit 08da949
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
35 changes: 34 additions & 1 deletion src/Ninject.Test/Integration/StandardKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,40 @@ public void ItCanBeResolved()
}
}
}


public class WhenServiceIsResolvedThroughServiceProviderInterface : StandardKernelContext
{
[Fact]
public void ItResolvesBoundService()
{
this.kernel.Bind<IWeapon>().To<Sword>();

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<ActivationException>();
}
}
}

public class InitializableA : IInitializable
{
public static int Count = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/Ninject/INinjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public interface INinjectSettings
/// </remarks>
bool AllowNullInjection { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the old (&lt;= 3.3.4) behavior of <see cref="IServiceProvider.GetService(Type)"/>
/// should be used which throws 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 <see langword="null"/> if there is no such service.
/// </summary>
bool ThrowOnGetServiceNotFound { get; set; }

/// <summary>
/// Gets a value indicating whether method injection should enabled.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Ninject/KernelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ public virtual IBinding[] GetBindings(Type service)
/// <returns>The service object.</returns>
object IServiceProvider.GetService(Type service)
{
return this.Get(service);
return this.Settings.ThrowOnGetServiceNotFound
? this.Get(service)
: this.TryGet(service);
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Ninject/NinjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public NinjectSettings()
/// </remarks>
public bool AllowNullInjection { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the old (&lt;= 3.3.4) behavior of <see cref="IServiceProvider.GetService(Type)"/>
/// should be used which throws 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 <see langword="null"/> if there is no such service.
/// </summary>
public bool ThrowOnGetServiceNotFound { get; set; }

/// <summary>
/// Gets or sets a value indicating whether method injection should enabled.
/// </summary>
Expand Down

0 comments on commit 08da949

Please # to comment.