forked from Azure-Samples/functions-quickstart-dotnet-azd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpGetFunction.cs
50 lines (42 loc) · 1.66 KB
/
httpGetFunction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
namespace Company.Function
{
public class httpGetFunction
{
private readonly ILogger _logger;
private readonly IDistributedCache _cache;
public httpGetFunction(ILoggerFactory loggerFactory, IDistributedCache cache)
{
_logger = loggerFactory.CreateLogger<httpGetFunction>();
_cache = cache;
}
[Function("httpget")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")]
HttpRequest req,
string name = "World")
{
try {
if(await _cache.GetStringAsync(name) is string cachedValue)
{
_logger.LogInformation("C# HTTP trigger function processed a request for {name} from cache.", name);
return new OkObjectResult(cachedValue);
}
var returnValue = string.IsNullOrEmpty(name)
? "Hello, World."
: $"Hello, {name}.";
await _cache.SetStringAsync(name, returnValue);
_logger.LogInformation("C# HTTP trigger function processed a request for {name}.", name);
return new OkObjectResult(returnValue);
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred in the function.");
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
}
}