1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Anemonis.AspNetCore.JsonRpc;
- using JsonRPCTest.Controllers;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- namespace aspnetcore_json_rpc.Controllers
- {
-
- [JsonRpcRoute("/api")]
- public class WeatherForecastController : IJsonRpcService
- {
- private static readonly string[] Summaries = new[]
- {
- "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
- };
- private readonly ILogger<WeatherForecastController> _logger;
- public WeatherForecastController(ILogger<WeatherForecastController> logger)
- {
- _logger = logger;
- }
- [JsonRpcMethod("m1", "p1", "p2")]
- public Task<long> InvokeMethod1Async(long p1, long p2)
- {
- if (p2 == 0L)
- {
- throw new JsonRpcServiceException(100L,"12");
- }
- return Task.FromResult(p1 / p2);
- }
- [JsonRpcMethod("m2", 0, 1)]
- public Task<long> InvokeMethod2Async(long p1, long p2)
- {
- return Task.FromResult(p1 + p2);
- }
- [JsonRpcMethod("Gets", "item")]
- public IEnumerable<WeatherForecast> Get(string item)
- {
- var rng = new Random();
- return Enumerable.Range(1, 5).Select(index => new WeatherForecast
- {
- Date = DateTime.Now.AddDays(index),
- TemperatureC = rng.Next(-20, 55),
- Summary = Summaries[rng.Next(Summaries.Length)]
- })
- .ToArray();
- }
- }
- }
|