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

从代码(c#)部署Azure函数,c#,azure,azure-functions,arm-template,C#,Azure,Azure Functions,Arm Template,如何将代码为字符串(c#)的azure函数(按计划执行)部署到给定的azure函数应用程序? 我将使用ARM模板部署azure基金应用程序(+所有它需要的),它可以通过代码部署 但我看不到通过代码将函数部署到函数应用程序的方法 +更详细一点:部署将从应用程序服务进行,因此最好不要有NuGet之外的任何依赖项。例如,我不喜欢从c#调用azure cli的想法。如果您打算直接从代码部署函数本身,而不是使用CI/CD管道,那么最好使用将函数代码作为zip上传到正在运行的函数应用程序。您应该能够使用Ht

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

我将使用ARM模板部署azure基金应用程序(+所有它需要的),它可以通过代码部署

但我看不到通过代码将函数部署到函数应用程序的方法


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

如果您打算直接从代码部署函数本身,而不是使用CI/CD管道,那么最好使用将函数代码作为zip上传到正在运行的函数应用程序。您应该能够使用HttpClient或任何其他.NET REST库进行操作。

如果您打算直接从代码而不是使用CI/CD管道部署函数本身,那么最好使用将函数代码作为zip上传到正在运行的函数应用程序。您应该可以使用HttpClient或任何其他.NET REST库来完成此任务。

正如Jesse Carter提到的,我们可以使用它来完成此任务。我做了一个演示。对我来说,它工作正常。以下是我的详细步骤:

准备:

注册AD应用程序并将角色分配给应用程序,更多详细信息请参阅。之后,我们可以从Azure门户获取tenantId、appId和secretKey

1.准备一个身份验证文件,我们可以从github获得更多信息

2.压缩需要发布的文件

步骤:

1.创建一个C#控制台项目

2.请参阅,更多详细信息请参阅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.在当地进行测试

5.检查Azure kudu工具()发布的结果

packages.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>

正如杰西·卡特(Jesse Carter)提到的那样,我们可以用它来实现这一点。我做了一个演示。对我来说,它工作正常。以下是我的详细步骤:

准备:

注册AD应用程序并将角色分配给应用程序,更多详细信息请参阅。之后,我们可以从Azure门户获取tenantId、appId和secretKey

1.准备一个身份验证文件,我们可以从github获得更多信息

2.压缩需要发布的文件

步骤:

1.创建一个C#控制台项目

2.请参阅,更多详细信息请参阅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.在当地进行测试

5.检查Azure kudu工具()发布的结果

packages.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>


您是否可以提供有关如何在刚创建的azure functions应用程序中对此访问进行身份验证的详细信息?文档中有大量的信息。身份验证不在您的问题范围之内。我建议您花些时间仔细阅读文档,尝试一些不同的方法。如果您对下面的代码有特殊的问题,请询问一个新的问题,我们已经阅读了许多文档页面。但是,这些都不包括通过代码获取应用程序的凭据。请提供有关如何在刚创建的azure functions应用程序中对此访问进行身份验证的详细信息?文档中有大量的信息。身份验证不在您的问题范围之内。我建议您花些时间仔细阅读文档,尝试一些不同的方法。如果您对下面的代码有特殊的问题,请询问一个新的问题,我们已经阅读了许多文档页面。但是,这些都不包括通过代码获取应用程序的凭据。是否有人对使用上述kudu api示例将ServiceBustigger函数部署到现有函数应用程序存在问题?部署后,使用第二个触发器同步选项手动同步函数应用程序的触发器。是否发现任何人在使用上述kudu api示例将ServiceBustigger函数部署到现有函数应用程序时存在问题?部署后,使用找到的第二个触发器同步选项手动同步功能应用程序的触发器