Advertisement
kipusoep

TextOrMtomEncodingBinding

Apr 20th, 2012
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.ServiceModel.Channels;
  6. using System.ServiceModel.Configuration;
  7. using System.Configuration;
  8. using System.ComponentModel;
  9. using System.Reflection;
  10. using System.Xml;
  11.  
  12. namespace InfoCaster.SocialProxy.lib.TextOrMtom
  13. {
  14.     public class TextOrMtomEncodingElement : BindingElementExtensionElement
  15.     {
  16.         [ConfigurationProperty("maxArrayLength", DefaultValue = "2147483647")]
  17.         public string MaxArrayLength
  18.         {
  19.             get { return (string)base["maxArrayLength"]; }
  20.             set { base["maxArrayLength"] = value; }
  21.         }
  22.  
  23.         [ConfigurationProperty("MaxDepth", DefaultValue = "2147483647")]
  24.         public string MaxDepth
  25.         {
  26.             get { return (string)base["MaxDepth"]; }
  27.             set { base["MaxDepth"] = value; }
  28.         }
  29.  
  30.         [ConfigurationProperty("MaxStringContentLength", DefaultValue = "2147483647")]
  31.         public string MaxStringContentLength
  32.         {
  33.             get { return (string)base["MaxStringContentLength"]; }
  34.             set { base["MaxStringContentLength"] = value; }
  35.         }
  36.  
  37.         [ConfigurationProperty("MaxBytesPerRead", DefaultValue = "2147483647")]
  38.         public string MaxBytesPerRead
  39.         {
  40.             get { return (string)base["MaxBytesPerRead"]; }
  41.             set { base["MaxBytesPerRead"] = value; }
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Gets the reader quotas.
  46.         /// </summary>
  47.         /// <value>The reader quotas.</value>
  48.         [ConfigurationProperty("readerQuotas")]
  49.         public XmlDictionaryReaderQuotasElement ReaderQuotas
  50.         {
  51.             get { return (XmlDictionaryReaderQuotasElement)base["readerQuotas"]; }
  52.             set { base["readerQuotas"] = value; }
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Initializes a new instance of the <see cref="TextOrMtomEncodingElement"/> class.
  57.         /// </summary>
  58.         public TextOrMtomEncodingElement()
  59.         { }
  60.  
  61.         /// <summary>
  62.         /// When overridden in a derived class, gets the <see cref="T:System.Type"/> object that represents the custom binding element.
  63.         /// </summary>
  64.         /// <value></value>
  65.         /// <returns>A <see cref="T:System.Type"/> object that represents the custom binding type.</returns>
  66.         public override Type BindingElementType
  67.         {
  68.             get { return typeof(TextOrMtomEncodingBindingElement); }
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Gets or sets the inner message encoding.
  73.         /// </summary>
  74.         /// <value>The inner message encoding.</value>
  75.         [ConfigurationProperty("messageVersion", DefaultValue = "Soap11")]
  76.         public string MessageVersion
  77.         {
  78.             get { return (string)base["messageVersion"]; }
  79.             set { base["messageVersion"] = value; }
  80.         }
  81.  
  82.         /// <summary>
  83.         /// When overridden in a derived class, returns a custom binding element object.
  84.         /// </summary>
  85.         /// <returns>
  86.         /// A custom <see cref="T:System.ServiceModel.Channels.BindingElement"/> object.
  87.         /// </returns>
  88.         protected override BindingElement CreateBindingElement()
  89.         {
  90.             TextOrMtomEncodingBindingElement bindingElement = new TextOrMtomEncodingBindingElement(this.ReaderQuotas);
  91.             this.ApplyConfiguration(bindingElement);
  92.             return bindingElement;
  93.         }
  94.  
  95.         public override void ApplyConfiguration(BindingElement bindingElement)
  96.         {
  97.             TextOrMtomEncodingBindingElement binding = (TextOrMtomEncodingBindingElement)bindingElement;
  98.             binding.ReaderQuotas.MaxArrayLength = this.ReaderQuotas.MaxArrayLength;
  99.             binding.ReaderQuotas.MaxBytesPerRead = this.ReaderQuotas.MaxBytesPerRead;
  100.             binding.ReaderQuotas.MaxDepth = this.ReaderQuotas.MaxDepth;
  101.             binding.ReaderQuotas.MaxNameTableCharCount = this.ReaderQuotas.MaxNameTableCharCount;
  102.             binding.ReaderQuotas.MaxStringContentLength = this.ReaderQuotas.MaxStringContentLength;
  103.  
  104.             // set the soap version here.
  105.             binding.MessageVersion = System.ServiceModel.Channels.MessageVersion.Soap11;
  106.             PropertyInfo pInfo = typeof(System.ServiceModel.Channels.MessageVersion).GetProperty(this.MessageVersion);
  107.             if (pInfo != null)
  108.                 binding.MessageVersion = (System.ServiceModel.Channels.MessageVersion)pInfo.GetValue(null, null);
  109.  
  110.             base.ApplyConfiguration(bindingElement);
  111.         }
  112.     }
  113.  
  114.     public class TextOrMtomEncodingBindingElement : MessageEncodingBindingElement
  115.     {
  116.         public const string IsIncomingMessageMtomPropertyName = "IncomingMessageIsMtom";
  117.         public const string MtomBoundaryPropertyName = "__MtomBoundary";
  118.         public const string MtomStartInfoPropertyName = "__MtomStartInfo";
  119.         public const string MtomStartUriPropertyName = "__MtomStartUri";
  120.  
  121.         MessageVersion _messageVersion = MessageVersion.Default;
  122.         XmlDictionaryReaderQuotas _readerQuotas;
  123.  
  124.         public XmlDictionaryReaderQuotas ReaderQuotas { get { return this._readerQuotas; } }
  125.  
  126.         public TextOrMtomEncodingBindingElement()
  127.         {
  128.             this._readerQuotas = new XmlDictionaryReaderQuotas();
  129.         }
  130.  
  131.         public TextOrMtomEncodingBindingElement(TextOrMtomEncodingBindingElement elementToBeCloned)
  132.             : base (elementToBeCloned)
  133.         {
  134.             this._messageVersion = elementToBeCloned.MessageVersion;
  135.             elementToBeCloned.ReaderQuotas.CopyTo(this._readerQuotas);
  136.         }
  137.  
  138.         public TextOrMtomEncodingBindingElement(XmlDictionaryReaderQuotasElement xmlDictionaryReaderQuotasElement)
  139.             : this()
  140.         {
  141.             this._readerQuotas.MaxArrayLength = xmlDictionaryReaderQuotasElement.MaxArrayLength;
  142.             this._readerQuotas.MaxBytesPerRead = xmlDictionaryReaderQuotasElement.MaxBytesPerRead;
  143.             this._readerQuotas.MaxDepth = xmlDictionaryReaderQuotasElement.MaxDepth;
  144.             this._readerQuotas.MaxNameTableCharCount = xmlDictionaryReaderQuotasElement.MaxNameTableCharCount;
  145.             this._readerQuotas.MaxStringContentLength = xmlDictionaryReaderQuotasElement.MaxStringContentLength;
  146.         }
  147.  
  148.         public override MessageEncoderFactory CreateMessageEncoderFactory()
  149.         {
  150.             return new TextOrMtomEncoderFactory(this._messageVersion);
  151.         }
  152.  
  153.         public override MessageVersion MessageVersion
  154.         {
  155.             get { return this._messageVersion; }
  156.             set { this._messageVersion = value; }
  157.         }
  158.  
  159.         public override BindingElement Clone()
  160.         {
  161.             return this;
  162.         }
  163.  
  164.         public override T GetProperty<T>(BindingContext context)
  165.         {
  166.             if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
  167.             {
  168.                 return (T)(object)this._readerQuotas;
  169.             }
  170.             else
  171.             {
  172.                 return base.GetProperty<T>(context);
  173.             }
  174.         }
  175.  
  176.         public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
  177.         {
  178.             context.BindingParameters.Add(this);
  179.             return context.BuildInnerChannelFactory<TChannel>();
  180.         }
  181.  
  182.         public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
  183.         {
  184.             context.BindingParameters.Add(this);
  185.             return context.BuildInnerChannelListener<TChannel>();
  186.         }
  187.  
  188.         public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
  189.         {
  190.             return context.CanBuildInnerChannelFactory<TChannel>();
  191.         }
  192.  
  193.         public override bool CanBuildChannelListener<TChannel>(BindingContext context)
  194.         {
  195.             return context.CanBuildInnerChannelListener<TChannel>();
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement