5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System
.Collections
.Generic
;
30 using System
.ServiceModel
;
31 using System
.ServiceModel
.Description
;
32 using System
.ServiceModel
.Security
;
34 namespace System
.ServiceModel
.Channels
36 public class BindingContext
38 static BindingElementCollection empty_collection
=
39 new BindingElementCollection ();
41 CustomBinding binding
;
42 BindingParameterCollection parameters
;
43 BindingElementCollection elements
= empty_collection
;
44 BindingElementCollection remaining_elements
;
46 string listen_uri_relative
;
47 ListenUriMode listen_uri_mode
;
49 public BindingContext (CustomBinding binding
,
50 BindingParameterCollection parameters
)
53 throw new ArgumentNullException ("binding");
54 if (parameters
== null)
55 throw new ArgumentNullException ("parameters");
57 this.binding
= binding
;
58 this.parameters
= parameters
;
61 public BindingContext (CustomBinding binding
,
62 BindingParameterCollection parameters
,
63 Uri listenUriBaseAddress
,
64 string listenUriRelativeAddress
,
65 ListenUriMode listenUriMode
)
66 : this (binding
, parameters
)
68 listen_uri_base
= listenUriBaseAddress
;
69 listen_uri_relative
= listenUriRelativeAddress
;
70 listen_uri_mode
= listenUriMode
;
74 private BindingContext (BindingContext other
)
76 binding
= other
.binding
;
77 parameters
= other
.parameters
;
78 elements
= other
.elements
== empty_collection
?
79 empty_collection
: other
.elements
.Clone ();
82 public CustomBinding Binding
{
83 get { return binding; }
86 public BindingParameterCollection BindingParameters
{
87 get { return parameters; }
90 public Uri ListenUriBaseAddress
{
91 get { return listen_uri_base; }
92 set { listen_uri_base = value; }
95 public string ListenUriRelativeAddress
{
96 get { return listen_uri_relative; }
97 set { listen_uri_relative = value; }
100 public ListenUriMode ListenUriMode
{
101 get { return listen_uri_mode; }
102 set { listen_uri_mode = value; }
105 public BindingElementCollection RemainingBindingElements
{
107 if (remaining_elements
== null)
108 remaining_elements
= new BindingElementCollection ();
109 return remaining_elements
;
113 internal BindingElement
DequeueBindingElement ()
115 return DequeueBindingElement (true);
118 BindingElement
DequeueBindingElement (bool raiseError
)
120 if (elements
.Count
== 0) {
122 throw new InvalidOperationException ("There is no more available binding element.");
126 BindingElement el
= elements
[0];
127 elements
.RemoveAt (0);
131 private bool PrepareElements ()
133 if (elements
!= empty_collection
)
135 elements
= binding
.CreateBindingElements ();
139 public IChannelFactory
<TChannel
>
140 BuildInnerChannelFactory
<TChannel
> ()
142 bool restore
= PrepareElements ();
144 return DequeueBindingElement ().BuildChannelFactory
<TChannel
> (this);
147 elements
= empty_collection
;
152 public IChannelListener
<TChannel
>
153 BuildInnerChannelListener
<TChannel
> ()
154 where TChannel
: class, IChannel
156 bool restore
= PrepareElements ();
158 return DequeueBindingElement ().BuildChannelListener
<TChannel
> (this);
161 elements
= empty_collection
;
166 public bool CanBuildInnerChannelFactory
<TChannel
> ()
168 bool restore
= PrepareElements ();
170 return elements
.Count
> 0 &&
171 DequeueBindingElement ().CanBuildChannelFactory
<TChannel
> (this);
174 elements
= empty_collection
;
179 public bool CanBuildInnerChannelListener
<TChannel
> ()
180 where TChannel
: class, IChannel
182 bool restore
= PrepareElements ();
184 return elements
.Count
> 0 &&
185 DequeueBindingElement ().CanBuildChannelListener
<TChannel
> (this);
188 elements
= empty_collection
;
193 public T GetInnerProperty
<T
> () where T
: class
195 bool restore
= PrepareElements ();
197 BindingElement e
= DequeueBindingElement (false);
198 return e
== null ? default (T
) : e
.GetProperty
<T
> (this);
201 elements
= empty_collection
;
205 public BindingContext
Clone ()
207 return new BindingContext (this);