Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function not stateless

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?

like image 672
teocomi Avatar asked Feb 24 '26 16:02

teocomi


1 Answers

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();
like image 191
DavidG Avatar answered Feb 27 '26 06:02

DavidG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!