When you want to extend the default API functionality on your SharePoint site, you can add your own webservices to it. There are plenty of articles out there telling you how to do so. But they all have one thing in common; your SVC file mostly ends up in either the _layouts or _vti_bin virtual directories of SharePoint. There’s not much wrong with that, untill you want to deploy to a multi tenant, shared server. Since those virtual directories all point to the same folders on disk, this means your SVC is shared between every SharePoint webapplication on your server.
Mostly this still isn’t a really big problem since your users won’t know the SVC is there and probably don’t have a clue as to what it’s for and how to use it. But let’s face it; it’s not real pretty either. Unfortunately, SharePoint doesn’t really give you any other options. You can place your SVC file in the websites root dir of IIS, sure, but that means manual deployment and no guarantees your file will stay there since SharePoint manages those folders by itself. There’s the option of creating an entire seperate site in IIS which hosts your webservices, but you’d have to manually deploy that to every front-end server in your farm, not great either.
Well, with the help of the community, I found a solution which uses the SharePoint packaging and deployment model and gives you an option to deploy to a specific webapplication! The key is using the local resources folder of a webapplication. Here are the steps to take:
- Add New item… ‘Empty Element’ to your project.
- Delete the elements.xml file which it creates.
- Add your .svc file into the element.
- Right click the .svc file and pick properties.
- Under deployment type, set it to ApplicationResource
- You can customize the Deployment Location, however it will always be under the Resources folder of your inetpub folder (couldnt find a way around this yet)
- Make sure the element is added to your package, and not to any other features.
- Deploy and you will find your service at http://webapp/Resources/service.svc
That’s it! Works like a charm and optimizes security. For your webservice code, it doesn’t really matter that much where the svc file is located.
Oh yeah; make sure you’ve got the TokenReplacement setup correctly if you want Visual Studio to replace the assembly reference in your SVC file properly. Check out http://www.chaholl.com/archive/2010/03/10/how-deploy-a-wcf-service-to-sharepoint-2010.aspx for that.
Thanks to Dennis George for providing this solution on StackExchange!