Skip to main content

Elumenotion Blog

Go Search

 
Elumenotion > Elumenotion Blog > Posts > Specifying Reader Quotas for WCF Services in SharePoint 2010
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. ISAPI\MySolution\MyService.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

Comments

how use method ConfigureWebService()

how use method ConfigureWebService()?
Peter at 12/13/2010 5:58 AM

access denied error

contentService.Update();
access denied error
Anon at 1/6/2011 4:20 AM

WcfServiceSettings key must be lowercase

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 :-)
Andi Fandrich at 3/14/2011 9:18 AM

Where do I run this

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?
shaune at 3/16/2011 11:36 AM

RE: Where do I run this

I use a feature event receiver at WebApplication or Farm scope in the package that deploys the WCF service.
Doug at 3/17/2011 8:47 AM

RE: Where do I run this

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.
Dmitry at 8/25/2011 9:32 AM

I had to do this


    /* Must set this to -1, else, the MaxReceivedMessageSize value for
    SPWebService.ContentService.WcfServiceSettings["client.svc"] will not be used.*/
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1;
Jey at 1/26/2012 8:09 AM

Re: Specifying Reader Quotas for WCF Services in SharePoint 2010

Thanks a lot!

Agrees with Dmitry.
Bottger at 3/30/2012 3:31 AM

Thanks !

this save me a lot of time !
diego at 6/3/2012 10:53 AM

Amazing post

I've been looking for this for a long time! Thank you so much
gbelzile at 8/7/2012 4:24 PM

Help me, fix "Bad request 400"

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?
TuanDavi at 8/21/2012 5:54 AM

Re: Specifying Reader Quotas for WCF Services in SharePoint 2010

contentService.WcfServiceSettings contains only listdata.svc, i cannot see my service there, i can browse my wcf service through browser.
Shafaqat ALi at 9/25/2012 2:58 AM

Re: Specifying Reader Quotas for WCF Services in SharePoint 2010

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()
Garry Sinclair at 12/6/2012 1:47 PM

WcfServiceSettings Key

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;
Anon at 3/27/2013 1:11 PM

Both "myservice.svc" and "myfolder/myservice.svc" don't work

Hi, I have read through various blog posts and there seems to be differing opinions on whether you need to include the subfolder name in the key or not. In my case, my service is in a subfolder and I have tried both "myservice.svc" and "myfolder/myservice.svc" without any luck.

I can confirm that the WcfServiceSettings are correctly added, but they just don't seem to have any effect on the actual WCF service (eg. the MaxReceivedMessageSize doesn't get changed).

Anyone know what else I could try?
Anon2 at 4/15/2013 9:05 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your Name *


Attachments
Follow me on twitter!
The Atlanta .NET User Group
MVP Logo
  Archive
  Archive (Calendar)
  Skinner Created Themes
  New Skinner Download
  New Skinner Tutorial

©  2009 Elumenotion, LLC  |   SharePoint Training, SharePoint Consulting and SharePoint Staffing
8075 Cavendish Place | Suwanee, Georgia 30024 | + 1 (888) 653-5021