1

I am refractoring an .NET C# application to the latest MS MVVM Toolkit. MS suggests the refit as a goto library to interact with REST API. However, I would like to use AddHttpClient following Ioc (Inversion of control) pattern. Below is the sample code I would like to refractor however it throws an error:

Ioc.Default.ConfigureServices(
    new ServiceCollection()
    //Services
    .AddSingleton<ISettingsService, SettingsService>()
    // Change below line to AddHttpClient
    .AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
    //ViewModels
    .AddTransient<PostWidgetViewModel>()
    //I would like to do below but it throws error
    .AddHttpClient<IRedditService>()
    .BuildServiceProvider());

So the question is how to register AddHttpClient in Ioc.Default.ConfigureServices?

Error

'IHttpClientBuilder' does not contain a definition for 'BuildServiceProvider' and the best extension method

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Jim
  • 2,760
  • 8
  • 42
  • 66

1 Answers1

2

First populate the collection

var collection = new ServiceCollection();
collection.AddSingleton<ISettingsService, SettingsService>();
collection.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"));
collection.AddTransient<PostWidgetViewModel>();
collection.AddHttpClient<IRedditService>();

then you should call the BuildServiceProvider method on the ServiceCollection

Ioc.Default.ConfigureServices(collection.BuildServiceProvider());
Steven
  • 166,672
  • 24
  • 332
  • 435
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • I have tried this already (see above) ... but I am getting the stated error. – Jim Oct 12 '22 at 11:19
  • @Jim You can't get the same error, since my suggested solution calls the `BuildServiceProvider` method on the `IServiceCollection` against which this method is [defined](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectioncontainerbuilderextensions.buildserviceprovider?view=dotnet-plat-ext-6.0). – Peter Csala Oct 12 '22 at 11:29
  • 1
    @Jim Since the `AddHttpClient` returns an `IHttpClientBuilder` implementation to be able to call methods like `AddPolicyHandler` that's why you can't call the `BuildServiceProvider` on that. – Peter Csala Oct 12 '22 at 11:30
  • I guess the question remains .... how to properly register AddHttpClient – Jim Oct 12 '22 at 12:13
  • @Jim what do you mean by *properly*? – Peter Csala Oct 12 '22 at 15:47
  • I mean that the suggested answer/snippet does not work. Therefore I my question in the original post remains i.e. how to register AddHttpClient in Ioc.Default.ConfigureServices if that possible at all .. if not then what are the alternatives – Jim Oct 12 '22 at 19:41
  • @Jim Could you please elaborate what do you mean by not working? Compile time error? Runtime error? Or what? Could you please be a more specific? – Peter Csala Oct 12 '22 at 19:56
  • the error description is stated in the question: 'IHttpClientBuilder' does not contain a definition for 'BuildServiceProvider' and the best extension method – Jim Oct 14 '22 at 21:21
  • @Jim With my proposed solution you can not receive this error. In my solution the BuildServiceProvider method call is not chained after the AddHttpClient rather it is called directly on the collection variable. – Peter Csala Oct 15 '22 at 05:26
  • 1
    it did actually worked.... it had to be written exactly as Ioc.Default.ConfigureServices(collection.BuildServiceProvider()); no other way which is really really weird. – Jim Oct 17 '22 at 19:24