File: BindingContext.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (195 lines) | stat: -rw-r--r-- 6,000 bytes parent folder | download | duplicates (6)
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
//
// BindingContext.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005-2006 Novell, Inc.  http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;

namespace System.ServiceModel.Channels
{
	public class BindingContext
	{
		CustomBinding binding;
		BindingParameterCollection parameters;
		Uri listen_uri_base;
		string listen_uri_relative;
		ListenUriMode listen_uri_mode;

		BindingElementCollection elements; // for internal use

		public BindingContext (CustomBinding binding,
			BindingParameterCollection parameters)
		{
			if (binding == null)
				throw new ArgumentNullException ("binding");
			if (parameters == null)
				throw new ArgumentNullException ("parameters");

			this.binding = binding;
			this.parameters = new BindingParameterCollection ();
			foreach (var item in parameters)
				this.parameters.Add (item);
			this.elements = new BindingElementCollection ();
			foreach (var item in binding.Elements)
				this.elements.Add (item);
		}

		public BindingContext (CustomBinding binding,
			BindingParameterCollection parameters,
			Uri listenUriBaseAddress,
			string listenUriRelativeAddress,
			ListenUriMode listenUriMode)
			: this (binding, parameters)
		{
			listen_uri_base = listenUriBaseAddress;
			listen_uri_relative = listenUriRelativeAddress;
			listen_uri_mode = listenUriMode;
		}

		// deep clone .ctor().
		BindingContext (CustomBinding binding,
			BindingParameterCollection parms,
			BindingElementCollection elems,
			Uri listenUriBaseAddress,
			string listenUriRelativeAddress,
			ListenUriMode listenUriMode)
		{
			this.binding = new CustomBinding (binding);
			parameters = new BindingParameterCollection ();
			foreach (var item in parms)
				parameters.Add (item);
			this.elements = new BindingElementCollection ();
			foreach (var item in elems)
				this.elements.Add (item);
			listen_uri_base = listenUriBaseAddress;
			listen_uri_relative = listenUriRelativeAddress;
			listen_uri_mode = listenUriMode;
		}


		public CustomBinding Binding {
			get { return binding; }
		}

		public BindingParameterCollection BindingParameters {
			get { return parameters; }
		}

		public Uri ListenUriBaseAddress {
			get { return listen_uri_base; }
			set { listen_uri_base = value; }
		}

		public string ListenUriRelativeAddress {
			get { return listen_uri_relative; }
			set { listen_uri_relative = value; }
		}

		public ListenUriMode ListenUriMode {
			get { return listen_uri_mode; }
			set { listen_uri_mode = value; }
		}

		public BindingElementCollection RemainingBindingElements {
			get {
				if (elements == null)
					elements = binding.CreateBindingElements ();
				return elements;
			}
		}

		internal BindingElement DequeueBindingElement ()
		{
			return DequeueBindingElement (true);
		}

		BindingElement DequeueBindingElement (bool raiseError)
		{
			if (RemainingBindingElements.Count == 0) {
				if (raiseError)
					throw new InvalidOperationException ("There is no more available binding element.");
				else
					return null;
			}
			BindingElement el = RemainingBindingElements [0];
			RemainingBindingElements.RemoveAt (0);
			return el;
		}

		public IChannelFactory<TChannel>
			BuildInnerChannelFactory<TChannel> ()
		{
			BindingContext ctx = this.Clone ();
			return ctx.DequeueBindingElement ().BuildChannelFactory<TChannel> (ctx);
		}

#if !MOBILE
		public IChannelListener<TChannel>
			BuildInnerChannelListener<TChannel> ()
			where TChannel : class, IChannel
		{
			BindingContext ctx = this.Clone ();
			var be = ctx.DequeueBindingElement (false);
			if (be == null)
				throw new InvalidOperationException ("There is likely no TransportBindingElement that can build a channel listener in this binding context");
			return be.BuildChannelListener<TChannel> (ctx);
		}
#endif

		public bool CanBuildInnerChannelFactory<TChannel> ()
		{
			BindingContext ctx = this.Clone ();
			return ctx.DequeueBindingElement ().CanBuildChannelFactory<TChannel> (ctx);
		}

#if !MOBILE
		public bool CanBuildInnerChannelListener<TChannel> ()
			where TChannel : class, IChannel
		{
			BindingContext ctx = this.Clone ();
			var be = ctx.DequeueBindingElement (false);
			if (be == null)
				throw new InvalidOperationException ("There is likely no TransportBindingElement that can build a channel listener in this binding context");
			return be.CanBuildChannelListener<TChannel> (ctx);
		}
#endif

		public T GetInnerProperty<T> () where T : class
		{
			BindingContext ctx = this.Clone ();
			BindingElement e = ctx.DequeueBindingElement (false);
			return e == null ? default (T) : e.GetProperty<T> (ctx);
		}

		public BindingContext Clone ()
		{
			return new BindingContext (this.binding, this.parameters, this.elements, this.listen_uri_base, this.listen_uri_relative, this.listen_uri_mode);
		}
	}
}