2 // TcpChannelFactory.cs
5 // Marcos Cobena (marcoscobena@gmail.com)
7 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
11 using System
.Collections
.Generic
;
13 using System
.Net
.Security
;
14 using System
.ServiceModel
;
15 using System
.ServiceModel
.Description
;
16 using System
.ServiceModel
.Security
;
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
>
42 public TcpChannelFactory (TcpTransportBindingElement source
, BindingContext ctx
)
45 XmlDictionaryReaderQuotas quotas
= null;
46 foreach (BindingElement be
in ctx
.Binding
.Elements
) {
47 MessageEncodingBindingElement mbe
= be
as MessageEncodingBindingElement
;
49 MessageEncoder
= CreateEncoder
<TChannel
> (mbe
);
50 quotas
= mbe
.GetProperty
<XmlDictionaryReaderQuotas
> (ctx
);
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
));