Search This Blog

Monday, June 17, 2013

Send Large data with SharePoint Webservice

When ever you need to send lots of lots of data in WCF REST service using custom SharePoint MultipleBaseAddressWebServiceHostFactory then you will encounter following error 

(400) HTTP Bad Request

When you look into service trace you will find the actual root cause of this issue is that the data sent is not supported by default and its failing internally in deserialize process

<ExceptionType>System.ServiceModel.Dispatcher.NetDispatcherFaultException, System.ServiceModel, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter The InnerException message was 'There was an error deserializing the object of type System.String. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'.  Please see InnerException for more details.</Message>

Now if you try to override the settings in web.config then web service will complain about multiple settings for the same end point because factory it self creates the dynamic end points so web.service changes can't be used unless you remove the factory class and do it conventional way.

So what is the solution :

Its easy that we need to just extend two classes


 public class CustomMultipleBaseAddressWebServiceHostFactory : Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory
        protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            return new CustomMultipleBaseServiceHost(serviceType, baseAddresses);


    public class CustomMultipleBaseServiceHost : Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHost
        public CustomMultipleBaseServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)

        protected override void OnOpening()


            foreach (ServiceEndpoint endpoint in this.Description.Endpoints)
                Binding binding = endpoint.Binding;
                if (binding is WebHttpBinding)
                    var web = binding as WebHttpBinding;
                    web.MaxBufferSize = Int32.MaxValue;
                    web.MaxReceivedMessageSize = Int32.MaxValue;
                var myReaderQuotas = new XmlDictionaryReaderQuotas();
                myReaderQuotas.MaxStringContentLength = Int32.MaxValue;
                binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null);

Now if you see we can make the changes to any parameters like maxBufferSize or MaxrecievedMessageSize at the time of opening of channel.

Now instead of using default Microsoft class in the svc file we will use our custom dll reference. Deploy your solution. DO an IISRESET and you will be able to transmit huge data now :)

<%@ ServiceHost Language="C#" 
Factory="CustomMultipleBaseAddressWebServiceHostFactory, $SharePoint.Project.AssemblyFullName$"  %>