Skip to content

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

Closed

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.

Activity

  1. freenetbrasil commented on Sep 18, 2024

    @freenetbrasil

    Same here; I had to downgrade to 7.1.2

  2. TimothyMakkison commented on Sep 18, 2024

    @TimothyMakkison
    Collaborator

    Thanks for the example repository, this looks pretty severe. I don't see any implementations of IBlogApi or even partial class Generated being created. Looks like the source generator isn't running.

    @ChrisPulman any idea what has changed since 7.1.2 to cause this?

  3. ChrisPulman commented on Sep 18, 2024

    @ChrisPulman
    Member

    image

  4. ChrisPulman commented on Sep 18, 2024

    @ChrisPulman
    Member

    This was run using VS2022 V17.11.3, sorry, I don't have Rider to test with.

  5. ChrisPulman commented on Sep 18, 2024

    @ChrisPulman
    Member

    Thanks for the example repository, this looks pretty severe. I don't see any implementations of IBlogApi or even partial class Generated being created. Looks like the source generator isn't running.

    @ChrisPulman any idea what has changed since 7.1.2 to cause this?

    @TimothyMakkison Do you have Rider? If so, does this issue occur when you run it?

  6. TimothyMakkison commented on Sep 18, 2024

    @TimothyMakkison
    Collaborator

    I think the issue is 29e0e1c. Looks like Microsoft.CodeAnalysis.CSharp was mistakenly updated.

    Do you have Rider? If so, does this issue occur when you run it?

    Yeah, I have rider and I get the same error message.

  7. ChrisPulman commented on Sep 18, 2024

    @ChrisPulman
    Member

    @TimothyMakkison Thank you Tim, strange that it only affects Rider.

  8. TimothyMakkison commented on Sep 18, 2024

    @TimothyMakkison
    Collaborator

    I'm guessing you are on the latest version of Visual studio where they support Microsoft.CodeAnalysis.CSharp 4.11.0. Rider might still be on 4.10.0 hence why nothing is generated.

  9. flojon commented on Sep 18, 2024

    @flojon
  10. ChrisPulman commented on Sep 19, 2024

    @ChrisPulman
    Member

    Please update to 7.2.1 this issue should be resolved, thank you

  11. flojon commented on Sep 19, 2024

    @flojon

    7.2.1 works good for me. Thanks!

  12. aywee26 commented on Sep 19, 2024

    @aywee26
    Author

    Weird development on the issue:

    Tried to test the issue of Refit 7.2.0 further. I had Visual Studio installed, not the most recent version, at least 3 months old. Tried to replicate the issue - success, same exception is thrown.

    So I updated the IDE via Visual Studio installer, to version 17.11.4. And now, whether I use VS or Rider, project builds, runs and the issue cannot be replicated.

    Tried to use different versions of Rider, installed via JetBrains Toolbox (specifically 2024.2.4, 2024.1.6 and 2023.3.6), couldn't replicate the issue.

    Anyway, Refit 7.2.1 also works fine, with no issues.

  13. github-actions commented on Oct 4, 2024

    @github-actions

    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.

  14. locked as resolved and limited conversation to collaborators on Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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