Example #1
0
        private static System.ServiceModel.Channels.Message DeserialzieMessageFromStream(MemoryStream ms, System.ServiceModel.Channels.MessageVersion messageVersion)
        {
            var bmebe = new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement();

            bmebe.MessageVersion = messageVersion;
            bmebe.ReaderQuotas   = XmlDictionaryReaderQuotas.Max;
            var bmef = bmebe.CreateMessageEncoderFactory();

            return(bmef.Encoder.ReadMessage(ms, int.MaxValue));
        }
Example #2
0
        private static MemoryStream SerializeMessageToStream(System.ServiceModel.Channels.Message requestMessage)
        {
            var bmebe = new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement();

            bmebe.MessageVersion = requestMessage.Version;
            bmebe.ReaderQuotas   = XmlDictionaryReaderQuotas.Max;
            var bmef = bmebe.CreateMessageEncoderFactory();
            var ms   = new MemoryStream(64 * 1024); // 64K to keep out of LOH

            bmef.Encoder.WriteMessage(requestMessage, ms);
            ms.Position = 0;
            return(ms);
        }
Example #3
0
        protected override System.ServiceModel.Channels.Binding AdjustBinding(System.ServiceModel.Channels.Binding binding)
        {
            System.ServiceModel.Channels.Binding result = binding;

            binding.SendTimeout = TimeSpan.MaxValue;
            binding.ReceiveTimeout = TimeSpan.MaxValue;

            // [XC-9600] - large byte buffers throw exceptions.
            #region Set timeouts and other binding class specific values

            // This is horrible, why couldn't these bindings all implement the same interface.
            //if (binding is System.ServiceModel.NetNamedPipeBinding)
            //{
            //    var polyBinding = (binding as System.ServiceModel.NetNamedPipeBinding);
            //}

            //if (binding is System.ServiceModel.NetPeerTcpBinding)
            //{
            //    var polyBinding = (binding as System.ServiceModel.NetPeerTcpBinding);
            //}

            if (binding is System.ServiceModel.NetTcpBinding)
            {
                var polyBinding = (binding as System.ServiceModel.NetTcpBinding);
                polyBinding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
                polyBinding.ReliableSession.Ordered = true;
                polyBinding.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;
            }

            if (binding is System.ServiceModel.WSHttpBindingBase)
            {
                var polyBinding = (binding as System.ServiceModel.WSHttpBindingBase);
                polyBinding.ReliableSession.Ordered = true;
                polyBinding.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;
            }

            if (binding is System.ServiceModel.WSDualHttpBinding)
            {
                var polyBinding = (binding as System.ServiceModel.WSDualHttpBinding);
                polyBinding.ReliableSession.Ordered = true;
                polyBinding.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;
            }

            #endregion // Set timeouts and other binding class specific values

            // WCF - making things really complicated, one step at a time.

            // Ok, so unfortunately cannot access the transport directly in the binding.
            // So we have to use a "CustomBinding" wrapping it, which allows access to the TransportBindingElement element
            // (bit bizarre since BindingElementCollection is created dynamically on demand but CustomBinding bakes it at construction time)
            // which in turn allows access to the MaxReceivedMessageSize property, as specified in the exception message for reading too many bytes.
            System.ServiceModel.Channels.CustomBinding cb = new System.ServiceModel.Channels.CustomBinding(binding);
            System.ServiceModel.Channels.TransportBindingElement transport = cb.Elements.Find<System.ServiceModel.Channels.TransportBindingElement>();

            if (transport != null)
            {
                transport.MaxReceivedMessageSize = int.MaxValue;

            }

            // XML serialization may also get its knickers in a twist for which MaxReceivedMessageSize, it does nothing.
            // Changing MaxArrayLength in the encoding quotas on the other hand...
            System.ServiceModel.Channels.BinaryMessageEncodingBindingElement encoding = cb.Elements.Find<System.ServiceModel.Channels.BinaryMessageEncodingBindingElement>();

            if (encoding != null)
            {
                encoding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            }

            result = cb;

            return result;
        }