-
-
Notifications
You must be signed in to change notification settings - Fork 748
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
[BUG] AddRefitClient non-generic version does not register service #745
Comments
It looks like a bug. You can do PR with tests.
|
I also got this problem.here is my solution.
public static IHttpClientBuilder AddTypedClient(this IHttpClientBuilder builder, Type type, Func<HttpClient, IServiceProvider, object> factory)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}
builder.Services.AddTransient(type, s =>
{
var httpClientFactory = s.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient(builder.Name);
return factory(httpClient, s);
});
return builder;
}
public static IHttpClientBuilder AddRefitClient(this IServiceCollection services, Type refitInterfaceType, RefitSettings settings = null)
{
return services.AddHttpClient(UniqueName.ForType(refitInterfaceType))
.AddTypedClient(refitInterfaceType, (client, serviceProvider) => RestService.For(refitInterfaceType, client, settings));
} |
We had a similar problem, refit tried to register all the typeclient under the same name, so the DI container was throwing exception (this type is already registred...). We created this workaround : public static class IServiceCollectionExtension
{
public static IServiceCollection AddRestServiceClient(this IServiceCollection services, Type type, Action<IServiceProvider, HttpClient> clientFactory)
{
services.AddHttpClient(type.FullName, clientFactory)
.Services.AddTransient(type, services =>
{
var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient(type.FullName);
return Refit.RestService.For(type, httpClient);
});
return services;
}
public static IServiceCollection AddRestServiceClient(this IServiceCollection services, Type type, Action<HttpClient> clientFactory)
{
return AddRestServiceClient(services, type, (serviceProvider, client) => clientFactory(client));
}
} I think the problem seem to be around the |
+1 me too. |
is it released? |
Closing due to age. Please try Refit v6 and reopen if still an issue. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Describe the bug
When registering the refit client with the
HttpClientFactoryExtensions
using the method overload that takes in a runtime type (i.e.services.AddRefitClient(myInterfaceType, settings)
), the service is not registered as it is with the generic version. This is due to it missing the lineservices.AddSingleton(provider => RequestBuilder.ForType(refitInterfaceType, settings));
similar to the way it does it in the first line of the generic method, located here.Steps To Reproduce
IMyApi
Startup.ConfigureServices
, get aType
for your interface type and call the broken AddRefitClient method:InvalidOperationException: Unable to resolve service for type 'IMyApi' while attempting to activate 'test_api.Controllers.ValuesController'.
services.AddRefitClient<IMyApi>();
Expected behavior
It is expected that adding the refit client with the runtime type will act the same as adding it with the generic type.
Environment
The text was updated successfully, but these errors were encountered: