/
NetTcpBinding.cs
185 lines (163 loc) · 6.17 KB
/
NetTcpBinding.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.ServiceModel.Channels;
using System.Xml;
namespace System.ServiceModel
{
public class NetTcpBinding : Binding
{
private OptionalReliableSession _reliableSession;
// private BindingElements
private TcpTransportBindingElement _transport;
private BinaryMessageEncodingBindingElement _encoding;
private ReliableSessionBindingElement _session;
private NetTcpSecurity _security = new NetTcpSecurity();
public NetTcpBinding()
{
Initialize();
}
public NetTcpBinding(SecurityMode securityMode)
: this()
{
_security.Mode = securityMode;
}
public NetTcpBinding(SecurityMode securityMode, bool reliableSessionEnabled) : this(securityMode)
{
ReliableSession.Enabled = reliableSessionEnabled;
}
[DefaultValue(ConnectionOrientedTransportDefaults.TransferMode)]
public TransferMode TransferMode
{
get { return _transport.TransferMode; }
set { _transport.TransferMode = value; }
}
[DefaultValue(TcpTransportDefaults.MaxBufferPoolSize)]
public long MaxBufferPoolSize
{
get { return _transport.MaxBufferPoolSize; }
set { _transport.MaxBufferPoolSize = value; }
}
[DefaultValue(TcpTransportDefaults.MaxBufferSize)]
public int MaxBufferSize
{
get { return _transport.MaxBufferSize; }
set { _transport.MaxBufferSize = value; }
}
public int MaxConnections
{
get { return _transport.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint; }
set { _transport.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = value; }
}
[DefaultValue(TcpTransportDefaults.MaxReceivedMessageSize)]
public long MaxReceivedMessageSize
{
get { return _transport.MaxReceivedMessageSize; }
set { _transport.MaxReceivedMessageSize = value; }
}
public XmlDictionaryReaderQuotas ReaderQuotas
{
get { return _encoding.ReaderQuotas; }
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
}
value.CopyTo(_encoding.ReaderQuotas);
}
}
public OptionalReliableSession ReliableSession
{
get
{
return _reliableSession;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(value)));
}
_reliableSession.CopySettings(value);
}
}
public override string Scheme { get { return _transport.Scheme; } }
public EnvelopeVersion EnvelopeVersion
{
get { return EnvelopeVersion.Soap12; }
}
public NetTcpSecurity Security
{
get { return _security; }
set
{
_security = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
}
}
private void Initialize()
{
_transport = new TcpTransportBindingElement();
_encoding = new BinaryMessageEncodingBindingElement();
_session = new ReliableSessionBindingElement();
_reliableSession = new OptionalReliableSession(_session);
}
public override BindingElementCollection CreateBindingElements()
{
// return collection of BindingElements
BindingElementCollection bindingElements = new BindingElementCollection();
// order of BindingElements is important
// add session
if (_reliableSession.Enabled)
{
// This check was originall in ReliableSessionBindingElement.VerifyTransportMode but
// the transport base type isn't visible to primitives any more so the check has
// been move here.
if (_transport.TransferMode != TransferMode.Buffered)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new InvalidOperationException(SR.Format(SR.TransferModeNotSupported,
_transport.TransferMode, typeof(ReliableSessionBindingElement).Name)));
}
bindingElements.Add(_session);
}
// add security (*optional)
SecurityBindingElement wsSecurity = CreateMessageSecurity();
if (wsSecurity != null)
{
bindingElements.Add(wsSecurity);
}
// add encoding
bindingElements.Add(_encoding);
// add transport security
BindingElement transportSecurity = CreateTransportSecurity();
if (transportSecurity != null)
{
bindingElements.Add(transportSecurity);
}
_transport.ExtendedProtectionPolicy = _security.Transport.ExtendedProtectionPolicy;
// add transport (tcp)
bindingElements.Add(_transport);
return bindingElements.Clone();
}
private BindingElement CreateTransportSecurity()
{
return _security.CreateTransportSecurity();
}
private SecurityBindingElement CreateMessageSecurity()
{
if (_security.Mode == SecurityMode.Message)
{
throw new PlatformNotSupportedException(nameof(SecurityMode.Message));
}
if (_security.Mode == SecurityMode.TransportWithMessageCredential)
{
return _security.CreateMessageSecurity(ReliableSession.Enabled);
}
else
{
return null;
}
}
}
}