* xbuild/Microsoft.Common.targets (_RecordCleanFile): Append list of
[mcs.git] / class / System.ServiceModel / System.ServiceModel.Channels / BindingContext.cs
blob1dac543ba51fb0136abe4be34486ae5316befa53
1 //
2 // BindingContext.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
28 using System;
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;
45 Uri listen_uri_base;
46 string listen_uri_relative;
47 ListenUriMode listen_uri_mode;
49 public BindingContext (CustomBinding binding,
50 BindingParameterCollection parameters)
52 if (binding == null)
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;
73 // copy .ctor().
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 {
106 get {
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) {
121 if (raiseError)
122 throw new InvalidOperationException ("There is no more available binding element.");
123 else
124 return null;
126 BindingElement el = elements [0];
127 elements.RemoveAt (0);
128 return el;
131 private bool PrepareElements ()
133 if (elements != empty_collection)
134 return false;
135 elements = binding.CreateBindingElements ();
136 return true;
139 public IChannelFactory<TChannel>
140 BuildInnerChannelFactory<TChannel> ()
142 bool restore = PrepareElements ();
143 try {
144 return DequeueBindingElement ().BuildChannelFactory<TChannel> (this);
145 } finally {
146 if (restore)
147 elements = empty_collection;
151 #if !NET_2_1
152 public IChannelListener<TChannel>
153 BuildInnerChannelListener<TChannel> ()
154 where TChannel : class, IChannel
156 bool restore = PrepareElements ();
157 try {
158 return DequeueBindingElement ().BuildChannelListener<TChannel> (this);
159 } finally {
160 if (restore)
161 elements = empty_collection;
164 #endif
166 public bool CanBuildInnerChannelFactory<TChannel> ()
168 bool restore = PrepareElements ();
169 try {
170 return elements.Count > 0 &&
171 DequeueBindingElement ().CanBuildChannelFactory<TChannel> (this);
172 } finally {
173 if (restore)
174 elements = empty_collection;
178 #if !NET_2_1
179 public bool CanBuildInnerChannelListener<TChannel> ()
180 where TChannel : class, IChannel
182 bool restore = PrepareElements ();
183 try {
184 return elements.Count > 0 &&
185 DequeueBindingElement ().CanBuildChannelListener<TChannel> (this);
186 } finally {
187 if (restore)
188 elements = empty_collection;
191 #endif
193 public T GetInnerProperty<T> () where T : class
195 bool restore = PrepareElements ();
196 try {
197 BindingElement e = DequeueBindingElement (false);
198 return e == null ? default (T) : e.GetProperty<T> (this);
199 } finally {
200 if (restore)
201 elements = empty_collection;
205 public BindingContext Clone ()
207 return new BindingContext (this);