I have a .NetCore 2 Azure Function (runtime 2) on a consumption plan that makes use of reflection, it loads an assembly in the current Domain (Assembly.LoadFrom()) and does other stuff with it.
Most times the function runs fine, but sometimes it complains that the assembly is already loaded. I though this should not be possible as functions in the consumption plan should be running statelessly...
Am I missing something?
It's stateless yes, but your app domain will remain loaded indefinitely unless you unload it. As you are using Azure Functions v2, this means you are in the land of .NET Core, meaning you don't have an AppDomain you can use. However, you can use AssemblyLoadContext. For example:
public class FooContext : AssemblyLoadContext
{
public FooContext() : base(isCollectible: true)
{
}
}
And now:
var myLoader = new FooContext();
var assembly = myLoader.LoadFromAssemblyPath("your-path");
// Now do stuff with "assembly" as you did before
Finally, remember to clean up:
myLoader.Unload();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With