2 years ago

#56678

test-img

user16023203

inject IHttpClientFactory in WPF .net 6 application

I want to inject IHttpClientFactory in WPF .net 6 application using Prism (ver 8.1.97) (container I use is Unity), but the following doesn't work. It wouldn't start WPF interface. It just crashes.

What am I doing wrong?

app.xaml.cs - adds httpclient via PrismContainerExtension (ver 8.0.62)


    protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            PrismContainerExtension.Current.RegisterServices(s =>
            {
                s.AddHttpClient("test.api", c=>
                {
                    c.BaseAddress = new Uri("http://test.com/api");
                });

            });
        }

viewmodel

    public MainWindowViewModel(IHttpClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }

Error I get is the following

Prism.Ioc.ContainerResolutionException
  HResult=0x80131500
  Message=An unexpected error occurred while resolving 'TestApp.MainWindow'
  Source=Prism.Unity.Wpf
  StackTrace:
   at Prism.Unity.UnityContainerExtension.Resolve(Type type, ValueTuple`2[] parameters)
   at Prism.Unity.UnityContainerExtension.Resolve(Type type)
   at Prism.Ioc.IContainerProviderExtensions.Resolve[T](IContainerProvider provider)
   at PrinterLabelApp.App.CreateShell() in C:\Users\TestApp\PrinterLabelApp\App.xaml.cs:line 33
   at Prism.PrismApplicationBase.Initialize()
   at Prism.PrismApplicationBase.InitializeInternal()
   at Prism.PrismApplicationBase.OnStartup(StartupEventArgs e)
   at System.Windows.Application.<.ctor>b__1_0(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

Inner Exception 1:
ContainerResolutionException: An unexpected error occurred while resolving 'System.IServiceProvider'

Inner Exception 2:
ResolutionFailedException: Resolution failed with error: No public constructor is available for type System.IServiceProvider.

For more detailed information run Unity in debug mode: new UnityContainer().AddExtension(new Diagnostic())

Inner Exception 3:
InvalidOperationException: No public constructor is available for type System.IServiceProvider.

Inner Exception 4:
InvalidRegistrationException: Exception_WasThrown


prism

0 Answers

Your Answer

Accepted video resources