Azure Functions - Shared classes

后端 未结 4 1357
天命终不由人
天命终不由人 2020-12-31 03:24

I want to use some shared classes on my Azure Functions to not duplicate code.

I have tried to create a empty C# function and create the classes inside the function

4条回答
  •  -上瘾入骨i
    2020-12-31 03:32

    First, put your shared code inside a folder in the root of your Function App directory (e.g. "Shared"). Let's say I put a shared Message.csx class in that folder (e.g. full path D:\home\site\wwwroot\Shared\Message.csx).

    To include this into your function use the #load command:

    #load "..\Shared\Message.csx"
    
    using System;
    using Microsoft.Azure.WebJobs.Host;
    
    public static void Run(Message message, TraceWriter log)
    {
        log.Info($"C# Queue trigger function processed message: {message.Id}");
    }
    

    See the help page here for more information. By default, the files in that directory won't be tracked for changes. If you want to ensure that when files in that directory change your functions will pick up the changes and be recompiled, you can add your "Shared" directory to the watchDirectories list in host.json. E.g.:

    {
        "watchDirectories": [ "Shared" ]
    }
    

提交回复
热议问题