c# - 从代码部署 Azure 函数 (c#)

标签 c# azure azure-functions azure-rm-template

如何使用代码作为字符串(在 C# 中)将 azure 函数(按计划执行)部署到给定的 azure 函数应用程序?

我将使用 ARM 模板来部署 azure 基金应用程序(+它需要的所有内容)https://github.com/Azure/azure-quickstart-templates/tree/master/101-function-app-create-dynamic ,可以通过代码部署;

但我没有找到通过代码将函数部署到函数应用程序的方法。

+ 更多上下文:部署将从应用服务进行,因此最好不要有 NuGet 之外的任何依赖项。例如。我不喜欢从 c# 调用 azure cli 的想法。

最佳答案

正如 Jesse Carter 提到的,我们可以使用 Kudu Zip Api要做到这一点。我为此做了一个演示。它在我这边工作正常。以下是我的详细步骤:

准备工作:

注册AD应用并为应用分配角色,更多详情请引用Azure official tutorials 。之后我们可以从Azure Portal获取tenantId、appId、secretKey。

1.准备一个认证文件,我们可以从github获取更多信息document .

subscription=########-####-####-####-############
client=########-####-####-####-############
tenant=########-####-####-####-############
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
graphURL=https\://graph.windows.net/

2.压缩需要发布的文件

步骤:

1.创建C#控制台项目

2.引用Microsoft.Azure.Management.ResourceManager.FluentMicrosoft.Azure.Management.AppService.Fluent ,更多详细信息请参阅packages.config文件部分。

3.在Program.cs文件中添加以下代码

   var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"authentication file path");
   var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();
   var webFunctionAppName = "azure function name";
   var webFunctionApp = azure.AppServices.FunctionApps.List().Where(x => x.Name.Equals(webFunctionAppName))?.First();
   var ftpUsername = azure.AppServices.FunctionApps.GetById(webFunctionApp.Id).GetPublishingProfile().FtpUsername;
   var username = ftpUsername.Split('\\').ToList()[1];
   var password = azure.AppServices.FunctionApps.GetById(webFunctionApp.Id).GetPublishingProfile().FtpPassword;
   var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
   var file = File.ReadAllBytes(@"zip file path");
   MemoryStream stream = new MemoryStream(file);

    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64Auth);
        var baseUrl = new Uri($"https://{webFunctionAppName}.scm.azurewebsites.net/");
        var requestURl = baseUrl+ "api/zip/site/wwwroot";
        var httpContent = new StreamContent(stream);
        var response = client.PutAsync(requestURl, httpContent).Result;
     }

4.本地测试

enter image description here

5.查看Azure kudu工具发布的结果( https://yourazurefunctionanme.scm.azurewebsites.net/ )

enter image description here

包.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.Management.AppService.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Batch.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Cdn.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Compute.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.ContainerRegistry.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Dns.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.DocumentDB.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Graph.RBAC.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.KeyVault.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Network.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Redis.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.ResourceManager.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.ServiceBus.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Sql.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Storage.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.TrafficManager.Fluent" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.8" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.3.0" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
</packages>

关于c# - 从代码部署 Azure 函数 (c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45034054/

相关文章:

Azure Functions - StorageBlob 使用 CloudBlockBlob 而不是 Stream 触发方法签名

C# ESC POS 特殊字符

c# - 如何解决错误 "Unable to locate local.config.user file. Make sure you have run ' build.cmd local' '

Azure Function 消耗计划限制

python - 使用Python中的Azure语音服务读取音频文件并转换为文本,但只有第一句话转换为语音

azure - 为 VM 部署 ARM 模板时出现 InvalidResourceReference

azure - 与旧库相比,新库的 Cosmos DB 速度非常慢

c# - 查看dll的内容

c# - 发送草稿邮件时 Outlook SendUsingAccount 为空

rest - Azure DocumentDB Rest API PowerShell 删除集合 401 未经授权