question

wirejp-9293 avatar image
0 Votes"
wirejp-9293 asked wirejp-9293 commented

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

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();
          }
      }



dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your warning about AppDomains not being available in .Net 6 is probably not relevant to the Xamarin app crashing on launch, since Xamarin doesn't run on .Net 6. You'll need to solve the NInject problem before moving to MAUI, which does run on .Net 6, but the error quoted shouldn't be relevant to you yet.

That error looks like it comes from AppDomain code inside NInject, so it probably either needs to be fixed in NInject (check for a newer version or if there are options to avoid it in your current version), or you'll need to replace NInject with a different DI framework. I suspect that's what you're trying to do in your answer, but without explanation it's not clear what exactly you're trying to do here.

AssemblyLoadContext isn't a direct replacement for NInject. It's a conceptual replacement for the AppDomains used inside of NInject. You cannot just swap it in for NInject's kernel.


0 Votes 0 ·

Hi RobCaplan,

Thank you for your feedback. The Ninject version which I had used in my project was the latest version. I will replace the Ninject package with another DI framework such as .Net Core DI. Thanks

0 Votes 0 ·

0 Answers