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

how to use httpclientFactory in desktop client app? #820

Open
softboy99 opened this issue Jan 7, 2020 · 1 comment
Open

how to use httpclientFactory in desktop client app? #820

softboy99 opened this issue Jan 7, 2020 · 1 comment
Labels

Comments

@softboy99
Copy link

softboy99 commented Jan 7, 2020

//Microsoft.Extensions.Http和Microsoft.Extensions.DependencyInjection
ServiceCollection services = new ServiceCollection(); services.AddTransient(typeof(IDisposable), typeof(AuthenticatedParameterizedHttpClientHandler)); services.AddRefitClient<IApiService>(settings) .ConfigureHttpClient(c => { c.BaseAddress = new Uri("http://127.0.0.1/:8080" + "/iLab/"); c.DefaultRequestHeaders.Add("Content-Type", "application/json; charset=UTF-8"); }) .AddHttpMessageHandler<AuthenticatedParameterizedHttpClientHandler>() .SetHandlerLifetime(TimeSpan.FromMinutes(2)); var serviceProvider = services.BuildServiceProvider(); object httpClientFactory = serviceProvider.GetRequiredService(typeof(IApiService));

on the above last statement, the exception is thrown. says no type for 'AuthenticatedParameterizedHttpClientHandler' registered

@StephenCleary
Copy link

Your formatted code is:

ServiceCollection services = new ServiceCollection();
services.AddTransient(typeof(IDisposable), typeof(AuthenticatedParameterizedHttpClientHandler));
services.AddRefitClient<IApiService>(settings)
  .ConfigureHttpClient(c =>
  {
    c.BaseAddress = new Uri("http://127.0.0.1/:8080" + "/iLab/");
    c.DefaultRequestHeaders.Add("Content-Type", "application/json; charset=UTF-8");
  })
  .AddHttpMessageHandler<AuthenticatedParameterizedHttpClientHandler>()
  .SetHandlerLifetime(TimeSpan.FromMinutes(2));
var serviceProvider = services.BuildServiceProvider();
object httpClientFactory = serviceProvider.GetRequiredService(typeof(IApiService));

and as the exception message indicates, your problem is this line:

services.AddTransient(typeof(IDisposable), typeof(AuthenticatedParameterizedHttpClientHandler));

which should be:

services.AddTransient<AuthenticatedParameterizedHttpClientHandler>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants