コード例 #1
0
        public async Task <Stream> Transport(Stream request, Context context)
        {
            var clientContext = context as ClientContext;
            var result        = MockAgent.Handler(clientContext.Uri.Host, request);
            var timeout       = clientContext.Timeout;

            if (timeout > TimeSpan.Zero)
            {
                using (CancellationTokenSource source = new CancellationTokenSource()) {
#if NET40
                    var timer = TaskEx.Delay(timeout, source.Token);
                    var task  = await TaskEx.WhenAny(timer, result).ConfigureAwait(false);
#else
                    var timer = Task.Delay(timeout, source.Token);
                    var task  = await Task.WhenAny(timer, result).ConfigureAwait(false);
#endif
                    source.Cancel();
                    if (task == timer)
                    {
                        throw new TimeoutException();
                    }
                }
            }
            return(await result);
        }
コード例 #2
0
        public Task Bind(MockServer server)
        {
            MockAgent.Register(server.Address, Handler);
#if NET40
            return(TaskEx.FromResult <object>(null));
#elif NET45 || NET451 || NET452
            return(Task.FromResult <object>(null));
#else
            return(Task.CompletedTask);
#endif
        }
コード例 #3
0
 public void Close()
 {
     MockAgent.Cancel(Address);
 }