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 bd12b1c commit b7668b3
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.ShouldThrow<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 @@ -96,6 +96,14 @@ public interface INinjectSettings
/// <value><c>true</c> if null is allowed as injected value otherwise false.</value>
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 the value for the specified key.
/// </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 @@ -411,7 +411,9 @@ public virtual IEnumerable<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 @@ -142,6 +142,14 @@ public bool AllowNullInjection
set { this.Set("AllowNullInjection", value); }
}

/// <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 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 <see langword="null"/> if there is no such service.
/// </summary>
public bool ThrowOnGetServiceNotFound { get; set; }

/// <summary>
/// Gets the value for the specified key.
/// </summary>
Expand Down

0 comments on commit b7668b3

Please sign in to comment.