using Application.Models; using Infrastructure.Extensions; using IoT.Shared.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using System; using System.Threading.Tasks; namespace IoTNode.Controllers { public class BaseDeviceController : Controller { internal readonly IServiceProvider applicationService; public BaseDeviceController(IServiceProvider applicationServices) { this.applicationService = applicationServices; } [ApiExplorerSettings(IgnoreApi = true)] [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] public string Api() { try { var prefix = $"/{RouteData.Values["controller"]}/"; using var scope = applicationService.CreateScope(); var iotNodeClient = scope.ServiceProvider.GetService(); return iotNodeClient.GetApiJson(prefix); } catch (Exception ex) { ex.PrintStack(); return null; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] public ApiResponse AsyncAction(Action action) { if (action is null) { throw new ArgumentNullException(nameof(action)); } try { Task.Run(() => { action(); }); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.GetMessage()); } return ApiResponse.AsyncSuccess(); } } }