有没有办法从预先编写的C#Azure Functions自动生成Swagger标签?

是否有人知道任何.NET属性可用于注释预编译的C#Azure函数,以便它可以自动生成Swagger标记? 例如,我想在Swagger中自动生成“tags”条目:

/api/v1/revision: get: operationId: /api/v1/revision/get tags: - System produces: [] consumes: [] parameters: [] description: Gets the API version responses: '200': description: Success operation security: - apikeyQuery: [] 

这是我的C#函数:

  public static class VersioningService { [FunctionName("ApiVersion")] public static async Task ApiVersion([HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/revision")]HttpRequestMessage req, TraceWriter log) { return req.CreateResponse(HttpStatusCode.OK, "

API Version: 1.0340"); } }

不幸的是,目前似乎不支持它。 我也在github中发现了类似的问题 。 您还可以向Azurefunction团队提供反馈 。

发现这篇文章:

  • 以编程方式为函数创建Swagger定义

我已经使用PowerShell实现了这些步骤。
该脚本假定您已配置host.json文件:

 { "swagger": { "enabled": true } } 

此脚本仅适用于Azure Function runtime v1,尚未为运行时v2实现swagger生成

 function Get-KuduApiAuthorisationHeaderValueAzure { Param ( [Parameter(Mandatory = $true)] [string]$resourceGroupName, [Parameter(Mandatory = $true)] [string]$functionappName ) Begin { $resourceType = "Microsoft.Web/sites/config" $resourceName = "$functionappName/publishingcredentials" $publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))) return $base64AuthInfo } } function Get-MasterAPIKey { Param ( [Parameter(Mandatory = $true)] [string]$resourceGroupName, [Parameter(Mandatory = $true)] [string]$functionappName ) Begin { $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValueAzure -resourceGroupName $resourceGroupName -functionappName $functionappName $apiUrl = "https://$functionappName.scm.azurewebsites.net/api/functions/admin/masterkey" $response = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization = ("Basic {0}" -f $kuduApiAuthorisationToken);"If-Match"="*"} return $response.masterKey } } function Get-FunctionsApiJwt { Param ( [Parameter(Mandatory = $true)] [string]$resourceGroupName, [Parameter(Mandatory = $true)] [string]$functionappName ) Begin { $base64AuthInfo = Get-KuduApiAuthorisationHeaderValueAzure -resourceGroupName $resourceGroupName -functionappName $functionappName $apiUrl = "https://$functionappName.scm.azurewebsites.net/api/functions/admin/token" $jwt = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method GET return $jwt } } function Generate-SwaggerDefinition { Param ( [Parameter(Mandatory = $true)] [string]$resourceGroupName, [Parameter(Mandatory = $true)] [string]$functionappName ) Begin { # Get the swagger documentation key $masterKey = Get-MasterAPIKey -resourceGroupName $resourceGroupName -functionappName $functionappName $apiUrl = "https://$functionappName.azurewebsites.net/admin/host/systemkeys/swaggerdocumentationkey" $swaggerdocumentationkey = (Invoke-RestMethod -Uri $apiUrl -Headers @{"x-functions-key"=$masterKey} -Method Post).value # Update the resource $config = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType "microsoft.web/sites/config" -Name "$functionappName/web" -ApiVersion "2015-08-01" $config.Properties.apiDefinition = @{"url"="https://$functionappName.azurewebsites.net/admin/host/swagger?code=$swaggerdocumentationkey"} $config | Set-AzureRmResource -Force -ApiVersion "2015-08-01" # Generate the swagger definition $jwt = Get-FunctionsApiJwt -resourceGroupName $resourceGroupName -functionappName $functionappName $swaggerDefinition = (Invoke-RestMethod -Uri "https://$functionappName.azurewebsites.net/admin/host/swagger/default?code=$swaggerdocumentationkey" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method Get) # Save the swagger definition Invoke-RestMethod -Uri "https://$functionappName.azurewebsites.net/admin/host/swagger?code=$swaggerdocumentationkey" -ContentType 'application/json' -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method Post -Body (ConvertTo-Json $swaggerDefinition -Depth 100) } } 

你可以像这样调用这个脚本:

 Generate-SwaggerDefinition -resourceGroupName "myresourcegroupname" -functionappName "myfunctionappname" 

编辑

新创建的function应用程序默认使用TLS 1.2,因此您需要在Powershell脚本的顶部添加此行:

 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12