Specifying Reader Quotas for WCF Services in SharePoint 2010

Creating custom WCF services for SharePoint 2010 is very easy. If you read the article here, Creating a Custom WCF Service in SharePoint Foundation, you’ll see how easy. Most of the article deals with the sample service, but only two steps deal with the actual service hosting. All you need to do for most scenarios is deploy an .svc file to the ISAPI folder in the SharePoint root and use the MultipleBaseAddressBasicHttpBindingServiceHostFactory class as your service host factory like so:

<%@ServiceHost
language="c#"


Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory,
Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"


Service="MyAssembly.MyService, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2f6e123a83b3b3e5"

%>

 

That’s it! MultipleBaseAddressBasicHttpBindingServiceHostFactory does all the work required to set up the end points and bindings. You don’t need to do create a web.config file or write any WCF service code.

But, you might see some errors when you use the service like:

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.

–OR–
The remote server returned an unexpected response: (400) Bad Request.

 

Both of these errors are caused by the message size. The first happens when a single parameter exceeds the default length of 8k and the second happens when the overall message size exceeds the default of 65k.

You can change these limits as follows:

//This code makes it possible to pass large messages (up to 10MB)
//To the outward facing service (not the service app)
//i.e. ISAPIMySolutionMyService.svc
//Note:10MB is not the upper limit, it's just what I'm using in this example


private void ConfigureWebService()
{
SPWebService contentService = SPWebService.ContentService;

SPWcfServiceSettings wcfServiceSettings = new SPWcfServiceSettings();
wcfServiceSettings.ReaderQuotasMaxStringContentLength = 10485760;
wcfServiceSettings.ReaderQuotasMaxArrayLength = 2097152;
wcfServiceSettings.ReaderQuotasMaxBytesPerRead = 10485760;
wcfServiceSettings.MaxReceivedMessageSize = 10485760;
contentService.WcfServiceSettings["MyService.svc"] = wcfServiceSettings;

contentService.Update();
}

Author: Doug Ware

Atlanta based entrepreneur, author of many SharePoint books and videos, leader of Atlanta .NET user group, founder of InstantQuick, and SharePoint MVP.

14 Comments

  • Peter

    December 13, 2010, 5:58 am

    how use method ConfigureWebService()?

  • Anon

    January 6, 2011, 4:20 am

    contentService.Update();
    access denied error

  • Andi Fandrich

    March 14, 2011, 9:18 am

    The key used in contentService.WcfServiceSettings["MyService.svc"] and the actual name of the svc file must be lowercase.
    I couldn’t get that to work in any other way and to find this out cost me half a day.

    Apart from this: thanks for the post. It saved me a couple other half days :-)

  • shaune

    March 16, 2011, 11:36 am

    I have spent the better part of a day trying to figure this out. Your post is the only one I have found that talks about this :)
    But how do I use this code?

    It would seem to me that it would have to be on the server, maybe at install or activation but I have not figured that out.

    Where do you run this code from?

  • Doug

    March 17, 2011, 8:47 am

    I use a feature event receiver at WebApplication or Farm scope in the package that deploys the WCF service.

  • Dmitry

    August 25, 2011, 9:32 am

    As mentioned above, you can add a feature to your web service project(if it hasnt got one already) and add an event receiver from which you can call this code. Use FeatureActivated method.
    Then deploy your service project and enjoy new bandwidth.

    In fact, this is the only blog post that really helped me after 2 days of googleing and reading msdn. Everyone else is just copy-pasting the web.config solution which doesnt work when you’re deploying your service through the wsp package without changing the web.config file.
    Too bad this blog is not on the first page if you search by the exception message.

  • Jey

    January 26, 2012, 8:09 am

    /* Must set this to -1, else, the MaxReceivedMessageSize value for
    SPWebService.ContentService.WcfServiceSettings["client.svc"] will not be used.*/
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1;

  • Bottger

    March 30, 2012, 3:31 am

    Thanks a lot!

    Agrees with Dmitry.

  • diego

    June 3, 2012, 10:53 am

    this save me a lot of time !

  • gbelzile

    August 7, 2012, 4:24 pm

    I’ve been looking for this for a long time! Thank you so much

  • TuanDavi

    August 21, 2012, 5:54 am

    Hi! I had “Bad request 400″ when deploy WCF service in sharepoint 2010. Very happy when i read this but i can not know : How use method ConfigureWebService()? Demo?

  • Shafaqat ALi

    September 25, 2012, 2:58 am

    contentService.WcfServiceSettings contains only listdata.svc, i cannot see my service there, i can browse my wcf service through browser.

  • Garry Sinclair

    December 6, 2012, 1:47 pm

    To Shafaqat ALi, you need to add your service name to the collection like this (if it is not listed in the collection):

    contentService.WcfServiceSettings.Add(“myservice.svc”, wcfServiceSettings);
    contentService.Update()

  • Anon

    March 27, 2013, 1:11 pm

    If you are putting your service in a subfolder, then you must include the subfolder in the key. This must all be lowercase. So for a service located at

    http://myserver/_vti_bin/MyFolder/MyService.svc/MEX

    The collection key looks like this:

    contentService.WcfServiceSettings["myfolder/myservice.svc"] = settings;

Comments are closed