Gal*_*let 20 azure azure-functions
我写了3个函数如下
在[3]函数中,我将调用[2]函数来获取用户使用Azure函数URL,如下所示: -
https://hdidownload.azurewebsites.net/api/getusers
有没有其他方法在没有完整路径的另一个Azure功能中调用Azure功能?
Mik*_*kov 14
函数应用程序中没有任何内置功能可以从其他函数调用一个HTTP函数,而无需实际进行HTTP调用.
对于简单的用例,我会坚持使用完整的URL进行调用.
有关更高级的工作流程,请查看Durable Functions,特别是Function Chaining.
前面的所有答案都是有效的,但是,正如评论中提到的那样,今年早些时候(2018年第一季度/第二季度)引入了持久功能的概念。简而言之,耐用功能:
...使您可以在无服务器环境中编写有状态功能。该扩展为您管理状态,检查点并重新启动。
这实际上意味着您现在还可以将多个功能链接在一起。如果需要,它管理从功能A => B => C流出的状态。
通过将耐久功能扩展安装到您的功能应用程序中,它可以工作。这样,您就可以使用一些新的上下文绑定,例如在C#中,您可以执行以下操作(伪代码):
[FunctionName("ExpensiveDurableSequence")]
public static async Task<List<string>> Run(
[OrchestrationTrigger] DurableOrchestrationTrigger context)
{
var response = new List<Order>();
// Call external function 1
var token = await context.CallActivityAsync<string>("GetDbToken", "i-am-root");
// Call external function 2
response.Add(await context.CallActivityAsync<IEnumerable<Order>>("GetOrdersFromDb", token));
return response;
}
[FunctionName("GetDbToken")]
public static string GetDbToken([ActivityTrigger] string username)
{
// do expensive remote api magic here
return token;
}
[FunctionaName("GetOrdersFromDb")]
public static IEnumerable<Order> GetOrdersFromDb([ActivityTrigger] string apikey)
{
// do expensive db magic here
return orders;
}
Run Code Online (Sandbox Code Playgroud)
这里有一些不错的方面:
这样,您既可以依次运行多个功能(例如,功能链),也可以并行执行多个功能,然后等待所有功能完成(扇出/扇入)。
关于此的一些其他背景参考:
我知道我们有 Durable 函数,但我像普通的静态方法一样调用我的函数并且它可以工作,这里有一个例子:
public static class HelloWorld
{
[FunctionName("HelloWorld")]
public static string Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
{
return "Hello World";
}
}
public static class HelloWorldCall
{
[FunctionName("HelloWorldCall")]
public static string Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
{
var caller = HelloWorld.Run(req, log);
return caller;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过将第二个函数作为普通 C# 静态方法调用来直接执行此操作。
但在这种情况下,您将失去 Azure Functions 缩放和分发的好处(例如,根据服务器负载,您的第二个函数可能会从世界的不同地方调用)。
至于 C# 中的第一个选项,您可以这样做:
static HttpClient client = new HttpClient();
[FunctionName("RequestImageProcessing")]
public static async Task RequestImageProcessing([HttpTrigger(WebHookType = "genericJson")]
HttpRequestMessage req)
{
string anotherFunctionSecret = ConfigurationManager.AppSettings
["AnotherFunction_secret"];
// anotherFunctionUri is another Azure Function's
// public URL, which should provide the secret code stored in app settings
// with key 'AnotherFunction_secret'
Uri anotherFunctionUri = new Uri(req.RequestUri.AbsoluteUri.Replace(
req.RequestUri.PathAndQuery,
$"/api/AnotherFunction?code={anotherFunctionSecret}"));
var responseFromAnotherFunction = await client.GetAsync(anotherFunctionUri);
// process the response
}
[FunctionName("AnotherFunction")]
public static async Task AnotherFunction([HttpTrigger(WebHookType = "genericJson")]
HttpRequestMessage req)
{
await Worker.DoWorkAsync();
}
Run Code Online (Sandbox Code Playgroud)
此外,有时您需要第一个 Azure 函数首先返回 HTTP 响应,然后在后台执行某些操作,因此此解决方案不起作用。在这种情况下,选项 2 和 3 是合适的。
| 归档时间: |
|
| 查看次数: |
17053 次 |
| 最近记录: |