Пример #1
0
 public static Task<ApiResult> Process(Task task)
 {
     return task.ContinueWith<ApiResult>(t =>
     {
         ApiResult apiResult = null;
         if (!t.IsFaulted)
         {
             if (t.GetType().IsGenericType)
             {
                 var result = (t as Task<object>).Result;
                 if (result != null)
                 {
                     var resultType = result.GetType();
                     var apiResultType = typeof(ApiResult<>).MakeGenericType(resultType);
                     apiResult = Activator.CreateInstance(apiResultType, result) as ApiResult;
                 }
                 else
                 {
                     apiResult = new ApiResult();
                 }
             }
             else
             {
                 apiResult = new ApiResult();
             }
         }
         else
         {
             var baseException = t.Exception.GetBaseException();
             if (baseException is SysException)
             {
                 var sysException = baseException as SysException;
                 apiResult = new ApiResult { ErrorCode = sysException.ErrorCode, Message = sysException.Message };
             }
             else
             {
                 apiResult = new ApiResult { ErrorCode = ErrorCode.UnknownError, Message = baseException.Message };
             }
         }
         return apiResult;
     });
 }
Пример #2
0
 public async static Task<ApiResult> Process(Func<Task> func)
 {
     ApiResult apiResult = null;
     try
     {
         var t = func();
         await t;
         if (t.GetType().IsGenericType)
         {
             var result = (t as Task<object>).Result;
             if (result != null)
             {
                 var resultType = result.GetType();
                 var apiResultType = typeof(ApiResult<>).MakeGenericType(resultType);
                 apiResult = Activator.CreateInstance(apiResultType, result) as ApiResult;
             }
             else
             {
                 apiResult = new ApiResult();
             }
         }
         else
         {
             apiResult = new ApiResult();
         }
     }
     catch (Exception ex)
     {
         var baseException = ex.GetBaseException();
         if (baseException is SysException)
         {
             var sysException = baseException as SysException;
             apiResult = new ApiResult { ErrorCode = sysException.ErrorCode, Message = sysException.Message };
         }
         else
         {
             apiResult = new ApiResult { ErrorCode = ErrorCode.UnknownError, Message = baseException.Message };
         }
     }
     return apiResult;
 }