Created
July 7, 2015 20:17
-
-
Save tugberkugurlu/ee4dff82f120b4f41939 to your computer and use it in GitHub Desktop.
HttpMessageInvoker sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 { | |
class Program { | |
static void Main(string[] args) { | |
HttpMessageHandler handler = new Handler1() { | |
InnerHandler = new Handler2() { | |
InnerHandler = new Handler3() | |
} | |
}; | |
HttpMessageInvoker invoker = new HttpMessageInvoker(handler); | |
invoker.SendAsync(new HttpRequestMessage(), CancellationToken.None); | |
} | |
} | |
public class Handler1 : DelegatingHandler { | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { | |
Console.WriteLine("Handler1"); | |
var response = await base.SendAsync(request, cancellationToken); | |
Console.WriteLine("Handler1 cont."); | |
return response; | |
} | |
} | |
public class Handler2 : DelegatingHandler { | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { | |
Console.WriteLine("Handler2"); | |
var response = await base.SendAsync(request, cancellationToken); | |
Console.WriteLine("Handler2 cont."); | |
return response; | |
} | |
} | |
public class Handler3 : HttpMessageHandler { | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { | |
Console.WriteLine("Handler3"); | |
Console.WriteLine("Handler2 cont."); | |
return Task.FromResult(new HttpResponseMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment