1

Previously, I have been fine by just using the implicitly injected HttpClient in my Xamarin.Forms typed clients app like below. Now I want to use the .AddHttpClient():

 public HrService(HttpClient httpClient)
        {
            // Stuff
        }

And this from app.xaml.cs

containerRegistry.RegisterSingleton<IHrService, HrService>();

However, I now find myself in a situation, where I need to setup my HttpClient from the App.Xaml.cs file, because I need to add a specific HttpMessageHandler.

I have no luck doing the usual .AddHttpClient(), as it simply doesn't resolve. I am using the following nuget package:

  • Prism.DryIoC.Forms

I stumpled across this SO: DryIoc and IServiceProvider on Prism for Xamarin.Forms (DryIoc.Microsoft.DependencyInjection) but it didn't make a difference.

Any help is much appreciated.

Jesper Plantener
  • 229
  • 3
  • 16
  • What doesn't make a difference? Why didn't you install `Prism.Forms.Extended` and `Prism.DryIoc.Extensions` packages, too, as stated in the linked post? – Haukinger Nov 23 '21 at 07:48
  • @Haukinger I did try that, but that does not give me the option to .AddHttpClient(). Maybe it was removed since, I dont know. But it is not available anymore. – Jesper Plantener Nov 23 '21 at 09:51
  • This doesn't look like "was removed" to me: https://github.com/dansiegel/Prism.Container.Extensions/blob/044921bef4426e42672ed4524d8e4d339b87fcdd/src/Prism.Container.Extensions/IServiceCollectionExtensions.cs#L19 Perhaps you post your `using` statements? – Haukinger Nov 23 '21 at 11:42
  • @Haukinger I have the "RegisterServices" function, but the IServiceCollection does not contain ".AddHttpClient()" – Jesper Plantener Nov 23 '21 at 15:08
  • 2
    Of course not, it's an [extension method](https://github.com/dotnet/runtime/blob/57bfe474518ab5b7cfe6bf7424a79ce3af9d6657/src/libraries/Microsoft.Extensions.Http/ref/Microsoft.Extensions.Http.cs#L27), too. Just add the `Microsoft.Extensions.Http` nuget and `using Microsoft.Extensions.DependencyInjection;` – Haukinger Nov 23 '21 at 16:46
  • Also, installing `Resharper` might be a good idea if you don't like learning all those extension method namespaces by heart :-) – Haukinger Nov 23 '21 at 16:48
  • @Haukinger I see! Thank you for getting me back on track here. Fun thing though, I am using Rider, so I would have expected it to point me in the right direction regarding those dependencies. – Jesper Plantener Nov 23 '21 at 20:54

0 Answers0