Skip to content

Commit 343e5e9

Browse files
committed
Fix the servieprovider missing method exception on .net framework whe… (#2714)
* Fix the servieprovider missing method exception on .net framework when using DI 6.0 * Remove the try catch in the private method
1 parent 66e6ab0 commit 343e5e9

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/Microsoft.AspNet.OData.Shared/DefaultContainerBuilder.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,13 @@ public virtual IServiceProvider BuildContainer()
8585
{
8686
try
8787
{
88-
return services.BuildServiceProvider();
88+
// On .NET Framework platform, if using Microsoft.Extensions.DependencyInjection > 6.x version,
89+
// The runtime throws exception directly when calling BuildContainer if we call 'services.BuildServiceProvider()' within it.
90+
// So, wrap 'services.BuildServiceProvider()' into a private method call, we can catch the runtime exception directly
91+
return BuildServiceProviderImpl();
8992
}
90-
catch (MissingMethodException)
93+
catch
9194
{
92-
/* "services.BuildServiceProvider()" returns IServiceProvider in Microsoft.Extensions.DependencyInjection 1.0 and ServiceProvider in Microsoft.Extensions.DependencyInjection 2.0
93-
* * (This is a breaking change)[https://github.com/aspnet/DependencyInjection/issues/550].
94-
* To support both versions with the same code base in OData/WebAPI we decided to call that extension method using reflection.
95-
* More info at https://github.com/OData/WebApi/pull/1082
96-
*/
97-
9895
MethodInfo buildServiceProviderMethod =
9996
typeof(ServiceCollectionContainerBuilderExtensions)
10097
.GetMethod(nameof(ServiceCollectionContainerBuilderExtensions.BuildServiceProvider), new[] { typeof(IServiceCollection) });
@@ -103,6 +100,11 @@ public virtual IServiceProvider BuildContainer()
103100
}
104101
}
105102

103+
private IServiceProvider BuildServiceProviderImpl()
104+
{
105+
return services.BuildServiceProvider();
106+
}
107+
106108
private static Microsoft.Extensions.DependencyInjection.ServiceLifetime TranslateServiceLifetime(
107109
Microsoft.OData.ServiceLifetime lifetime)
108110
{

tools/WebStack.versions.settings.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<VersionMajor Condition="'$(VersionMajor)' == ''">7</VersionMajor>
77
<VersionMinor Condition="'$(VersionMinor)' == ''">5</VersionMinor>
8-
<VersionBuild Condition="'$(VersionBuild)' == ''">17</VersionBuild>
8+
<VersionBuild Condition="'$(VersionBuild)' == ''">18</VersionBuild>
99
<VersionRelease Condition="'$(VersionRelease)' == ''"></VersionRelease>
1010
</PropertyGroup>
1111

0 commit comments

Comments
 (0)