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 Apr 17, 2022
1 parent 3ff6bda commit 1178d7f
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 @@ -123,5 +123,13 @@ public interface INinjectSettings
/// is <see langword="true"/>.
/// </value>
bool PropertyInjection { 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 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>
bool ThrowOnGetServiceNotFound { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/Ninject/NinjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,13 @@ public NinjectSettings()
/// is <see langword="true"/>.
/// </value>
public bool PropertyInjection { 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 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; }
}
}
4 changes: 3 additions & 1 deletion src/Ninject/ReadOnlyKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down

0 comments on commit 1178d7f

Please sign in to comment.