forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientFactory.cs
More file actions
92 lines (83 loc) · 5.11 KB
/
HttpClientFactory.cs
File metadata and controls
92 lines (83 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.Http;
namespace System.Net.Http
{
public static class HttpClientFactory
{
/// <summary>
/// Creates a new <see cref="HttpClient"/> instance configured with the handlers provided and with an
/// <see cref="HttpClientHandler"/> as the innermost handler.
/// </summary>
/// <param name="handlers">An ordered list of <see cref="DelegatingHandler"/> instances to be invoked as an
/// <see cref="HttpRequestMessage"/> travels from the <see cref="HttpClient"/> to the network and an
/// <see cref="HttpResponseMessage"/> travels from the network back to <see cref="HttpClient"/>.
/// The handlers are invoked in a top-down fashion. That is, the first entry is invoked first for
/// an outbound request message but last for an inbound response message.</param>
/// <returns>An <see cref="HttpClient"/> instance with the configured handlers.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Handler is disposed with HttpClient")]
public static HttpClient Create(params DelegatingHandler[] handlers)
{
return Create(new HttpClientHandler(), handlers);
}
/// <summary>
/// Creates a new <see cref="HttpClient"/> instance configured with the handlers provided and with the
/// provided <paramref name="innerHandler"/> as the innermost handler.
/// </summary>
/// <param name="innerHandler">The inner handler represents the destination of the HTTP message channel.</param>
/// <param name="handlers">An ordered list of <see cref="DelegatingHandler"/> instances to be invoked as an
/// <see cref="HttpRequestMessage"/> travels from the <see cref="HttpClient"/> to the network and an
/// <see cref="HttpResponseMessage"/> travels from the network back to <see cref="HttpClient"/>.
/// The handlers are invoked in a top-down fashion. That is, the first entry is invoked first for
/// an outbound request message but last for an inbound response message.</param>
/// <returns>An <see cref="HttpClient"/> instance with the configured handlers.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Handler is disposed with HttpClient")]
public static HttpClient Create(HttpMessageHandler innerHandler, params DelegatingHandler[] handlers)
{
HttpMessageHandler pipeline = CreatePipeline(innerHandler, handlers);
return new HttpClient(pipeline);
}
/// <summary>
/// Creates an instance of an <see cref="HttpMessageHandler"/> using the <see cref="DelegatingHandler"/> instances
/// provided by <paramref name="handlers"/>. The resulting pipeline can be used to manually create <see cref="HttpClient"/>
/// or <see cref="HttpMessageInvoker"/> instances with customized message handlers.
/// </summary>
/// <param name="innerHandler">The inner handler represents the destination of the HTTP message channel.</param>
/// <param name="handlers">An ordered list of <see cref="DelegatingHandler"/> instances to be invoked as part
/// of sending an <see cref="HttpRequestMessage"/> and receiving an <see cref="HttpResponseMessage"/>.
/// The handlers are invoked in a top-down fashion. That is, the first entry is invoked first for
/// an outbound request message but last for an inbound response message.</param>
/// <returns>The HTTP message channel.</returns>
public static HttpMessageHandler CreatePipeline(HttpMessageHandler innerHandler, IEnumerable<DelegatingHandler> handlers)
{
if (innerHandler == null)
{
throw Error.ArgumentNull("innerHandler");
}
if (handlers == null)
{
return innerHandler;
}
// Wire handlers up in reverse order starting with the inner handler
HttpMessageHandler pipeline = innerHandler;
IEnumerable<DelegatingHandler> reversedHandlers = handlers.Reverse();
foreach (DelegatingHandler handler in reversedHandlers)
{
if (handler == null)
{
throw Error.Argument("handlers", Properties.Resources.DelegatingHandlerArrayContainsNullItem, typeof(DelegatingHandler).Name);
}
if (handler.InnerHandler != null)
{
throw Error.Argument("handlers", Properties.Resources.DelegatingHandlerArrayHasNonNullInnerHandler, typeof(DelegatingHandler).Name, "InnerHandler", handler.GetType().Name);
}
handler.InnerHandler = pipeline;
pipeline = handler;
}
return pipeline;
}
}
}