定义Azure Function应用程序我有许多几乎相同的功能:
public static class FunctionA
{
[FunctionName("function_a")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest request, ILogger logger)
{
//... Authentication, etc
return result = ServiceA.Do(request);
}
}
public static class FunctionB
{
[FunctionName("function_b")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest request, ILogger logger)
{
//... Authentication, etc
return result = ServiceB.Do(request);
}
}
//...
有没有办法DRY此代码?
理想情况下,我希望具有泛型继承之类的东西,但我不能这样做,因为静态类只能从对象继承。
答案 0 :(得分:1)
我想到的一个想法是使用路由。您基本上只定义了一个外部HTTP触发函数,然后根据路由属性在其中切换。像这样:
[FunctionName("function_external")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "service/{service}")]HttpRequest request, string service, ILogger logger)
{
//... Authentication, etc
// Depending on how many cases you have, you probably want some smarter lookup here
switch(service)
{
case "a": return ServiceA.Do(request);
//...
}
}
这不是一个完美的通用解决方案,但是至少您今天可以轻松实现。
答案 1 :(得分:0)
您可以使用proxy或API management进行路由。