c# - Azure Function 不是无状态的

标签 c# reflection azure-functions

我在使用反射的消费计划上有一个 .NetCore 2 Azure 函数(运行时 2),它在当前域中加载一个程序集(Assembly.LoadFrom()) 并用它做其他事情。

大多数时候该函数运行良好,但有时它会提示程序集已经加载。我虽然这不应该是可能的,因为消费计划中的功能应该无状态地运行......

我错过了什么吗?

最佳答案

是的,它是无状态的,但您的应用程序域将无限期保持加载状态,除非您卸载它。当您使用 Azure Functions v2 时,这意味着您处于 .NET Core 领域,这意味着您没有可以使用的 AppDomain。但是,您可以使用 AssemblyLoadContext .例如:

public class FooContext : AssemblyLoadContext
{
    public FooContext() : base(isCollectible: true)
    {
    }
}

现在:

var myLoader = new FooContext();

var assembly = myLoader.LoadFromAssemblyPath("your-path");

// Now do stuff with "assembly" as you did before

最后记得清理一下:

myLoader.Unload();

关于c# - Azure Function 不是无状态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60099893/

相关文章:

c# - 在 C# 中创建 MJPEG 视频流

c# - 将多个文本框值绑定(bind)到单个属性

Java 8 是否有可能使用反射或其他方法在方法中插入代码?

Azure函数: Limiting number of concurrant Service Bus messages

c# - Automapper,映射配置不持久

c# - 我无法使用反射订阅我的事件

C# 将反射与泛型属性结合使用

azure - Azure Functions 中的 .json 文件的用途是什么?

azure - 在 Azure Function App 中使用 PowerShell 连接到不同的 Azure AD 租户

c# - string.Format 异常输入字符串的格式不正确