如何以编程方式启用/禁用Azure功能

And*_*NET 6 azure azure-webjobs azure-functions

有没有办法以编程方式启用/禁用Azure功能?

我可以使用"管理"部分下的门户启用/禁用功能,这会导致发送请求 https://<myfunctionapp>.scm.azurewebsites.net/api/functions/<myfunction>

JSON有效负载看起来有点像:

{
   "name":"SystemEventFunction",
   "config":{
      "disabled":true,
      "bindings":[ 
         // the bindings for this function
       ]
   }
  // lots of other properties (mostly URIs)
}
Run Code Online (Sandbox Code Playgroud)

我正在门户外创建一个管理工具,允许用户启用和禁用功能.

希望我可以避免手动创建JSON有效负载,所以我想知道SDK(WebJobs ??)中是否存在具有此功能的内容.

Dav*_*uge 7

继@James Z.的回答,我在C#中创建了以下类,允许您以编程方式禁用/启用Azure功能.

functionsSiteRoot构造函数参数是函数应用程序的Kudu根,例如 https://your-functions-web-app.scm.azurewebsites.net/api/vfs/site/wwwroot/

用户名和密码可以从"功能"的"应用程序服务"设置中的"获取发布配置文件"中获取.

public class FunctionsHelper : IFunctionsHelper
{
    private readonly string _username;
    private readonly string _password;
    private readonly string _functionsSiteRoot;
    private WebClient _webClient;

    public FunctionsHelper(string username, string password, string functionsSiteRoot)
    {
        _username = username;
        _password = password;
        _functionsSiteRoot = functionsSiteRoot;

        _webClient = new WebClient
        {
            Headers = { ["ContentType"] = "application/json" },
            Credentials = new NetworkCredential(username, password),
            BaseAddress = functionsSiteRoot
        };
    }

    public void StopFunction(string functionName)
    {
        SetFunctionState(functionName, isDisabled: true);
    }

    public void StartFunction(string functionName)
    {
        SetFunctionState(functionName, isDisabled: false);
    }

    private void SetFunctionState(string functionName, bool isDisabled)
    {
        var functionJson =
            JsonConvert.DeserializeObject<FunctionSettings>(_webClient.DownloadString(GetFunctionJsonUrl(functionName)));
        functionJson.disabled = isDisabled;
        _webClient.Headers["If-Match"] = "*";
        _webClient.UploadString(GetFunctionJsonUrl(functionName), "PUT", JsonConvert.SerializeObject(functionJson));
    }

    private static string GetFunctionJsonUrl(string functionName)
    {
        return $"{functionName}/function.json";
    }
}

internal class FunctionSettings
{
    public bool disabled { get; set; }
    public List<Binding> bindings { get; set; }
}

internal class Binding
{
    public string name { get; set; }
    public string type { get; set; }
    public string direction { get; set; }
    public string queueName { get; set; }
    public string connection { get; set; }
    public string accessRights { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

  • 这确实有帮助,虽然我不得不做一些更改,但是第一次必须将功能url发送到:return $"/ home/site/wwwroot/{functionName} /function.json"; 第二次json转换和序列化不起作用,我只需要做一个简单的搜索并替换:funcFile = funcFile.Replace("\"disabled \":true","\"disabled \":false"); (2认同)

mat*_*ewc 6

不,目前这是不可能的.disabledfunction.json中的metadata属性决定了是否启用了一个函数.在门户中启用/禁用时,门户网站只会更新该值.

不确定它是否满足您的需求,但我会指出还有一个host.json functions数组,可用于控制将要加载的函数集(此处记录).因此,例如,如果您只想启用10个​​函数中的2个,则可以将此属性设置为仅包含这2个函数名称的数组(例如"functions": [ "QueueProcessor", "GitHubWebHook" ]),并且只会加载/启用这些函数.但是,这与启用/禁用略有不同,因为您将无法通过门户调用排除的函数,而您可以通过门户调用禁用的函数.


Jon*_*igh 5

除了上面@DavidGouge 的回答之外,他发布的代码确实有效,我刚刚对其进行了测试并将在我的应用程序中使用它。但是它需要一些调整:

  1. 从 IFunctionsHelper 中删除继承。我不确定那个接口是什么,但它不是必需的。

  2. 将 Binding 的类定义更改如下:

        internal class Binding
        {
            public string name { get; set; }
            public string type { get; set; }
            public string direction { get; set; }
            public string queueName { get; set; }
            public string connection { get; set; }
            public string accessRights { get; set; }
            public string schedule { get; set; }
        }
    
    Run Code Online (Sandbox Code Playgroud)

    在那之后它会起作用。

PS 我会把它作为对原始答案的评论,但我在 Stack Overflow 上没有足够的声誉来发表评论!