It’s been a busy year personally and professionally but even more is coming up in August and September. From a blog perspective I’ve lapsed a bit these past several months. The good news is that I’ve been lucky enough to have developed some SharePoint 2010 solutions including a Service Application, Features, simple WCF Services, and Silverlight web parts. Of course, the catch-22 is finding the time to blog so I can refer back to what I write – which is a real benefit for me.
This post is a “note to self” with regard to developing a simple WCF service from within SharePoint 2010. These notes refer to a basic farm-wide accessible service in the style of a “traditional” SP 2007 SOAP service originating from the ISAPI folder, or perhaps a SharePoint 2010 REST service like ListData.svc. These notes do not cover “Service Application” development in any way, which is a different animal. This material can, in some form or another, be found on the web already including MSDN, See – http://msdn.microsoft.com/en-us/library/ff521581.aspx
. My intent here is to group this material with some other related material I found very useful.
Preliminary – your solution properties should be set to .net v3.5 / x64
Setting up Visual Studio 2010 and web.config:
- Setup token replacement, see http://msdn.microsoft.com/en-us/library/ee231545.aspx
Essentially, this minimizes the need to manually add things like a 4-part assembly name. After all, the solution already has that information so why not have VS2010 do the work for you. You can also use a tool like ReSharper – anywho – you can setup the basics by following this procedure.
Navigate to Program files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools
Open up the Microsoft.VisualStudio.SharePoint.targets file and locate the TokenReplacementFileExtensions node.
Add svc to the list of replacement extensions
2. Be sure your SharePoint web.config has debugging fully enabled, most of us know this drill by now.
Navigate to the SharePoint sites web.config, example, C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config and set the following
3. Another useful tool that I encourage you to use is described here via Sahil Malik – http://blah.winsmarts.com
(a great SP resource)
Basically, setup Powershell within Tools ==> External Tools, resulting in this command line
Powershell.exe -command [System.Reflection.AssemblyName]::GetAssemblyName("$(TargetPath)").FullName
My own screenshot of Sahil’s utility appears as follows – be sure the output window is visible <Ctrl>–<Alt>-O
When you run the utility via Tools ==> Strong Name you will see something like this screen shot
Setup your SharePoint solution
1. Create a generic SharePoint 2010 solution
2. Make this a farm solution
3. Add the ISAPI Mapped folder
4. Add a WCF service to the project
At this point you should see two additional files in your project, IService1.cs and Service1.cs. It’s really up to you on how your want to arrange the service code. For example, you could combine the code into a single file or place one or both in a folder – etc, etc
5. Add a descriptive folder under ISAPI, I call this one MyService. Next, add a file to be used as the service endpoint. For now, I use an empty text file called MyService.svc.
6. The solution should look as follows
7. Next we need to make a reference to Microsoft.SharePoint.Client.ServerRuntime this reference contains the service factories (plumbing) we need for the next step. This was found at the following path on my machine
8. At this point we need to pull in plumbing for WCF to use. This also means we need to decide exactly what kind of service endpoint we’re talking about. We have some choices now – see http://msdn.microsoft.com/en-us/library/ff521586.aspx
. From the MSDN link we can see SOAP, REST, ADO.NET Data Services. In this case I am selecting SOAP as the endpoint – so the MultipleBaseAddressBasicHttpBindingServiceHostFactory
will be used
9. Next we need to add this factory class to our empty MyService.svc text file we added earlier, as well as the 4-part name of the assembly and related code. This all goes in @ServiceHost and looks as follows
Microsoft.SharePoint.Client.ServerRuntime, Version=18.104.22.168, Culture=neutral,
Service="SampleServices.Service1, $SharePoint.Project.AssemblyFullName$" %>
Note: SampleService is the assembly name, Service1 was auto-generated when I added the WCF service to the project.
10. Having selected the SOAP factory as our basic plumbing we need to do something useful with the service itself – oh, I know – how about “Hello World”. First we need to add these attributes to the Service1 class
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
The service code changes as follows
11. Lets test this out by deploying to SharePoint as shown
I won’t go into invoking this from the client as I assume most of us can work through that piece rather well. In any event, the final result when hitting the URL appears as follows. I’ll spare you the entire response 🙂
This completes these notes – very cool indeed.