/
NetHttpBinding.cs
169 lines (142 loc) · 5.58 KB
/
NetHttpBinding.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.ServiceModel.Channels;
using System.Xml;
namespace System.ServiceModel
{
public class NetHttpBinding : HttpBindingBase
{
private BinaryMessageEncodingBindingElement _binaryMessageEncodingBindingElement;
private ReliableSessionBindingElement _session;
private OptionalReliableSession _reliableSession;
private BasicHttpSecurity _basicHttpSecurity;
public NetHttpBinding()
: this(BasicHttpSecurityMode.None)
{
}
public NetHttpBinding(BasicHttpSecurityMode securityMode)
: base()
{
Initialize();
_basicHttpSecurity.Mode = securityMode;
}
public NetHttpBinding(BasicHttpSecurityMode securityMode, bool reliableSessionEnabled) : this(securityMode)
{
ReliableSession.Enabled = reliableSessionEnabled;
}
private NetHttpBinding(BasicHttpSecurity security)
: base()
{
Initialize();
_basicHttpSecurity = security;
}
[DefaultValue(NetHttpMessageEncoding.Binary)]
public NetHttpMessageEncoding MessageEncoding { get; set; }
public BasicHttpSecurity Security
{
get
{
return _basicHttpSecurity;
}
set
{
_basicHttpSecurity = value ?? throw FxTrace.Exception.ArgumentNull(nameof(value));
}
}
public OptionalReliableSession ReliableSession
{
get
{
return _reliableSession;
}
set
{
if (value == null)
{
throw FxTrace.Exception.ArgumentNull(nameof(value));
}
_reliableSession.CopySettings(value);
}
}
public WebSocketTransportSettings WebSocketSettings
{
get
{
return InternalWebSocketSettings;
}
}
internal override BasicHttpSecurity BasicHttpSecurity
{
get
{
return _basicHttpSecurity;
}
}
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingParameterCollection parameters)
{
if ((BasicHttpSecurity.Mode == BasicHttpSecurityMode.Transport ||
BasicHttpSecurity.Mode == BasicHttpSecurityMode.TransportCredentialOnly) &&
BasicHttpSecurity.Transport.ClientCredentialType == HttpClientCredentialType.InheritedFromHost)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.Format(SR.HttpClientCredentialTypeInvalid, BasicHttpSecurity.Transport.ClientCredentialType)));
}
return base.BuildChannelFactory<TChannel>(parameters);
}
public override BindingElementCollection CreateBindingElements()
{
CheckSettings();
// return collection of BindingElements
BindingElementCollection bindingElements = new BindingElementCollection();
// order of BindingElements is important
// add session
if (_reliableSession.Enabled)
{
bindingElements.Add(_session);
}
// add security (*optional)
SecurityBindingElement messageSecurity = BasicHttpSecurity.CreateMessageSecurity();
if (messageSecurity != null)
{
bindingElements.Add(messageSecurity);
}
// add encoding
switch (MessageEncoding)
{
case NetHttpMessageEncoding.Text:
bindingElements.Add(TextMessageEncodingBindingElement);
break;
case NetHttpMessageEncoding.Mtom:
bindingElements.Add(MtomMessageEncodingBindingElement);
break;
default:
bindingElements.Add(_binaryMessageEncodingBindingElement);
break;
}
// add transport (http or https)
bindingElements.Add(GetTransport());
return bindingElements.Clone();
}
internal override void SetReaderQuotas(XmlDictionaryReaderQuotas readerQuotas)
{
readerQuotas.CopyTo(_binaryMessageEncodingBindingElement.ReaderQuotas);
}
internal override EnvelopeVersion GetEnvelopeVersion()
{
return EnvelopeVersion.Soap12;
}
private void Initialize()
{
MessageEncoding = NetHttpBindingDefaults.MessageEncoding;
_binaryMessageEncodingBindingElement = new BinaryMessageEncodingBindingElement() { MessageVersion = MessageVersion.Soap12WSAddressing10 };
TextMessageEncodingBindingElement.MessageVersion = MessageVersion.Soap12WSAddressing10;
MtomMessageEncodingBindingElement.MessageVersion = MessageVersion.Soap12WSAddressing10;
_session = new ReliableSessionBindingElement();
_reliableSession = new OptionalReliableSession(_session);
WebSocketSettings.TransportUsage = NetHttpBindingDefaults.TransportUsage;
WebSocketSettings.SubProtocol = WebSocketTransportSettings.SoapSubProtocol;
_basicHttpSecurity = new BasicHttpSecurity();
}
}
}