question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Losing RegionAdapter mappings using Microsoft.DI extensions...

See original GitHub issue

We can get constructor injection working with the following.

protected override IContainerExtension CreateContainerExtension() => PrismContainerExtension.Init();

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry = containerRegistry.RegisterServices(svc =>
    {
        MyApplication.DependencyInjection.AddApplication(svc);
    });
}

The IRegionAdapter for the type System.Windows.Controls.ContentControl is not registered

Prism.Regions.UpdateRegionsException: 'An exception occurred while trying to create region objects. 
    - The most likely causing exception was: 'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'SidePanelRegion'. The exception was: System.Collections.Generic.KeyNotFoundException: The IRegionAdapter for the type System.Windows.Controls.ContentControl is not registered in the region adapter mappings. You can register an IRegionAdapter for this control by overriding the ConfigureRegionAdapterMappings method in the bootstrapper.
   at Prism.Regions.RegionAdapterMappings.GetMapping(Type controlType)
   at Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName). 
 ---> System.Collections.Generic.KeyNotFoundException: The IRegionAdapter for the type System.Windows.Controls.ContentControl is not registered in the region adapter mappings. You can register an IRegionAdapter for this control by overriding the ConfigureRegionAdapterMappings method in the bootstrapper.
   at Prism.Regions.RegionAdapterMappings.GetMapping(Type controlType)
   at Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName)
   --- End of inner exception stack trace ---
   at Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName)
   at Prism.Regions.Behaviors.DelayedRegionCreationBehavior.TryCreateRegion()
   at Prism.Regions.Behaviors.DelayedRegionCreationBehavior.OnUpdatingRegions(Object sender, EventArgs e)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Prism.Events.WeakDelegatesManager.Raise(Object[] args)
   at Prism.Regions.RegionManager.UpdateRegions()'.
    But also check the InnerExceptions for more detail or call .GetRootException(). '

After overriding the ConfigureRegionAdapterMappings we can see the mappings there after invoking the base method.

Not sure where these get lost? The app works as it should using Unity.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Karnahcommented, Apr 18, 2021

@hippieZhou, it seems to me, that it doesn’t work at all. Probably, it’s better to use another container.

0reactions
nivlecommented, Apr 28, 2023

Instead of using this package, since it doesn’t work…, I just wrote some static functions, where I can register services… Basicly I make use of the generic container to register the services, and then I copy them over to prism container.

public static void RegisterServices(this IContainerRegistry containerRegistry)
{
    IServiceCollection serviceCollection = null;

    IHost appHost = Host.CreateDefaultBuilder().ConfigureServices((hostContext, services) =>
    {
        services.ConfigureServices();
        serviceCollection = services;
    }).Build();

    RegisterServices(containerRegistry, serviceCollection, appHost.Services);
}

private static void RegisterServices(IContainerRegistry containerRegistry, IServiceCollection services, IServiceProvider serviceProvider)
{
    foreach (ServiceDescriptor service in services)
    {
        switch (service.Lifetime)
        {
            case ServiceLifetime.Singleton:
                if (service.ImplementationType != null)
                {
                    containerRegistry.RegisterSingleton(service.ServiceType, service.ImplementationType);
                }
                else if (service.ImplementationInstance != null)
                {
                    containerRegistry.RegisterInstance(service.ServiceType, service.ImplementationInstance);
                }
                else if (service.ImplementationFactory != null)
                {
                    containerRegistry.RegisterSingleton(service.ServiceType, () => service.ImplementationFactory(serviceProvider));
                }
                break;
            case ServiceLifetime.Scoped:
                if (service.ImplementationType != null)
                {
                    containerRegistry.RegisterScoped(service.ServiceType, service.ImplementationType);
                }
                else if (service.ImplementationFactory != null)
                {
                    containerRegistry.RegisterScoped(service.ServiceType, () => service.ImplementationFactory(serviceProvider));
                }
                else
                {
                    containerRegistry.RegisterScoped(service.ServiceType);
                }
                break;
            case ServiceLifetime.Transient:
                if (service.ImplementationType != null)
                {
                    containerRegistry.Register(service.ServiceType, service.ImplementationType);
                }
                else if (service.ImplementationFactory != null)
                {
                    containerRegistry.Register(service.ServiceType, () => service.ImplementationFactory(serviceProvider));
                }
                break;
            default:
                break;
        }
    }
}

public static void ConfigureServices(this IServiceCollection services)
{
    //add here your services
    services.AddHttpClient();
}

Followed by using it in app.xaml.cs …

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterServices();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

No regions in regionmanager using Microsoft Dependency ...
I am using the Microsoft Dependency Injection for DI. I don't want use Unity container. regionAdapterMappings is empty. View discovery and view ...
Read more >
Untitled
Prism.Regions.RegionAdapterMappings.html Create a Custom Prism RegionAdapter - Brian Lagunas WebNamespace: Microsoft.Practices.Prism.MefExtensions.
Read more >
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
Read more >
Essential .NET - Dependency Injection with .NET Core
In this article. Why Dependency Injection? .NET Core Microsoft.Extensions.DependencyInjection; Service Lifetime; Dependency Injection for the DI Implementation ...
Read more >
13.4 Dependency Injection Support - OData
As we internally maintain a dictionary to map the route name to its corresponding root container (1-1 mapping), multiple OData routes (i.e., ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found