© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
11 replies
sir loin

❔ Migrate AppDomain (.net framework) to AssemblyLoadContext (.net core)

I need help converting this code to the .net core equivalent
by using AppDomain here, ensured that the code is running in their own domains. However, by migrating the app to .net core (for MANY reasons), AppDomain becomes unusable. I've heard that using AssemblyLoadContext is the best (native) alternative. How could I migrate properly?

var setup = new AppDomainSetup
{
    ApplicationName = $"{Name} {Id}",
    ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
    DisallowCodeDownload = true,
    DisallowPublisherPolicy = true,
    DisallowBindingRedirects = true
};

var scriptDomain = AppDomain.CreateDomain(setup.ApplicationName, null, setup, new PermissionSet(PermissionState.Unrestricted));

ScriptProvider<TScript> scriptProvider;
try
{
    var scriptProviderHandle = Activator.CreateInstanceFrom(scriptDomain,
        typeof(ScriptProvider<TScript>).Assembly.ManifestModule.FullyQualifiedName,
        typeof(ScriptProvider<TScript>).FullName);

    scriptProvider = (ScriptProvider<TScript>)scriptProviderHandle.Unwrap();
    scriptProvider.Initialize(assemblyPath, ScriptTypeName);
}
catch
{
    AppDomain.Unload(scriptDomain);
    throw;
}

if (appDomain != null)
{
    Debug.Print($"{nameof(Scripting)}: Unloading domain {appDomain.FriendlyName}");
    AppDomain.Unload(appDomain);
}
appDomain = scriptDomain;

return scriptProvider;
var setup = new AppDomainSetup
{
    ApplicationName = $"{Name} {Id}",
    ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
    DisallowCodeDownload = true,
    DisallowPublisherPolicy = true,
    DisallowBindingRedirects = true
};

var scriptDomain = AppDomain.CreateDomain(setup.ApplicationName, null, setup, new PermissionSet(PermissionState.Unrestricted));

ScriptProvider<TScript> scriptProvider;
try
{
    var scriptProviderHandle = Activator.CreateInstanceFrom(scriptDomain,
        typeof(ScriptProvider<TScript>).Assembly.ManifestModule.FullyQualifiedName,
        typeof(ScriptProvider<TScript>).FullName);

    scriptProvider = (ScriptProvider<TScript>)scriptProviderHandle.Unwrap();
    scriptProvider.Initialize(assemblyPath, ScriptTypeName);
}
catch
{
    AppDomain.Unload(scriptDomain);
    throw;
}

if (appDomain != null)
{
    Debug.Print($"{nameof(Scripting)}: Unloading domain {appDomain.FriendlyName}");
    AppDomain.Unload(appDomain);
}
appDomain = scriptDomain;

return scriptProvider;
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Migrate .NET Framework 4.8 / .NET Core 5 to .NET 9 (.NET 10)
C#CC# / help
5mo ago
net core to net framework for some reason
C#CC# / help
4y ago
AssemblyLoadContext Error - .NET 8.0
C#CC# / help
2y ago
API integration issue in .NET core framework.
C#CC# / help
2y ago