diff options
author | Maryam Ariyan <maryam.ariyan@microsoft.com> | 2020-11-25 21:23:03 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-25 21:23:03 +0300 |
commit | 4cfc831bf802bee7daca3b01e0ce5ed1b09b4574 (patch) | |
tree | bd4cafc944471acdb3e354310cd4b1308682c9af /src/libraries | |
parent | 5660c4a8b121680f6965416fb5f37a6fd49e0976 (diff) |
Fix NRE when default value is null and ServiceCallSite is not found (#44877)
Diffstat (limited to 'src/libraries')
6 files changed, 54 insertions, 3 deletions
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/Directory.Build.props b/src/libraries/Microsoft.Extensions.DependencyInjection/Directory.Build.props new file mode 100644 index 00000000000..94276d35809 --- /dev/null +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/Directory.Build.props @@ -0,0 +1,7 @@ +<Project> + <Import Project="..\Directory.Build.props" /> + <PropertyGroup> + <PackageVersion>5.0.1</PackageVersion> + <AssemblyVersion>5.0.0.1</AssemblyVersion> + </PropertyGroup> +</Project>
\ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ConstantCallSite.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ConstantCallSite.cs index acc1d967f73..bd09485c617 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ConstantCallSite.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ConstantCallSite.cs @@ -7,10 +7,12 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup { internal class ConstantCallSite : ServiceCallSite { + private readonly Type _serviceType; internal object DefaultValue { get; } public ConstantCallSite(Type serviceType, object defaultValue): base(ResultCache.None) { + _serviceType = serviceType ?? throw new ArgumentNullException(nameof(serviceType)); if (defaultValue != null && !serviceType.IsInstanceOfType(defaultValue)) { throw new ArgumentException(SR.Format(SR.ConstantCantBeConvertedToServiceType, defaultValue.GetType(), serviceType)); @@ -19,8 +21,8 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup DefaultValue = defaultValue; } - public override Type ServiceType => DefaultValue.GetType(); - public override Type ImplementationType => DefaultValue.GetType(); + public override Type ServiceType => DefaultValue?.GetType() ?? _serviceType; + public override Type ImplementationType => DefaultValue?.GetType() ?? _serviceType; public override CallSiteKind Kind { get; } = CallSiteKind.Constant; } } diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/CallSiteTests.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/CallSiteTests.cs index 3dc3cec6c64..c83e27790b6 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/CallSiteTests.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/CallSiteTests.cs @@ -264,6 +264,37 @@ namespace Microsoft.Extensions.DependencyInjection.Tests Assert.NotNull(compileCallSite); } + [Theory] + [InlineData(ServiceProviderMode.Default)] + [InlineData(ServiceProviderMode.Dynamic)] + [InlineData(ServiceProviderMode.Runtime)] + [InlineData(ServiceProviderMode.Expressions)] + [InlineData(ServiceProviderMode.ILEmit)] + private void NoServiceCallsite_DefaultValueNull_DoesNotThrow(ServiceProviderMode mode) + { + var descriptors = new ServiceCollection(); + descriptors.AddTransient<ServiceG>(); + + var provider = descriptors.BuildServiceProvider(mode); + ServiceF instance = ActivatorUtilities.CreateInstance<ServiceF>(provider); + + Assert.NotNull(instance); + } + + private interface IServiceG + { + } + + private class ServiceG + { + public ServiceG(IServiceG service = null) { } + } + + private class ServiceF + { + public ServiceF(ServiceG service) { } + } + private class ServiceD { public ServiceD(IEnumerable<ServiceA> services) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderCompilationTest.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderCompilationTest.cs index 5c6853e1b1c..8094dde9364 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderCompilationTest.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderCompilationTest.cs @@ -12,6 +12,7 @@ namespace Microsoft.Extensions.DependencyInjection.Tests public class ServiceProviderCompilationTest { [Theory] + [InlineData(ServiceProviderMode.Default, typeof(I999))] [InlineData(ServiceProviderMode.Dynamic, typeof(I999))] [InlineData(ServiceProviderMode.Runtime, typeof(I999))] [InlineData(ServiceProviderMode.ILEmit, typeof(I999))] diff --git a/src/libraries/libraries-packages.proj b/src/libraries/libraries-packages.proj index 7efda346d55..937de6e822f 100644 --- a/src/libraries/libraries-packages.proj +++ b/src/libraries/libraries-packages.proj @@ -19,6 +19,7 @@ <ProjectReference Include="$(MSBuildThisFileDirectory)*\pkg\**\*.pkgproj" Condition="('$(BuildAllConfigurations)' == 'true' or '$(DotNetBuildFromSource)' == 'true') And '$(BuildAllOOBPackages)' == 'true'" /> <!-- If setting BuildAllOOBPackages to false, add bellow the individual OOB packages you want to continue to build --> <ProjectReference Include="$(LibrariesProjectRoot)\System.IO.Pipelines\pkg\System.IO.Pipelines.pkgproj" Condition="'$(BuildAllConfigurations)' == 'true'" /> + <ProjectReference Include="$(LibrariesProjectRoot)\Microsoft.Extensions.DependencyInjection\pkg\Microsoft.Extensions.DependencyInjection.pkgproj" Condition="'$(BuildAllConfigurations)' == 'true'" /> <ProjectReference Include="$(LibrariesProjectRoot)\System.Security.Cryptography.Pkcs\pkg\System.Security.Cryptography.Pkcs.pkgproj" Condition="'$(BuildAllConfigurations)' == 'true'" /> </ItemGroup> diff --git a/src/libraries/pkg/baseline/packageIndex.json b/src/libraries/pkg/baseline/packageIndex.json index 5e006066e71..f44f9d46dc3 100644 --- a/src/libraries/pkg/baseline/packageIndex.json +++ b/src/libraries/pkg/baseline/packageIndex.json @@ -507,11 +507,20 @@ "3.1.0", "3.1.1", "3.1.2", + "3.1.3", + "3.1.4", + "3.1.5", + "3.1.6", + "3.1.7", + "3.1.8", + "3.1.9", + "3.1.10", "5.0.0" ], "InboxOn": {}, "AssemblyVersionInPackageVersion": { - "5.0.0.0": "5.0.0" + "5.0.0.0": "5.0.0", + "5.0.0.1": "5.0.1" } }, "Microsoft.Extensions.DependencyInjection.Abstractions": { |