2 // HttpChannelListener.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 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.Collections.ObjectModel;
33 using System.Net.Security;
34 using System.ServiceModel;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Dispatcher;
37 using System.ServiceModel.Security;
40 namespace System.ServiceModel.Channels
42 internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
43 where TChannel : class, IChannel
45 public HttpSimpleChannelListener (HttpTransportBindingElement source,
46 BindingContext context)
47 : base (source, context)
51 object creator_lock = new object ();
53 protected override TChannel CreateChannel (TimeSpan timeout)
56 return CreateChannelCore (timeout);
60 TChannel CreateChannelCore (TimeSpan timeout)
62 if (typeof (TChannel) == typeof (IReplyChannel))
63 return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
65 throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
68 protected override HttpListenerManager CreateListenerManager ()
70 return new HttpSimpleListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
74 internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
75 where TChannel : class, IChannel
77 public AspNetChannelListener (HttpTransportBindingElement source,
78 BindingContext context)
79 : base (source, context)
83 protected override TChannel CreateChannel (TimeSpan timeout)
85 if (typeof (TChannel) == typeof (IReplyChannel))
86 return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
88 throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
91 protected override HttpListenerManager CreateListenerManager ()
93 return new AspNetListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
97 internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>
98 where TChannel : class, IChannel
100 List<TChannel> channels = new List<TChannel> ();
101 MessageEncoder encoder;
102 HttpListenerManager httpChannelManager;
104 internal static HttpChannelListenerBase<TChannel>CurrentHttpChannelListener;
106 public HttpChannelListenerBase (HttpTransportBindingElement source,
107 BindingContext context)
110 if (ServiceHostBase.CurrentServiceHostHack != null)
111 DispatcherBuilder.ChannelDispatcherSetter = delegate (ChannelDispatcher cd) { this.ChannelDispatcher = cd; };
113 this.Source = source;
114 // The null Uri check looks weird, but it seems the listener can be built without it.
115 // See HttpTransportBindingElementTest.BuildChannelListenerWithoutListenUri().
116 if (Uri != null && source.Scheme != Uri.Scheme)
117 throw new ArgumentException (String.Format ("Requested listen uri scheme must be {0}, but was {1}.", source.Scheme, Uri.Scheme));
119 foreach (BindingElement be in context.RemainingBindingElements) {
120 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
122 encoder = CreateEncoder<TChannel> (mbe);
127 encoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
129 if (context.BindingParameters.Contains (typeof (ServiceCredentials)))
130 SecurityTokenManager = new ServiceCredentialsSecurityTokenManager ((ServiceCredentials) context.BindingParameters [typeof (ServiceCredentials)]);
133 internal ChannelDispatcher ChannelDispatcher { get; set; }
135 public HttpTransportBindingElement Source { get; private set; }
137 public HttpListenerManager ListenerManager {
138 get { return httpChannelManager; }
141 public MessageEncoder MessageEncoder {
142 get { return encoder; }
145 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
147 protected override TChannel OnAcceptChannel (TimeSpan timeout)
149 TChannel ch = CreateChannel (timeout);
153 protected abstract TChannel CreateChannel (TimeSpan timeout);
155 protected override bool OnWaitForChannel (TimeSpan timeout)
157 throw new NotImplementedException ();
160 protected abstract HttpListenerManager CreateListenerManager ();
162 protected override void OnOpen (TimeSpan timeout)
164 httpChannelManager = CreateListenerManager ();
165 Properties.Add (httpChannelManager);
166 httpChannelManager.Open (timeout);
169 protected override void OnAbort ()
171 httpChannelManager.Stop (true);
174 protected override void OnClose (TimeSpan timeout)
176 if (State == CommunicationState.Closed)
178 base.OnClose (timeout);
179 // The channels are kept open when the creator channel listener is closed.
180 // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
181 httpChannelManager.Stop (false);