Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject HttpClient get Error: Method not found: SocketsHttpHandler.get_MeterFactory() #112634

Closed
dongfo opened this issue Feb 17, 2025 · 3 comments
Labels
area-Extensions-DependencyInjection needs-author-action An issue or pull request that requires more info or actions from the author.

Comments

@dongfo
Copy link

dongfo commented Feb 17, 2025

Description

GetRequiredService of class which inject IHttpClientFactory ,will get error
Method not found: 'System.Diagnostics.Metrics.IMeterFactory System.Net.Http.SocketsHttpHandler.get_MeterFactory()'.
at Microsoft.Extensions.Http.MetricsFactoryHttpMessageHandlerFilter.<>c__DisplayClass2_0.b__0(HttpMessageHandlerBuilder builder)
at Microsoft.Extensions.Http.LoggingHttpMessageHandlerBuilderFilter.<>c__DisplayClass6_0.b__0(HttpMessageHandlerBuilder builder)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandlerEntry(String name)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.<>c__DisplayClass14_0.<.ctor>b__2()
at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy1.CreateValue() at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandler(String name) at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(String name) at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTransientHelper[TClient,TImplementation](IServiceProvider s, IHttpClientBuilder builder) at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.<>c__DisplayClass16_02.b__0(IServiceProvider s)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)

Reproduction Steps

add nuget package
Microsoft.Extensions.DependencyInjection 9.0.2
Microsoft.Extensions.DependencyInjection.Abstractions 9.0.2
Microsoft.Extensions.Http 9.0.2
Microsoft.Extensions.Diagnostics 9.0.2

void Main()
{
	var services = new ServiceCollection();
	services.AddHttpClient<FooAppService>();
	
	var provider = services.BuildServiceProvider();

	using var scope = provider.CreateScope();
	var sp = scope.ServiceProvider;
	sp.GetRequiredService<FooAppService>();
}

public class FooAppService { 
	private HttpClient _httpClient;
	public FooAppService( HttpClient httpClient){
		_httpClient=httpClient;
	}
}

Expected behavior

normal use of HttpClient

Actual behavior

error to inject HttpClient

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Feb 17, 2025
@dongfo dongfo changed the title IHttpClientFactory Method not found SocketsHttpHandler.get_MeterFactory() Inject HttpClient get Error: Method not found: SocketsHttpHandler.get_MeterFactory() Feb 17, 2025
@steveharter
Copy link
Member

I was not able to repro this issue on v9. I used a console app with package references to Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Http. Then I added System.Diagnostics.DiagnosticSource without an issue.

I was successful with a .Net Framework 4.81 console app.

Since the MeterFactory property was added in v8 I suspect that an older version is being loaded somehow perhaps due to direct assembly reference that overrides the transitive package dependency.

@steveharter steveharter added the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 18, 2025
Copy link
Contributor

This issue has been marked needs-author-action and may be missing some important information.

@steveharter steveharter removed the untriaged New issue has not been triaged by the area owner label Feb 18, 2025
@dongfo
Copy link
Author

dongfo commented Feb 20, 2025

ok,I find it's because proj ues net8 sdk and dotnet9 nuget.

@dongfo dongfo closed this as completed Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-DependencyInjection needs-author-action An issue or pull request that requires more info or actions from the author.
Projects
None yet
Development

No branches or pull requests

2 participants