SharePoint Calculator Service Part 1 – WCF Contract

This is the first in a series of articles where we’ll build a simple calculator service to demonstrate how a web service can be integrated into SharePoint.

We’ll start by building a WCF service with a single method, the “Add” method, which takes two integer parameters and returns their sum as an integer.

Here’s the service contract in ICalculatorServiceContract.cs:

internal interface ICalculatorServiceContract
    int Add(int a, int b);

And here’s the contract implementation in CalculatorServiceContract.cs:

 internal sealed class CalculatorServiceContract : ICalculatorServiceContract
    public int Add(int a, int b)
        return a + b;

Since we’ll be hosting this service in IIS, we need an svc file to enable the service to be activated. I’ve named it calculator.svc:

  Service="Sample.Calculator.Service.CalculatorServiceContract, Sample.Calculator.Service, Version=, Culture=neutral, PublicKeyToken=d5d38459f57e2f46"

Note that I’ve signed my assembly (indicated by the PublicKeyToken parameter), which is a requirement since my assembly will live in the Global Assembly Cache (GAC).

And finally, we’ll use a web.config file to configure the service endpoints and bindings, which comes in very handy if an administrator for a particular deployment needs to tweak a setting such as a timeout value:

 <?xml version="1.0" encoding="utf-8" ?>
          bindingConfiguration="CalculatorServiceHttpBinding" />
            <transport clientCredentialType="Ntlm" />
        <anonymousAuthentication enabled="false" />
        <windowsAuthentication enabled="true" />

At this point, if I put my compiled assembly containing my service contract in the GAC, create an IIS web site and put the calculator.svc and web.config files in the site, my service is ready for action.

In Part 2, we’ll integrate this service with the SharePoint deployment model, which will enable SharePoint administrators to easily deploy the service to machines in a server farm.