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]: Refit.IRequestBuilder Dependency Injection Throws Exception #1583

Open
HamidMusayev opened this issue Oct 20, 2023 · 1 comment
Open
Labels

Comments

@HamidMusayev
Copy link

HamidMusayev commented Oct 20, 2023

Refit.IRequestBuilder dependency injection exception 🐞

Interface

public interface IPetStoreClient
{
    [Post("/store/order")]
    Task<Order> PlaceOrder([Body] Order body);

    [Get("/store/order/{orderId}")]
    Task<Order> GetOrderById(long orderId);

    [Delete("/store/order/{orderId}")]
    Task DeleteOrder(long orderId);
}

Exception
An unhandled exception of type 'System.AggregateException' occurred in Microsoft.Extensions.DependencyInjection.dll: 'Some services are not able to be constructed'
Inner exceptions found, see $exception in variables window for more details.
Innermost exception System.InvalidOperationException : Unable to resolve service for type 'Refit.IRequestBuilder' while attempting to activate 'Refit.Implementation.Generated+BLLExternalClientsIPetStoreClient'.

Injection

builder.Services
            .AddRefitClient<IPetStoreClient>()
            .ConfigureHttpClient(c => c.BaseAddress = new Uri(config.PetStoreClientSettings.BaseUrl));

Usage

public class HelperController : Controller
{
    private readonly IPetStoreClient _petStoreClient;

    public HelperController(IPetStoreClient petStoreClient)
    {
        _petStoreClient = petStoreClient;
    }

    [HttpGet("test")]
    public async Task<IActionResult> Get()
    {
        var response = await _petStoreClient.GetOrderById(24);
        return Ok(response);
    }
}

Note 1
Same error when creating inside constructor

public class TestController : Controller
{
  private readonly IPetStoreClient _petStoreClient;

  public HelperController(ConfigSettings configSettings)
  {
      _petStoreClient = RestService.For<IPetStoreClient>(configSettings.PetStoreClientSettings.BaseUrl);
  }

Note 2
Same error when creating instance

[HttpGet("test")]
    public async Task<IActionResult> Get()
    {
        var client = RestService.For<IPetStoreClient>(_configSettings.PetStoreClientSettings.BaseUrl);
        var response = await client.GetOrderById(24);

        return Ok(response);
    }

Step to reproduce

Refit.HttpClientFactory v7.0.0
Refit v7.0.0

Reproduction repository

https://github.com/reactiveui/refit

Expected behavior

Dependency injection IRequestBuilder exception

Screenshots 🖼️

No response

IDE

Visual Studio 2022, Rider Windows, Visual Studio Code

Operating system

Windows

Version

No response

Device

No response

Refit Version

7.0.0

Additional information ℹ️

null

@HamidMusayev HamidMusayev changed the title [Bug]: Dependency Injection Throws Exception [Bug]: Refit.IRequestBuilder Dependency Injection Throws Exception Oct 20, 2023
@HamidMusayev
Copy link
Author

HamidMusayev commented Nov 7, 2023

Update: Moving refit interfaces to another project inside solution fixes the bug

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

1 participant