WeatherForecastController.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Anemonis.AspNetCore.JsonRpc;
  6. using JsonRPCTest.Controllers;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Logging;
  9. namespace aspnetcore_json_rpc.Controllers
  10. {
  11. [JsonRpcRoute("/api")]
  12. public class WeatherForecastController : IJsonRpcService
  13. {
  14. private static readonly string[] Summaries = new[]
  15. {
  16. "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
  17. };
  18. private readonly ILogger<WeatherForecastController> _logger;
  19. public WeatherForecastController(ILogger<WeatherForecastController> logger)
  20. {
  21. _logger = logger;
  22. }
  23. [JsonRpcMethod("m1", "p1", "p2")]
  24. public Task<long> InvokeMethod1Async(long p1, long p2)
  25. {
  26. if (p2 == 0L)
  27. {
  28. throw new JsonRpcServiceException(100L,"12");
  29. }
  30. return Task.FromResult(p1 / p2);
  31. }
  32. [JsonRpcMethod("m2", 0, 1)]
  33. public Task<long> InvokeMethod2Async(long p1, long p2)
  34. {
  35. return Task.FromResult(p1 + p2);
  36. }
  37. [JsonRpcMethod("Gets", "item")]
  38. public IEnumerable<WeatherForecast> Get(string item)
  39. {
  40. var rng = new Random();
  41. return Enumerable.Range(1, 5).Select(index => new WeatherForecast
  42. {
  43. Date = DateTime.Now.AddDays(index),
  44. TemperatureC = rng.Next(-20, 55),
  45. Summary = Summaries[rng.Next(Summaries.Length)]
  46. })
  47. .ToArray();
  48. }
  49. }
  50. }