Skip to content

Commit 0668d6e

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 9715f5d commit 0668d6e

1 file changed

Lines changed: 10 additions & 8 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
{

0 commit comments

Comments
 (0)