Use of AppDomain.CreateDomain() detected in assembly: Ninject,.NET 6 will only support a single AppDomain.

wire_jp 216 Reputation points
2022-04-23T02:40:29.723+00:00

Hello,

I am creating a Xamarin Forms app, and my code is below: -

using Ninject;
using System.Collections.Generic;
using InventoryTrackerApp.Infrastructure.Storage;
using InventoryTrackerApp.Models.Storage;
using InventoryTrackerApp.ViewModels.Storage;

namespace InventoryTrackerApp.IoC
{
    class ServiceLocator
    {
        public static IKernel Kernel { get; private set; } = new StandardKernel();

        public static void Setup()
        {
            Kernel.Bind<IIOService<List<City>>>().To<JsonService>();
        }

        public static T Get<T>()
        {
            return Kernel.Get<T>();
        }

        public MainViewModel MainViewModel
        {
            get => Kernel.Get<MainViewModel>();
        }
    }
}

When I tried to build the solution, the emulator will start loading but then it will suddenly stopped and disappear. Afterwards I will receive a deployment error message: -

Use of AppDomain.CreateDomain() detected in assembly: Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7. .NET 6 will only support a single AppDomain, so this API will no longer be available in Xamarin.Android once .NET 6 is released.

I updated my code and when I build a solution, the emulator is still stopping suddenly and restarting again, and then it will show a failed deployment error message.

I reckon that the issue with the emulator stopping and restarting was due to some conflict between different versions of some nuget packages and so I write some code for the AssemblyLoadContext below, the code is throwing an error message at line 18, where isCollectible is underlined in red:-

using System.IO;
 using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Runtime.Loader;
 using InventoryTrackerApp.IoC;

 namespace InventoryTrackerApp.Models.Host
 {
     // This is a collectible (unloadable) AssemblyLoadContext that loads the dependencies
     // of the plugin from the plugin's binary directory.
     public class HostAssemblyLoadContext : AssemblyLoadContext
     {
         public bool isCollectible { get; }
         // Resolver of the locations of the assemblies that are dependencies of the
         // main plugin assembly.
         private AssemblyDependencyResolver _resolver;

         public HostAssemblyLoadContext(string pluginPath) : base(isCollectible: true) // CS1739 the best overload for .'ctor' does not have a parameter named 'isCollectible'
         {
             _resolver = new AssemblyDependencyResolver(pluginPath);
         }

         // The Load method override causes all the dependencies present in the plugin's binary directory to get loaded
         // into the HostAssemblyLoadContext together with the plugin assembly itself.
         // NOTE: The Interface assembly must not be present in the plugin's binary directory, otherwise we would
         // end up with the assembly being loaded twice. Once in the default context and once in the HostAssemblyLoadContext.
         // The types present on the host and plugin side would then not match even though they would have the same names.
         protected override Assembly Load(AssemblyName name)
         {
             string assemblyPath = (string)_resolver.ResolveAssemblyToPath(name);
             if (assemblyPath != null)
             {
                 Console.WriteLine($"Loading assembly {assemblyPath} into the HostAssemblyLoadContext");
                 return LoadFromAssemblyPath(assemblyPath);
             }

             return null;
         }

         internal void Unload()
         {
             throw new NotImplementedException();
         }
     }
Developer technologies | .NET | Xamarin
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.