コード例 #1
0
        private static T TryExecute <T>(Func <ITrackCancel, T> func,
                                        TrackCancellationToken trackCancellationToken,
                                        string methodName)
        {
            T response = default;

            try
            {
                response = func(trackCancellationToken);
            }
            catch (Exception e)
            {
                _msg.Error($"Error in {methodName}", e);
                trackCancellationToken.Exception = e;
            }

            return(response);
        }
コード例 #2
0
        public static async Task <T> ExecuteServiceCall <T>(
            Func <ITrackCancel, T> func,
            ServerCallContext serverCallContext,
            TaskScheduler taskScheduler,
            bool throwException = false)
        {
            CancellationToken cancellationToken = serverCallContext.CancellationToken;
            string            methodName        = serverCallContext.Method;

            var trackCancellationToken = new TrackCancellationToken(cancellationToken);

            T result =
                await Task.Factory.StartNew(
                    () => TryExecute(func, trackCancellationToken, methodName),
                    cancellationToken, TaskCreationOptions.LongRunning, taskScheduler);

            if (throwException && trackCancellationToken.Exception != null)
            {
                ThrowExceptionToClient(trackCancellationToken.Exception);
            }

            return(result);
        }