【发布时间】:2017-11-29 10:56:51
【问题描述】:
我正在尝试创建一个 Azure 函数,在该函数中我使用 AutoMapper 的一些代码。我对 C#、Azure 和 AutoMapper 还很陌生,在找到初始化 AutoMapper 配置的正确方法时遇到了一些麻烦。
MapInitializer.cs:
public static class MapInitializer
{
public static void Activate()
{
Mapper.Initialize(cfg =>
{
// initialize mappings here
});
}
}
然后在我的函数中,我正在尝试执行以下操作:
Function.cs:
public static class ProcessQueueForIntercom
{
[FunctionName("ProcessQueue")]
public static void Run([QueueTrigger("messages")]string myQueueItem, TraceWriter log)
{
MapInitializer.Activate();
// rest of the code
}
}
现在的问题是,我第一次用这个函数处理消息时,一切都很顺利,代码也按预期运行。但是,从第二次开始,我收到一条错误消息,说我的配置已经初始化。但我真的不知道如何使用 Azure Function 正确执行此操作,因为通常您会在 App Startup 中对其进行初始化,但我认为 Azure Functions (CMIW) 没有这样的事情,并且我没有找到太多关于如何准确执行此操作的信息。我正在考虑用 try catch 包围 Activate() 调用,并记录一个配置已加载的警告,但这似乎不是很干净......
【问题讨论】:
标签: c# azure initialization automapper azure-functions