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;
39 using System.Threading;
41 namespace System.ServiceModel.Channels
43 internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
44 where TChannel : class, IChannel
46 public HttpSimpleChannelListener (HttpTransportBindingElement source,
47 BindingContext context)
48 : base (source, context)
52 protected override TChannel CreateChannel (TimeSpan timeout)
55 return CreateChannelCore (timeout);
59 TChannel CreateChannelCore (TimeSpan timeout)
61 if (typeof (TChannel) == typeof (IReplyChannel))
62 return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
64 throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
67 protected override HttpListenerManager CreateListenerManager ()
69 return new HttpSimpleListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
73 internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
74 where TChannel : class, IChannel
76 public AspNetChannelListener (HttpTransportBindingElement source,
77 BindingContext context)
78 : base (source, context)
82 protected override TChannel CreateChannel (TimeSpan timeout)
84 if (typeof (TChannel) == typeof (IReplyChannel))
85 return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
87 throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
90 protected override HttpListenerManager CreateListenerManager ()
92 return new AspNetListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
96 internal interface IChannelDispatcherBoundListener
98 ChannelDispatcher ChannelDispatcher { get; set; }
101 internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>, IChannelDispatcherBoundListener
102 where TChannel : class, IChannel
104 HttpListenerManager httpChannelManager;
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.Binding.Elements) {
120 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
122 MessageEncoder = CreateEncoder<TChannel> (mbe);
126 if (MessageEncoder == null)
127 MessageEncoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
129 if (context.BindingParameters.Contains (typeof (ServiceCredentials)))
130 SecurityTokenManager = new ServiceCredentialsSecurityTokenManager ((ServiceCredentials) context.BindingParameters [typeof (ServiceCredentials)]);
133 public ChannelDispatcher ChannelDispatcher { get; set; }
135 public HttpTransportBindingElement Source { get; private set; }
137 public HttpListenerManager ListenerManager {
138 get { return httpChannelManager; }
141 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
143 ManualResetEvent accept_channel_handle = new ManualResetEvent (true);
145 protected override TChannel OnAcceptChannel (TimeSpan timeout)
147 // HTTP channel listeners do not accept more than one channel at a time.
148 DateTime start = DateTime.Now;
149 accept_channel_handle.WaitOne (timeout - (DateTime.Now - start));
150 accept_channel_handle.Reset ();
151 TChannel ch = CreateChannel (timeout - (DateTime.Now - start));
152 ch.Closed += delegate {
153 accept_channel_handle.Set ();
158 protected abstract TChannel CreateChannel (TimeSpan timeout);
160 protected override bool OnWaitForChannel (TimeSpan timeout)
162 throw new NotImplementedException ();
165 protected abstract HttpListenerManager CreateListenerManager ();
167 protected override void OnOpen (TimeSpan timeout)
169 httpChannelManager = CreateListenerManager ();
170 Properties.Add (httpChannelManager);
171 httpChannelManager.Open (timeout);
174 protected override void OnAbort ()
176 httpChannelManager.Stop (true);
179 protected override void OnClose (TimeSpan timeout)
181 if (State == CommunicationState.Closed)
183 base.OnClose (timeout);
184 // The channels are kept open when the creator channel listener is closed.
185 // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
186 httpChannelManager.Stop (false);
189 // immediately stop accepting channel.
190 public override bool CancelAsync (TimeSpan timeout)
193 CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.Zero);
194 } catch (TimeoutException) {