Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / BindingContext.cs
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.ServiceModel;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Security;
33
34 namespace System.ServiceModel.Channels
35 {
36         public class BindingContext
37         {
38                 CustomBinding binding;
39                 BindingParameterCollection parameters;
40                 Uri listen_uri_base;
41                 string listen_uri_relative;
42                 ListenUriMode listen_uri_mode;
43
44                 BindingElementCollection elements; // for internal use
45
46                 public BindingContext (CustomBinding binding,
47                         BindingParameterCollection parameters)
48                 {
49                         if (binding == null)
50                                 throw new ArgumentNullException ("binding");
51                         if (parameters == null)
52                                 throw new ArgumentNullException ("parameters");
53
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);
61                 }
62
63                 public BindingContext (CustomBinding binding,
64                         BindingParameterCollection parameters,
65                         Uri listenUriBaseAddress,
66                         string listenUriRelativeAddress,
67                         ListenUriMode listenUriMode)
68                         : this (binding, parameters)
69                 {
70                         listen_uri_base = listenUriBaseAddress;
71                         listen_uri_relative = listenUriRelativeAddress;
72                         listen_uri_mode = listenUriMode;
73                 }
74
75                 // deep clone .ctor().
76                 BindingContext (CustomBinding binding,
77                         BindingParameterCollection parms,
78                         BindingElementCollection elems,
79                         Uri listenUriBaseAddress,
80                         string listenUriRelativeAddress,
81                         ListenUriMode listenUriMode)
82                 {
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;
93                 }
94
95
96                 public CustomBinding Binding {
97                         get { return binding; }
98                 }
99
100                 public BindingParameterCollection BindingParameters {
101                         get { return parameters; }
102                 }
103
104                 public Uri ListenUriBaseAddress {
105                         get { return listen_uri_base; }
106                         set { listen_uri_base = value; }
107                 }
108
109                 public string ListenUriRelativeAddress {
110                         get { return listen_uri_relative; }
111                         set { listen_uri_relative = value; }
112                 }
113
114                 public ListenUriMode ListenUriMode {
115                         get { return listen_uri_mode; }
116                         set { listen_uri_mode = value; }
117                 }
118
119                 public BindingElementCollection RemainingBindingElements {
120                         get {
121                                 if (elements == null)
122                                         elements = binding.CreateBindingElements ();
123                                 return elements;
124                         }
125                 }
126
127                 internal BindingElement DequeueBindingElement ()
128                 {
129                         return DequeueBindingElement (true);
130                 }
131
132                 BindingElement DequeueBindingElement (bool raiseError)
133                 {
134                         if (RemainingBindingElements.Count == 0) {
135                                 if (raiseError)
136                                         throw new InvalidOperationException ("There is no more available binding element.");
137                                 else
138                                         return null;
139                         }
140                         BindingElement el = RemainingBindingElements [0];
141                         RemainingBindingElements.RemoveAt (0);
142                         return el;
143                 }
144
145                 public IChannelFactory<TChannel>
146                         BuildInnerChannelFactory<TChannel> ()
147                 {
148                         BindingContext ctx = this.Clone ();
149                         return ctx.DequeueBindingElement ().BuildChannelFactory<TChannel> (ctx);
150                 }
151
152 #if !MOBILE
153                 public IChannelListener<TChannel>
154                         BuildInnerChannelListener<TChannel> ()
155                         where TChannel : class, IChannel
156                 {
157                         BindingContext ctx = this.Clone ();
158                         var be = ctx.DequeueBindingElement (false);
159                         if (be == null)
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);
162                 }
163 #endif
164
165                 public bool CanBuildInnerChannelFactory<TChannel> ()
166                 {
167                         BindingContext ctx = this.Clone ();
168                         return ctx.DequeueBindingElement ().CanBuildChannelFactory<TChannel> (ctx);
169                 }
170
171 #if !MOBILE
172                 public bool CanBuildInnerChannelListener<TChannel> ()
173                         where TChannel : class, IChannel
174                 {
175                         BindingContext ctx = this.Clone ();
176                         var be = ctx.DequeueBindingElement (false);
177                         if (be == null)
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);
180                 }
181 #endif
182
183                 public T GetInnerProperty<T> () where T : class
184                 {
185                         BindingContext ctx = this.Clone ();
186                         BindingElement e = ctx.DequeueBindingElement (false);
187                         return e == null ? default (T) : e.GetProperty<T> (ctx);
188                 }
189
190                 public BindingContext Clone ()
191                 {
192                         return new BindingContext (this.binding, this.parameters, this.elements, this.listen_uri_base, this.listen_uri_relative, this.listen_uri_mode);
193                 }
194         }
195 }