Simple WCF service within SP2010, note to self

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:

 

  1. 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
image
 
 
Open up the Microsoft.VisualStudio.SharePoint.targets file and locate the TokenReplacementFileExtensions node.
 
 
image
 
Add svc to the list of replacement extensions
<TokenReplacementFileExtensions>$(TokenReplacementFileExtensions);xml;aspx;ascx;webpart;dwp;svc</TokenReplacementFileExtensions>
 
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
 
image
 
debug=”true”
image
 
CallStack=”true”

image
 
customErrors=”Off”
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
 
image
 
When you run the utility via Tools ==> Strong Name you will see something like this screen shot
 
image

Setup your SharePoint solution

1. Create a generic SharePoint 2010 solution
 
image
 
2. Make this a farm solution
 
image
 
3. Add the ISAPI Mapped folder
 
image
 
4. Add a WCF service to the project
 
image
 
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
 
image
 
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
C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client.ServerRuntime\14.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.ServerRuntime.dll
image
 
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
 
<% @ServiceHost
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory,
Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c"
Language="C#"
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
using System.ServiceModel.Activation;
using Microsoft.SharePoint.Client.Services;
[BasicHttpBindingServiceMetadataExchangeEndpoint]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

 
The service code changes as follows
 
image
 
And
 
image
11. Lets test this out by deploying to SharePoint as shown
 
image
 
Our service endpoint will look something like this – http://dev/_vti_bin/MyService/MyService.svc/MEX
 
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 🙂
image
 
This completes these notes – very cool indeed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s