WeatherForecastController.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Dapr.Client;
  2. using Microsoft.AspNetCore.Mvc;
  3. namespace MyBackEnd.Controllers
  4. {
  5. [ApiController]
  6. [Route("[controller]")]
  7. public class WeatherForecastController : ControllerBase
  8. {
  9. private static readonly string[] Summaries = new[]
  10. {
  11. "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
  12. };
  13. private readonly DaprClient _daprClient;
  14. private readonly ILogger<WeatherForecastController> _logger;
  15. public WeatherForecastController(
  16. DaprClient daprClient,
  17. ILogger<WeatherForecastController> logger)
  18. {
  19. _daprClient = daprClient;
  20. _logger = logger;
  21. }
  22. [HttpGet(Name = "GetWeatherForecast")]
  23. public IEnumerable<WeatherForecast> Get()
  24. {
  25. return Enumerable.Range(1, 5).Select(index => new WeatherForecast
  26. {
  27. Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
  28. TemperatureC = Random.Shared.Next(-20, 55),
  29. Summary = Summaries[Random.Shared.Next(Summaries.Length)]
  30. })
  31. .ToArray();
  32. }
  33. [HttpGet("remote-time")]
  34. public async Task<string> GetTime()
  35. {
  36. var r = await _daprClient.InvokeMethodAsync<Rsp>(
  37. HttpMethod.Get,
  38. "hotline",
  39. "api/v1/Test/time",
  40. HttpContext.RequestAborted);
  41. return r.Result;
  42. }
  43. }
  44. public class Rsp
  45. {
  46. //{
  47. // "result": "Wednesday, 19 July 2023 14:32:43",
  48. // "code": 0,
  49. // "message": null,
  50. // "error": null
  51. //}
  52. public string Result { get; set; }
  53. public int Code { get; set; }
  54. public string Message { get; set; }
  55. public string Error { get; set; }
  56. }
  57. }