Share via


Creating Custom SharePoint 2010 Service Applications and Consumers

Summary:  Learn how to create custom SharePoint 2010 service applications for robust, scalable, and fault tolerant shared services.

Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Andrew Connell, Critical Path Training, LLC (SharePoint MVP)

Overview

Microsoft Office SharePoint Server 2007 introduced Shared Services Providers (SSPs) to provide common services to web applications. In Microsoft SharePoint 2010, Microsoft changed this model to use the new service application framework, which no longer groups services together for multiple web applications. This new infrastructure enables more flexibility in managing and using the services. In addition, it is now possible to create custom service applications. This Visual How To demonstrates how to create custom SharePoint 2010 service applications and consumers that take advantage of the service.

Code It

Creating custom SharePoint 2010 service applications is a significant task. As this Visual How To demonstrates, there is a considerable amount of work in building up all the plumbing for a service application to support the actual work of the service. The example in the demonstration for this Visual How To is a service application that provides two simple math functions, as follows.

 [ServiceContract]
public interface IWingtipCalcContract
{
    [OperationContract]
    int Add(int x, int y);

    [OperationContract]
    int Subtract(int x, int y);
}

There are three classifications of components that make up a custom service application:

  • The back end—what resides on the SharePoint application servers

  • The front end—what resides on the SharePoint front-end web servers

  • The consumers that leverage or use the service application

Service Application Back-End Components

The back-end components in a service application consist of three pieces. The SPService is the hub for the service application. It contains references to all running instances of the service, timer jobs, and derived service applications. The SPServiceInstance hosts the running service. There is exactly one instance of a SPServiceInstance on a server, which can be installed on multiple servers. The more servers they are installed (and running) on, the more scalable the service application is. The last component is the SPServiceApplication. This is a configuration of the service that knows which databases it uses and implements the interface of the service application, in the case of the example in this Visual How To, IWingtipCalcContract, as shown in the following example.

public class CalcServiceApplication : SPIisWebServiceApplication, IWingtipCalcContract
{
    #region IWingtipCalcContract implementation
    public int Add(int x, int y)
    {
        return x + y;
    }

    public int Subtract(int x, int y)
    {
        return x - y;
    }
    #endregion
}

Service Application Front-End Components

There are two front-end components that make up a service application. The first is the SPServiceProxy, which like the SPService on the back end, acts as the hub for each front-end server. This object contains references to all the proxies connected to back-end service applications. The most important piece on the front end is the SPServiceApplicationProxy. This component's job is to talk directly to a SPServiceApplication running in one of the many SPServiceInstances on a back-end server. Using the internal service application software load balancer, it will get the endpoint address of a Windows Communication Foundation (WCF) service that is hosted by the SPServiceInstance. When a call is made by a consumer, it talks directly to the SPServiceApplicationProxy, which sends the reqest to a specified SPServiceApplication for processing.

Service Application Consumers

The final piece of a custom service application are the consumers. These could be custom WCF services, *.aspx pages, Web Parts, or Windows PowerShell cmdlets—anything that uses the service application. This is done by communicating directly with the SPServiceApplicationProxy and using the SPServiceContext, as shown in the following example.

public int Add(int x, int y)
{
    int result = 0;

    // run the call against the application proxy
    CalcServiceApplicationProxy.Invoke(SPServiceContext.Current,
        proxy => result = proxy.Add(x, y));

    return result;
}      
Read It

Microsoft completely revamped the service infrastructure in SharePoint 2010. This new architecture is much more flexible and scalable than the service offering in earlier versions of SharePoint. It enables administrators to configure services on a case-by-case basis. And instead of grouping them together into SSPs, as in Microsoft Office SharePoint Server 2007, administrators can associate them with web applications on a case-by-case basis. This a la carte configuration provides a great amount of flexibility in arranging services for each web application. Microsoft has also opened up this new architecture to developers to create their own service applications. Although creating custom service applications is a significant undertaking, as shown in this Visual How To, it can provide a great amount of functionality and capabilities in certain situations.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/3880ebb0-b932-4631-a54a-22982f8af38d]

Length: 00:28:18

Click to grab code

Grab the Code from Critical Path Training

Explore It

About the Author

MVP Andrew Connell is an author, instructor, and co-founder of Critical Path Training, a SharePoint education-focused company. Andrew is a six-time recipient of Microsoft’s Most Valuable Professional (MVP) award (2005-2010) for Microsoft Content Management Server (MCMS) and Microsoft SharePoint Server. He authored and contributed to numerous MCMS and SharePoint books over the years including his book Professional SharePoint 2007 Web Content Management Development by WROX. Andrew speaks about SharePoint development and WCM at conferences such as Tech-Ed, SharePoint Connections, VSLive, SharePoint Best Practice Conference, SharePoint Evolutions Conference, Office Developer Conference, and Microsoft SharePoint Conference in the United States, Australia, England, and Spain. Andrew blogs at Andrew Connell Blog and on Twitter @andrewconnell.