TcpChannelFactory.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // TcpChannelFactory.cs
  3. //
  4. // Author:
  5. // Marcos Cobena ([email protected])
  6. //
  7. // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
  8. //
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Net;
  12. using System.Net.Security;
  13. using System.ServiceModel;
  14. using System.ServiceModel.Description;
  15. using System.ServiceModel.Security;
  16. using System.Text;
  17. using System.Xml;
  18. namespace System.ServiceModel.Channels.NetTcp
  19. {
  20. internal class TcpChannelInfo
  21. {
  22. public TcpChannelInfo (TransportBindingElement element, MessageEncoder encoder, XmlDictionaryReaderQuotas readerQuotas)
  23. {
  24. this.BindingElement = element;
  25. this.MessageEncoder = encoder;
  26. this.ReaderQuotas = readerQuotas ?? new XmlDictionaryReaderQuotas ();
  27. }
  28. public TransportBindingElement BindingElement { get; private set; }
  29. public MessageEncoder MessageEncoder { get; private set; }
  30. public XmlDictionaryReaderQuotas ReaderQuotas { get; private set; }
  31. }
  32. internal class TcpChannelFactory<TChannel> : TransportChannelFactoryBase<TChannel>
  33. {
  34. TcpChannelInfo info;
  35. public TcpChannelFactory (TcpTransportBindingElement source, BindingContext ctx)
  36. : base (source, ctx)
  37. {
  38. XmlDictionaryReaderQuotas quotas = null;
  39. foreach (BindingElement be in ctx.Binding.Elements) {
  40. MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
  41. if (mbe != null) {
  42. MessageEncoder = CreateEncoder<TChannel> (mbe);
  43. quotas = mbe.GetProperty<XmlDictionaryReaderQuotas> (ctx);
  44. break;
  45. }
  46. }
  47. if (MessageEncoder == null)
  48. MessageEncoder = new BinaryMessageEncoder ();
  49. info = new TcpChannelInfo (source, MessageEncoder, quotas);
  50. }
  51. protected override TChannel OnCreateChannel (
  52. EndpointAddress address, Uri via)
  53. {
  54. ThrowIfDisposedOrNotOpen ();
  55. var targetUri = via ?? address.Uri;
  56. if (info.BindingElement.Scheme != targetUri.Scheme)
  57. throw new ArgumentException (String.Format ("Argument EndpointAddress has unsupported URI scheme: {0}", targetUri.Scheme));
  58. Type t = typeof (TChannel);
  59. if (t == typeof (IDuplexSessionChannel))
  60. return (TChannel) (object) new TcpDuplexSessionChannel (this, info, address, targetUri);
  61. if (t == typeof (IRequestChannel))
  62. return (TChannel) (object) new TcpRequestChannel (this, info, address, targetUri);
  63. throw new InvalidOperationException (String.Format ("Channel type {0} is not supported.", typeof (TChannel).Name));
  64. }
  65. }
  66. }