private async Task<ApiResultModel> ResolveCall(string id, string parameters) { if (string.IsNullOrWhiteSpace(id)) { return ApiResultModel.FromError("No API name given."); } if (apiMethods.TryGetValue(id, out ApiMethod info)) { try { object parametersObject = null; if (info.HasParameter && !string.IsNullOrWhiteSpace(parameters)) { parametersObject = JsonConverter.Deserialize(parameters, info.ParameterType); } object result = await info.InvokeAsync(parametersObject); return new ApiResultModel { Success = true, Value = info.HasReturnValue ? JsonConverter.Serialize(result) : null, }; } catch (Exception ex) { return ApiResultModel.FromError(ex.Message); } } else { return ApiResultModel.FromError($"Unknown API call \"{id}\"."); } }
public async Task HandleScriptCall(string data) { // run script call handling on separate task to free up UI await Task.Run(async() => { var info = JsonConverter.Deserialize <InvokeInfoModel>(data); if (info != null) { if (info.Type == "title") { string title = JsonConverter.Deserialize <string>(info.Parameters); TitleChanged?.Invoke(this, title); } else if (info.Type == "api") { var result = await ResolveCall(info.Id, info.Parameters); await EndApiCall(info, result); } else if (info.CallbackId != null) { string message = $"Invalid invoke type \"{info.Type ?? "<null>"}\"."; await EndApiCall(info, ApiResultModel.FromError(message)); } } }); }
public override Task Invoke(AspectContext context, AspectDelegate next) { Task t = null; Exception ex = null; try { t = context.Invoke(next); } catch (Exception e) { ex = e; } if (t != null && t.Exception != null) { ex = t.Exception; } if (ex != null && context.ProxyMethod.ReturnType == typeof(IApiResultModel)) { Exception exception = ex.GetInnerException(); if (exception is ServiceException) { context.ReturnValue = new ApiResultModelNoData() { code = 1001, enumMsgType = null, message = exception.Message, result = false, time = DateTime.Now.ToTimeStamp(true).ToString() }; } else if (exception is System.Threading.Tasks.TaskCanceledException) { context.ReturnValue = ApiResultModel.FromError("第三方服务暂时不可用,请稍候再试。"); } else if (exception is SqlException) { if (exception.Message.StartsWith("timeout", StringComparison.OrdinalIgnoreCase)) { context.ReturnValue = ApiResultModel.FromError("数据库连接超时,请稍候再试。"); } else { context.ReturnValue = ApiResultModel.FromError("数据库执行错误,请稍候再试。"); } } else { context.ReturnValue = ApiResultModel.FromError(exception.Message); } return(Task.CompletedTask); } else { return(t); } }
public IApiResultModel DoConvert([FromBody] ConvertDto dto) { if (string.IsNullOrWhiteSpace(dto.SolutionName)) { return(ApiResultModel.FromError("解决方案名称不能为空!")); } if (!convertService.IsProjectPreValid(dto.SolutionName)) { return(ApiResultModel.FromError("解决方案名称不合法,请更换!")); } if (dto.SolutionName.StartsWith("xLiAd.")) { return(ApiResultModel.FromError("解决方案名称不能以 xLiAd. 开头,请更换!")); } OptionsSelect optionsSelect = new OptionsSelect() { Items = dto.Items }; try { var fileItems = convertService.Convert(dto.SolutionName, optionsSelect); using (MemoryStream zipToOpen = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create)) { foreach (var item in fileItems) { var en = archive.CreateEntry(item.FileFullName.TrimStart('/')); using (var stream = en.Open()) stream.Write(item.Content, 0, item.Content.Length); } } if (!Directory.Exists(configModel.ZipSavePath)) { Directory.CreateDirectory(configModel.ZipSavePath); } var fileName = $"{dto.SolutionName}.{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.zip"; var fullName = Path.Combine(configModel.ZipSavePath, fileName); var bs = zipToOpen.ToArray(); using (var fs = new FileStream(fullName, FileMode.Create)) fs.Write(bs, 0, bs.Length); return(ApiResultModel.FromData(fileName)); } } catch (Exception ex) { return(ApiResultModel.FromError(ex.Message)); } }