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

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

Closed
aywee26 opened this issue Sep 17, 2024 · 13 comments
Closed

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

aywee26 opened this issue Sep 17, 2024 · 13 comments
Labels

Comments

@aywee26
Copy link

aywee26 commented Sep 17, 2024

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.

@aywee26 aywee26 added the bug label Sep 17, 2024
@freenetbrasil
Copy link

Same here; I had to downgrade to 7.1.2

@TimothyMakkison
Copy link
Contributor

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?

@ChrisPulman
Copy link
Member

image

@ChrisPulman
Copy link
Member

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

@ChrisPulman
Copy link
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?

@TimothyMakkison
Copy link
Contributor

TimothyMakkison commented Sep 18, 2024

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.

@ChrisPulman
Copy link
Member

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

@TimothyMakkison
Copy link
Contributor

TimothyMakkison commented Sep 18, 2024

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.

@flojon
Copy link

flojon commented Sep 18, 2024 via email

@ChrisPulman
Copy link
Member

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

@flojon
Copy link

flojon commented Sep 19, 2024

7.2.1 works good for me. Thanks!

@aywee26
Copy link
Author

aywee26 commented Sep 19, 2024

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.

Copy link

github-actions bot commented Oct 4, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants