我在Azure中构建了一个简单的函数,该函数从json主体中获取文件的位置,并读取第一行以从所述文件获取标头。我正在Visual Studio中构建该功能,并使用打包的部署发布它。
我能够在Azure Functions下的门户上测试该功能并获得返回结果,但是当我尝试使用Logic App中的功能时,却出现404 Not Found Error。
我已经创建了MS提供的示例HTTPRequest函数,并且在相同的函数名下可以正常工作,但是我不确定为什么我编写的HTTPRequest函数无法正常工作。
下面是我用于该函数的代码
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Functions
{
public static class GetTableHeaders
{
[FunctionName("GetTableHeaders")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
IBinder binder,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string guid = req.Query["guid"];
string header = req.Query["header"];
string location = req.Query["location"];
string line = null;
string[] headers = null;
int size = 0;
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
guid = guid ?? data?.guid;
header = header ?? data?.header;
location = location ?? data?.location;
if (guid != null)
{
location = location.Substring(1);
using (var reader = binder.Bind<TextReader>(new BlobAttribute(
$"{location}", FileAccess.Read)))
{
line = reader.ReadLine();
headers = line.Split(',');
size = headers.Length;
if (!Convert.ToBoolean(header))
{
List<string> genericheaders = new List<string>();
for (int i = 1; i <= size; i++)
{
genericheaders.Add($"column{i}");
}
headers = genericheaders.ToArray();
}
};
return (ActionResult)new OkObjectResult($"{string.Join("|", headers)}");
}
else
{
return (ActionResult)new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}
}
以下是在MS Sample Function中运行的代码:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Functions
{
public static class HttpFunction
{
[FunctionName("HttpFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}
这是我在Azure Functions中运行代码时的结果:
这是我尝试使用该函数运行时从Logic Apps中收到的错误:
下面是逻辑应用json(用xxxx替换订阅ID)
{
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/xxxx/resourceGroups/AMCDS/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/xxxx/providers/Microsoft.Web/locations/westeurope/managedApis/azureblob"
},
"sql": {
"connectionId": "/subscriptions/xxxx/resourceGroups/AMCDS/providers/Microsoft.Web/connections/sql-1",
"connectionName": "sql-1",
"id": "/subscriptions/xxxx/providers/Microsoft.Web/locations/westeurope/managedApis/sql"
}
}
},
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Create_GUID": {
"inputs": "@guid()",
"runAfter": {},
"type": "Compose"
},
"GetTableHeaders": {
"inputs": {
"body": {
"guid": "@{outputs('Create_GUID')}",
"header": "@{body('Insert_row_2')?['HasHeaders']}",
"location": "@{triggerBody()?['Path']}"
},
"function": {
"id": "/subscriptions/xxxx/resourceGroups/AMCDS/providers/Microsoft.Web/sites/AMCDSFunctionsWindows/functions/GetTableHeaders"
},
"method": "POST"
},
"runAfter": {
"Insert_row_2": [
"Succeeded"
]
},
"type": "Function"
},
"HttpFunction": {
"inputs": {
"body": {
"name": "Nirmal"
},
"function": {
"id": "/subscriptions/xxxx/resourceGroups/AMCDS/providers/Microsoft.Web/sites/AMCDSFunctionsWindows/functions/HttpFunction"
}
},
"runAfter": {
"Insert_row_2": [
"Succeeded"
]
},
"type": "Function"
},
"Insert_row_2": {
"inputs": {
"body": {
"DataType": "@{outputs('Split_FileName')[3]}",
"DateLoaded": "@{utcNow()}",
"FileDate": "@{outputs('Split_FileName')[1]}",
"FileName": "@triggerBody()?['Name']",
"FilePath": "@triggerBody()?['Path']",
"GUID": "@{outputs('Create_GUID')}",
"HasHeaders": "@if(equals(outputs('Split_FileName')[2],'#Y#'),true,false)"
},
"host": {
"connection": {
"name": "@parameters('$connections')['sql']['connectionId']"
}
},
"method": "post",
"path": "/datasets/default/tables/@{encodeURIComponent(encodeURIComponent('[meta].[FileMetadata]'))}/items"
},
"runAfter": {
"Split_FileName": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"Split_FileName": {
"inputs": "@split(triggerBody()?['Name'],'.')",
"runAfter": {
"Create_GUID": [
"Succeeded"
]
},
"type": "Compose"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"When_a_blob_is_added_or_modified_(properties_only)": {
"inputs": {
"host": {
"connection": {
"name": "@parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/datasets/default/triggers/batch/onupdatedfile",
"queries": {
"folderId": "JTJmcmVjZWl2ZWQ=",
"maxFileCount": 10
}
},
"metadata": {
"JTJmcmVjZWl2ZWQ=": "/received"
},
"recurrence": {
"frequency": "Minute",
"interval": 1
},
"splitOn": "@triggerBody()",
"type": "ApiConnection"
}
}
}
}
答案 0 :(得分:2)
您已经清除了吗?
我遇到了同样的麻烦。 通过在逻辑应用程序JSON中使用“方法”参数进行了修复。
我的函数仅接受“ GET”方法,而逻辑应用JSON没有方法参数,因此我添加了“'method':'GET'”参数。
答案 1 :(得分:0)
函数应用程序和逻辑应用程序位于单独的位置,并且逻辑应用程序触发器无法执行功能时,会发生NotFound
错误。这发生在我很多次了。一个简单的解决方案是将两个资源都部署在同一位置,并且应该可以正常工作。