It has always been the case with SharePoint that you sometimes need to develop custom ‘services’ to get a particular job done, and this is no exception with 2010. The provided services have continued to expand/improve over the years when compared back to earlier versions of SharePoint, but they are obviously general and not going to perform every function we might require.
Enter custom WCF Services: a big improvement over ASP .NET web services, and even more so compared to the days when we had to make WebDAV and RPC calls to get the job done.
Creating
There are a number of blog posts that already discuss this process, so I won’t bother repeating this here but simply provide some links:-
I would highly recommend installing and using CKSDev…it makes the whole process a lot simpler with minimal steps required between starting and running.
Standard Approaches to Configuration
Nearly all the examples I could find took one of two approaches when it came to performing configuration on the service:-
1. WCF Dynamic Configuration using one of the three provided Service Factories, most sticking with the MultipleBaseAddressBasicHttpBindingServiceHostFactory.
2. Providing static configuration settings either by modifying the main SharePoint config (yuk!) or providing a config file with the custom service.
Both of these approaches suck – in my opinion – as whilst no. 1 is really cool and just sets everything up for you, you don’t get to make adjustments such as increasing MaxReceivedMessageSize. No.2 means we have to create a config file, and provide all the configuration ourselves…which works, but is more effort and less maintainable, etc..
What we really want is to make use of approach no. 1, but be able to tap in and adjust the configuration according to our specific requirements.
WebApplication Level Feature
This can be achieved by creating a WebApplication level feature that overrides FeatureInstalled to apply any adjustments that you want to make to the WcfServiceSettings for your custom service. There is not much out there that adequately goes into doing this, but thankfully I stumbled across a great blog post by Shaun Donohue that covered this topic allowing me to move forward:
Reader Quotas for WCF Services in SharePoint 2010
This is easily achieved by Adding New Feature:


Then adding an Event Receiver:

Uncommenting FeatureInstalled and putting in your code:
Feature Installed
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
SPWebService contentService = SPWebService.ContentService;
SPWcfServiceSettings wcfServiceSettings = new SPWcfServiceSettings();
wcfServiceSettings.ReaderQuotasMaxStringContentLength = Int32.MaxValue;
wcfServiceSettings.ReaderQuotasMaxArrayLength = Int32.MaxValue;
wcfServiceSettings.ReaderQuotasMaxBytesPerRead = Int32.MaxValue;
wcfServiceSettings.MaxReceivedMessageSize = Int32.MaxValue;
contentService.WcfServiceSettings["MyService.svc"] = wcfServiceSettings;
contentService.Update(true);
}
If you want to debug this, then checkout the following post:
Debugging a FeatureInstalled event handler in SharePoint 2010
Conclusion
Being able to retain the automation provided by the factories is great, and then being able to adjust the configuration is really a necessity for any real-world scenario. The above allows us to achieve this, but given that we are now programmatically configuring our settings there is another way we can achieve this that we’ll take a look at in the next post.
Print | posted on Tuesday, 28 June 2011 11:34 AM