forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpBinding.cs
More file actions
254 lines (219 loc) · 9.48 KB
/
Copy pathHttpBinding.cs
File metadata and controls
254 lines (219 loc) · 9.48 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// 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.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web.Http.SelfHost.ServiceModel;
using System.Web.Http.SelfHost.ServiceModel.Channels;
namespace System.Web.Http.SelfHost.Channels
{
/// <summary>
/// A binding used with endpoints for web services that use strongly-type HTTP request
/// and response messages.
/// </summary>
public class HttpBinding : Binding, IBindingRuntimePreferences
{
internal const string CollectionElementName = "httpBinding";
internal const TransferMode DefaultTransferMode = System.ServiceModel.TransferMode.Buffered;
private HttpsTransportBindingElement _httpsTransportBindingElement;
private HttpTransportBindingElement _httpTransportBindingElement;
private HttpBindingSecurity _security;
private HttpMessageEncodingBindingElement _httpMessageEncodingBindingElement;
private Action<HttpTransportBindingElement> _configureTransportBindingElement;
/// <summary>
/// Initializes a new instance of the <see cref="HttpBinding"/> class.
/// </summary>
public HttpBinding()
{
Initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpBinding"/> class with the
/// type of security used by the binding explicitly specified.
/// </summary>
/// <param name="securityMode">The value of <see cref="HttpBindingSecurityMode"/> that
/// specifies the type of security that is used to configure a service endpoint using the
/// <see cref="HttpBinding"/> binding.
/// </param>
public HttpBinding(HttpBindingSecurityMode securityMode)
: this()
{
_security.Mode = securityMode;
}
/// <summary>
/// Gets the envelope version that is used by endpoints that are configured to use an
/// <see cref="HttpBinding"/> binding. Always returns <see cref="System.ServiceModel.EnvelopeVersion.None"/>.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This is existing public API")]
public EnvelopeVersion EnvelopeVersion
{
get { return EnvelopeVersion.None; }
}
/// <summary>
/// Gets or sets a value that indicates whether the hostname is used to reach the
/// service when matching the URI.
/// </summary>
[DefaultValue(HttpTransportDefaults.HostNameComparisonMode)]
public HostNameComparisonMode HostNameComparisonMode
{
get { return _httpTransportBindingElement.HostNameComparisonMode; }
set
{
_httpTransportBindingElement.HostNameComparisonMode = value;
_httpsTransportBindingElement.HostNameComparisonMode = value;
}
}
/// <summary>
/// Gets or sets the maximum amount of memory allocated for the buffer manager that manages the buffers
/// required by endpoints that use this binding.
/// </summary>
[DefaultValue(TransportDefaults.MaxBufferPoolSize)]
public long MaxBufferPoolSize
{
get { return _httpTransportBindingElement.MaxBufferPoolSize; }
set
{
_httpTransportBindingElement.MaxBufferPoolSize = value;
_httpsTransportBindingElement.MaxBufferPoolSize = value;
}
}
/// <summary>
/// Gets or sets the maximum amount of memory that is allocated for use by the manager of the message
/// buffers that receive messages from the channel.
/// </summary>
[DefaultValue(TransportDefaults.MaxBufferSize)]
public int MaxBufferSize
{
get { return _httpTransportBindingElement.MaxBufferSize; }
set
{
_httpTransportBindingElement.MaxBufferSize = value;
_httpsTransportBindingElement.MaxBufferSize = value;
}
}
/// <summary>
/// Gets or sets the maximum size for a message that can be processed by the binding.
/// </summary>
[DefaultValue(TransportDefaults.MaxReceivedMessageSize)]
public long MaxReceivedMessageSize
{
get { return _httpTransportBindingElement.MaxReceivedMessageSize; }
set
{
_httpTransportBindingElement.MaxReceivedMessageSize = value;
_httpsTransportBindingElement.MaxReceivedMessageSize = value;
}
}
/// <summary>
/// Gets or sets the delegate which can configure the <see cref="HttpTransportBindingElement"/> that this binding creates.
/// </summary>
public Action<HttpTransportBindingElement> ConfigureTransportBindingElement
{
get { return _configureTransportBindingElement; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_configureTransportBindingElement = value;
}
}
/// <summary>
/// Gets the URI transport scheme for the channels and listeners that are configured
/// with this binding. (Overrides <see cref="System.ServiceModel.Channels.Binding.Scheme">
/// Binding.Scheme</see>.)
/// </summary>
public override string Scheme
{
get { return GetTransport().Scheme; }
}
/// <summary>
/// Gets or sets the security settings used with this binding.
/// </summary>
public HttpBindingSecurity Security
{
get { return _security; }
set
{
if (value == null)
{
throw Error.ArgumentNull("value");
}
_security = value;
}
}
/// <summary>
/// Gets or sets a value that indicates whether the service configured with the
/// binding uses streamed or buffered (or both) modes of message transfer.
/// </summary>
[DefaultValue(HttpTransportDefaults.TransferMode)]
public TransferMode TransferMode
{
get { return _httpTransportBindingElement.TransferMode; }
set
{
_httpTransportBindingElement.TransferMode = value;
_httpsTransportBindingElement.TransferMode = value;
}
}
/// <summary>
/// Gets a value indicating whether incoming requests can be handled more efficiently synchronously or asynchronously.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This is the pattern used by all standard bindings.")]
bool IBindingRuntimePreferences.ReceiveSynchronously
{
get { return false; }
}
/// <summary>
/// Returns an ordered collection of binding elements contained in the current binding.
/// (Overrides <see cref="System.ServiceModel.Channels.Binding.CreateBindingElements">
/// Binding.CreateBindingElements</see>.)
/// </summary>
/// <returns>
/// An ordered collection of binding elements contained in the current binding.
/// </returns>
public override BindingElementCollection CreateBindingElements()
{
BindingElementCollection bindingElements = new BindingElementCollection();
bindingElements.Add(_httpMessageEncodingBindingElement);
bindingElements.Add(GetTransport());
return bindingElements.Clone();
}
private TransportBindingElement GetTransport()
{
HttpTransportBindingElement result = null;
if (_security.Mode == HttpBindingSecurityMode.Transport)
{
_security.Transport.ConfigureTransportProtectionAndAuthentication(_httpsTransportBindingElement);
result = _httpsTransportBindingElement;
}
else if (_security.Mode == HttpBindingSecurityMode.TransportCredentialOnly)
{
_security.Transport.ConfigureTransportAuthentication(_httpTransportBindingElement);
result = _httpTransportBindingElement;
}
else
{
_security.Transport.DisableTransportAuthentication(_httpTransportBindingElement);
result = _httpTransportBindingElement;
}
// give customer final chance to customize the auth scheme
if (_configureTransportBindingElement != null)
{
_configureTransportBindingElement(result);
}
return result;
}
private void Initialize()
{
_security = new HttpBindingSecurity();
_httpTransportBindingElement = new HttpTransportBindingElement();
_httpTransportBindingElement.ManualAddressing = true;
_httpsTransportBindingElement = new HttpsTransportBindingElement();
_httpsTransportBindingElement.ManualAddressing = true;
_httpMessageEncodingBindingElement = new HttpMessageEncodingBindingElement();
}
}
}