<small id='Z59ej'></small><noframes id='Z59ej'>

  1. <i id='Z59ej'><tr id='Z59ej'><dt id='Z59ej'><q id='Z59ej'><span id='Z59ej'><b id='Z59ej'><form id='Z59ej'><ins id='Z59ej'></ins><ul id='Z59ej'></ul><sub id='Z59ej'></sub></form><legend id='Z59ej'></legend><bdo id='Z59ej'><pre id='Z59ej'><center id='Z59ej'></center></pre></bdo></b><th id='Z59ej'></th></span></q></dt></tr></i><div id='Z59ej'><tfoot id='Z59ej'></tfoot><dl id='Z59ej'><fieldset id='Z59ej'></fieldset></dl></div>

  2. <legend id='Z59ej'><style id='Z59ej'><dir id='Z59ej'><q id='Z59ej'></q></dir></style></legend>
    • <bdo id='Z59ej'></bdo><ul id='Z59ej'></ul>
    1. <tfoot id='Z59ej'></tfoot>

      在 Azure 函数中初始化 AutoMapper

      时间:2023-10-25

            <small id='a7S95'></small><noframes id='a7S95'>

          1. <legend id='a7S95'><style id='a7S95'><dir id='a7S95'><q id='a7S95'></q></dir></style></legend>
            <i id='a7S95'><tr id='a7S95'><dt id='a7S95'><q id='a7S95'><span id='a7S95'><b id='a7S95'><form id='a7S95'><ins id='a7S95'></ins><ul id='a7S95'></ul><sub id='a7S95'></sub></form><legend id='a7S95'></legend><bdo id='a7S95'><pre id='a7S95'><center id='a7S95'></center></pre></bdo></b><th id='a7S95'></th></span></q></dt></tr></i><div id='a7S95'><tfoot id='a7S95'></tfoot><dl id='a7S95'><fieldset id='a7S95'></fieldset></dl></div>
            <tfoot id='a7S95'></tfoot>

                <tbody id='a7S95'></tbody>
                <bdo id='a7S95'></bdo><ul id='a7S95'></ul>
              • 本文介绍了在 Azure 函数中初始化 AutoMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试创建一个 Azure 函数,在该函数中我使用 AutoMapper 的一些代码.我对 C#、Azure 和 AutoMapper 还很陌生,在找到初始化 AutoMapper 配置的正确方法时遇到了一些麻烦.

                I am trying to create an Azure function in which I am using some code with AutoMapper. I am quite new to C#, Azure and AutoMapper and I'm having some trouble finding the correct way of initializing my AutoMapper configuration.

                MapInitializer.cs:

                public static class MapInitializer
                    {
                        public static void Activate()
                        {
                            Mapper.Initialize(cfg =>
                            {
                                // initialize mappings here
                            });
                        }
                    }
                

                然后在我的函数中,我正在尝试执行以下操作:

                Then in my function, I am trying to do the following:

                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() 调用,然后只记录一个配置已经加载的警告,但这似乎不是很干净......

                Now the problem is, the first time I processed a message with this function, everything went smoothly and the code ran as I expected. However, from the second time on, I get an error saying my configuration is already initialized. But I don't really have an idea on how to do this properly with an Azure Function, since normally you would initialize this in the App Startup, but I don't think there is such a thing for Azure Functions (CMIW), and I'm not finding much information about how to exactly do this. I was thinking about just surrounding the Activate() call with a try catch and just log a warning that the configuration is already loaded, but that doesn't seem very clean...

                推荐答案

                你只需要调用一次Activate.您可以从静态构造函数中执行此操作:

                You only need to call Activate once. You can do it from a static constructor:

                public static class ProcessQueueForIntercom
                {
                    static ProcessQueueForIntercom()
                    {
                        MapInitializer.Activate();
                    }
                
                    [FunctionName("ProcessQueue")]
                    public static void Run([QueueTrigger("messages")]string myQueueItem, TraceWriter log)
                    {             
                        // rest of the code
                    }
                }
                

                或者只是在 MapInitializer 本身上创建一个静态构造函数.

                Or just make a static constructor on MapInitializer itself.

                另请参阅此答案.

                这篇关于在 Azure 函数中初始化 AutoMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 下一篇:Azure 函数 2.0 版 - 应用 blobTrigger 无法正常工作

                相关文章

                <i id='QGIDl'><tr id='QGIDl'><dt id='QGIDl'><q id='QGIDl'><span id='QGIDl'><b id='QGIDl'><form id='QGIDl'><ins id='QGIDl'></ins><ul id='QGIDl'></ul><sub id='QGIDl'></sub></form><legend id='QGIDl'></legend><bdo id='QGIDl'><pre id='QGIDl'><center id='QGIDl'></center></pre></bdo></b><th id='QGIDl'></th></span></q></dt></tr></i><div id='QGIDl'><tfoot id='QGIDl'></tfoot><dl id='QGIDl'><fieldset id='QGIDl'></fieldset></dl></div>
                • <bdo id='QGIDl'></bdo><ul id='QGIDl'></ul>

              • <legend id='QGIDl'><style id='QGIDl'><dir id='QGIDl'><q id='QGIDl'></q></dir></style></legend>

                <small id='QGIDl'></small><noframes id='QGIDl'>

                  1. <tfoot id='QGIDl'></tfoot>