Skip to content

[Bug]: IExampleInterface doesn't look like a Refit interface #1817

@aywee26

Description

@aywee26

Describe the bug 🐞

The project is ASP.NET Core Web API that uses Refit packages (Refit 7.2.0, Refit.HttpClientFactory 7.2.0, Refit.Newtonsoft.Json 7.2.0). It contacts 3rd party resource JSONPlaceholder via HTTP.

The interface IBlogApi lists several methods, all of them are annotated by appropriate Refit attributes.

public interface IBlogApi
{
    [Get("/posts/{id}")]
    Task<Post> GetPostAsync(int id);

    [Get("/posts")]
    Task<List<Post>> GetPostsAsync();

    [Post("/posts")]
    Task<Post> CreatePostAsync([Body] Post post);

    [Put("/posts/{id}")]
    Task<Post> UpdatePostAsync(int id, [Body] Post post);

    [Delete("/posts/{id}")]
    Task DeletePostAsync(int id);

    [Get("/posts")]
    Task<List<Post>> GetPostsAsync([Query] PostQueryParameters parameters);
}

The project uses source-generated implementations of these methods to provide various routes. When "GET /posts/1" query is performed, the following exception is thrown:

System.InvalidOperationException: IBlogApi doesn't look like a Refit interface. Make sure it has at least one method with a Refit HTTP method attribute and Refit is installed in the project.
   at Refit.RestService.GetGeneratedType(Type refitInterfaceType) in c:\temp\releaser\refit\Refit\RestService.cs:line 188
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Refit.RestService.For(Type refitInterfaceType, HttpClient client, IRequestBuilder builder) in c:\temp\releaser\refit\Refit\RestService.cs:line 79
   at Refit.RestService.For[T](HttpClient client, IRequestBuilder`1 builder) in c:\temp\releaser\refit\Refit\RestService.cs:line 19
   at Refit.HttpClientFactoryExtensions.<>c__2`1.<AddRefitClient>b__2_3(HttpClient client, IServiceProvider serviceProvider) in c:\temp\releaser\refit\Refit.HttpClientFactory\HttpClientFactoryExtensions.cs:line 87
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.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.<RealizeService>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)
   at lambda_method2(Closure, Object, HttpContext)
   at Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

The result is similar for other queries and commands.

Step to reproduce

  1. Clone linked reproduction repository
  2. Launch the project in Debug mode
  3. Perform query: GET /posts/1
  4. Get error described

Reproduction repository

https://github.com/aywee26/refit-client-example

Expected behavior

The project should return OK response with a body similar to this:

{
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto",
  "userId": 1
}

Screenshots 🖼️

No response

IDE

Rider Windows

Operating system

Windows

Version

11 23H2

Device

No response

Refit Version

7.2.0

Additional information ℹ️

Current workaround: downgrade Refit packages from version 7.2.0 to 7.1.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions