2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.ServiceModel / System.ServiceModel.Channels / TcpChannelFactory.cs
blob66c60f0cf352b219153131ee645ef00ed6e2260a
1 //
2 // TcpChannelFactory.cs
3 //
4 // Author:
5 // Marcos Cobena (marcoscobena@gmail.com)
6 //
7 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
8 //
10 using System;
11 using System.Collections.Generic;
12 using System.Net;
13 using System.Net.Security;
14 using System.ServiceModel;
15 using System.ServiceModel.Description;
16 using System.ServiceModel.Security;
17 using System.Text;
18 using System.Xml;
20 namespace System.ServiceModel.Channels
22 internal class TcpChannelInfo
24 public TcpChannelInfo (TransportBindingElement element, MessageEncoder encoder, XmlDictionaryReaderQuotas readerQuotas)
26 this.BindingElement = element;
27 this.MessageEncoder = encoder;
28 this.ReaderQuotas = readerQuotas ?? new XmlDictionaryReaderQuotas ();
31 public TransportBindingElement BindingElement { get; private set; }
33 public MessageEncoder MessageEncoder { get; private set; }
35 public XmlDictionaryReaderQuotas ReaderQuotas { get; private set; }
38 internal class TcpChannelFactory<TChannel> : TransportChannelFactoryBase<TChannel>
40 TcpChannelInfo info;
42 public TcpChannelFactory (TcpTransportBindingElement source, BindingContext ctx)
43 : base (source, ctx)
45 XmlDictionaryReaderQuotas quotas = null;
46 foreach (BindingElement be in ctx.Binding.Elements) {
47 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
48 if (mbe != null) {
49 MessageEncoder = CreateEncoder<TChannel> (mbe);
50 quotas = mbe.GetProperty<XmlDictionaryReaderQuotas> (ctx);
51 break;
54 if (MessageEncoder == null)
55 MessageEncoder = new BinaryMessageEncoder ();
56 info = new TcpChannelInfo (source, MessageEncoder, quotas);
59 protected override TChannel OnCreateChannel (
60 EndpointAddress address, Uri via)
62 ThrowIfDisposedOrNotOpen ();
64 var targetUri = via ?? address.Uri;
65 if (info.BindingElement.Scheme != targetUri.Scheme)
66 throw new ArgumentException (String.Format ("Argument EndpointAddress has unsupported URI scheme: {0}", targetUri.Scheme));
68 Type t = typeof (TChannel);
70 if (t == typeof (IDuplexSessionChannel))
71 return (TChannel) (object) new TcpDuplexSessionChannel (this, info, address, targetUri);
73 if (t == typeof (IRequestChannel))
74 return (TChannel) (object) new TcpRequestChannel (this, info, address, targetUri);
76 throw new InvalidOperationException (String.Format ("Channel type {0} is not supported.", typeof (TChannel).Name));