コード例 #1
0
        private AsyncCallResult<XDocument> Call(Func<IAsyncClient, CancellationToken, Task<HttpResponseMessage>> innerCall_)
        {
            var source = new CancellationTokenSource();
            var token = source.Token;
            using (var client = new AsyncClient())
            {
                client.Init(_securityKeys.Credentials);
                using (var task = innerCall_(client, token))
                {
                    try
                    {
                        if (task.Wait(5000, token) == false)
                        {
                            if (token.CanBeCanceled)
                            {
                                source.Cancel();
                            }
                            return new AsyncCallResult<XDocument>(AsyncCallFailureReason.TimeOut);
                        }
                    }
                    catch (Exception)
                    {
                        return new AsyncCallResult<XDocument>(AsyncCallFailureReason.FailedConnection);
                    }
                    var content = task.Result.Content.ReadAsStreamAsync();
                    if (content.Wait(250, token) == false)
                    {
                        if (token.CanBeCanceled)
                        {
                            source.Cancel();
                        }
                        return new AsyncCallResult<XDocument>(AsyncCallFailureReason.TimeOut);
                    }

                    using (var streamReader = new StreamReader(content.Result))
                    {
                        var doc = XDocument.Load(streamReader,LoadOptions.SetLineInfo);
                        if (task.Result.IsSuccessStatusCode == false)
                        {
                            return new AsyncCallResult<XDocument>(AsyncCallFailureReason.FailedStatusCode, doc);
                        }
                        return new AsyncCallResult<XDocument>(AsyncCallFailureReason.None, doc);
                    }
                }
            }
        }
コード例 #2
0
 internal bool CallNoReturn(string className, object criteria, Func<IAsyncClient, CancellationToken, Task<HttpResponseMessage>> innerCall)
 {
     var source = new CancellationTokenSource();
     var token = source.Token;
     using (var client = new AsyncClient())
     {
         client.Init(_securityKeys.Credentials);
         using (var task = innerCall(client, token))
         {
             if (task.Wait(30000, token) == false)
             {
                 if (token.CanBeCanceled)
                 {
                     source.Cancel();
                 }
                 return false;
             }
             return task.Result.IsSuccessStatusCode;
         }
     }
 }