Using IHttpClientFactory in Prism

709 views Asked by At

I would like to use IHttpClientFactory in a WPF application built using the Prism library. I have added two packages, Prism.Unity and Prism.Unity.Extensions and i attempt to register the IHttpClientFactory with the following code:

containerRegistry.RegisterServices(s => s.AddHttpClient("DefaultClient")
                                         .AddPolicyHandler(GetRetryPolicy()));

However, I get the following error:

Type load exception for CreateScope in Prism.Unity.ContainerExtension.

I have also added the following override:

protected override IContainerExtension CreateContainerExtension() => PrismContainerExtension.Current;
1

There are 1 answers

0
thatguy On BEST ANSWER

Prism 8 introduces registration methods for scoped services. The IContainerProvider interface declares an additional method CreateScope and a member CurrentScope that were not present in earlier versions.

public interface IContainerProvider {
   // ...other members.

   IScopedProvider CreateScope();

   ScopedProvider CurrentScope { get; }
}

The PrismContainerExtension classes from Prism.Unity.Extensions in version 7.2.0.1054 and earlier do not implement this method. Consequently, you get an exception using it with Prism 8.

Since the types are incompatible, you either have to revert to Prism 7.2.0.1054 or you have to wait for a new version of Prism.Unity.Extensions that supports the new scoped services in Prism 8. There are already changes made in the repository, but a preview version is currently only available to sponsors.