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 CustomBinding binding;
39 BindingParameterCollection parameters;
41 string listen_uri_relative;
42 ListenUriMode listen_uri_mode;
44 BindingElementCollection elements; // for internal use
46 public BindingContext (CustomBinding binding,
47 BindingParameterCollection parameters)
50 throw new ArgumentNullException ("binding");
51 if (parameters == null)
52 throw new ArgumentNullException ("parameters");
54 this.binding = binding;
55 this.parameters = new BindingParameterCollection ();
56 foreach (var item in parameters)
57 this.parameters.Add (item);
58 this.elements = new BindingElementCollection ();
59 foreach (var item in binding.Elements)
60 this.elements.Add (item);
63 public BindingContext (CustomBinding binding,
64 BindingParameterCollection parameters,
65 Uri listenUriBaseAddress,
66 string listenUriRelativeAddress,
67 ListenUriMode listenUriMode)
68 : this (binding, parameters)
70 listen_uri_base = listenUriBaseAddress;
71 listen_uri_relative = listenUriRelativeAddress;
72 listen_uri_mode = listenUriMode;
75 // deep clone .ctor().
76 BindingContext (CustomBinding binding,
77 BindingParameterCollection parms,
78 BindingElementCollection elems,
79 Uri listenUriBaseAddress,
80 string listenUriRelativeAddress,
81 ListenUriMode listenUriMode)
83 this.binding = new CustomBinding (binding);
84 parameters = new BindingParameterCollection ();
85 foreach (var item in parms)
86 parameters.Add (item);
87 this.elements = new BindingElementCollection ();
88 foreach (var item in elems)
89 this.elements.Add (item);
90 listen_uri_base = listenUriBaseAddress;
91 listen_uri_relative = listenUriRelativeAddress;
92 listen_uri_mode = listenUriMode;
96 public CustomBinding Binding {
97 get { return binding; }
100 public BindingParameterCollection BindingParameters {
101 get { return parameters; }
104 public Uri ListenUriBaseAddress {
105 get { return listen_uri_base; }
106 set { listen_uri_base = value; }
109 public string ListenUriRelativeAddress {
110 get { return listen_uri_relative; }
111 set { listen_uri_relative = value; }
114 public ListenUriMode ListenUriMode {
115 get { return listen_uri_mode; }
116 set { listen_uri_mode = value; }
119 public BindingElementCollection RemainingBindingElements {
121 if (elements == null)
122 elements = binding.CreateBindingElements ();
127 internal BindingElement DequeueBindingElement ()
129 return DequeueBindingElement (true);
132 BindingElement DequeueBindingElement (bool raiseError)
134 if (RemainingBindingElements.Count == 0) {
136 throw new InvalidOperationException ("There is no more available binding element.");
140 BindingElement el = RemainingBindingElements [0];
141 RemainingBindingElements.RemoveAt (0);
145 public IChannelFactory<TChannel>
146 BuildInnerChannelFactory<TChannel> ()
148 BindingContext ctx = this.Clone ();
149 return ctx.DequeueBindingElement ().BuildChannelFactory<TChannel> (ctx);
153 public IChannelListener<TChannel>
154 BuildInnerChannelListener<TChannel> ()
155 where TChannel : class, IChannel
157 BindingContext ctx = this.Clone ();
158 var be = ctx.DequeueBindingElement (false);
160 throw new InvalidOperationException ("There is likely no TransportBindingElement that can build a channel listener in this binding context");
161 return be.BuildChannelListener<TChannel> (ctx);
165 public bool CanBuildInnerChannelFactory<TChannel> ()
167 BindingContext ctx = this.Clone ();
168 return ctx.DequeueBindingElement ().CanBuildChannelFactory<TChannel> (ctx);
172 public bool CanBuildInnerChannelListener<TChannel> ()
173 where TChannel : class, IChannel
175 BindingContext ctx = this.Clone ();
176 var be = ctx.DequeueBindingElement (false);
178 throw new InvalidOperationException ("There is likely no TransportBindingElement that can build a channel listener in this binding context");
179 return be.CanBuildChannelListener<TChannel> (ctx);
183 public T GetInnerProperty<T> () where T : class
185 BindingContext ctx = this.Clone ();
186 BindingElement e = ctx.DequeueBindingElement (false);
187 return e == null ? default (T) : e.GetProperty<T> (ctx);
190 public BindingContext Clone ()
192 return new BindingContext (this.binding, this.parameters, this.elements, this.listen_uri_base, this.listen_uri_relative, this.listen_uri_mode);