Smart Space 11.1.1: Creating Your First Smart Space Service

"Alert Service" Example

This section describes how to create a custom Smart Space service, which allows consuming gadgets to display alerts (toast messages) to a user.

Open Visual Studio and select File > New > Project. From Visual C# (or Visual Basic), Templates, click Smart Space Service. If you wish, you may modify the service name, the location and the solution name. Click OK. Enter a namespace and click Finish.

Tip: If you change the namespace later, you must edit the type field in the configuration.xml file to match.

Create the Service Interface

In order to expose some functionality from the service you must declare an interface, which will be implemented by the service and can be used by consumers (other gadgets or services) to access the exposed functionality.

To create the interface, right-click the solution name in the Solution Explorer and Select Add > New Project. Select Class Library from the Templates and name the project AlertServiceInterface. After the project is created, add a reference to Oracle.SmartSpace.Sdk.Services assembly. Rename Class1 to IAlertService, and replace the contents of the file with the following:

Visual Basic


Imports Oracle.SmartSpace.Sdk.Services

Public Interface IAlertService
    Inherits IService
    Sub displayAlert(ByVal message As String)
End Interface

C#


using Oracle.SmartSpace.Sdk.Services;

namespace MyNamespace
{
    public interface IAlertService : IService
    {
        void displayAlert(string message);
    }
}

Note: Change the namespace to be identical to the namespace that you chose for the service project. You can change the namespace in Visual Basic by going to Project Properties and setting the namespace in the Application tab. In C#, simply modify the namespace name after the namespace statement.

Implement the Service

Now that the interface is defined, change the declaration of the Service class to specify that it implements IAlertService, instead of IService. Then insert the following code inside the Service class definition:

Visual Basic


Private provider As Provider

Sub New(ByVal provider As Provider)
    Me.provider = provider
End Sub

Sub displayAlert(ByVal message As String) Implements IAlertService.displayAlert
    Me.provider.Services.ToastManager.Show(message)
End Sub

C#


AlertServiceProvider provider;

public AlertService(AlertServiceProvider provider)
{
    this.provider = provider;
}

public void displayAlert(string message)
{
    this.provider.Services.ToastManager.Show(message);
}

The code above implements IAlertService and adds a reference to the provider class. The provider class (which implements the IServicesProvider interface) allows the service to access other framework services through IServicesProvider.Services property. In addition, the provider class is used by the framework to find the service through IServicesProvider.GetServicesList.

Implement the Provider Class

From the Solution Explorer, double-click Provider.cs (or Provider.vb). First, you must provide a reference to the service that you created by instantiating an object of type Service in the Init method. You must also implement IServicesProvider.GetServicesList, which is called by the framework to locate the service. Insert the following code inside the Provider class definition.

Visual Basic


Private service As Service

Public Sub Init() Implements IServicesProvider.Init
    Me.service = New Service(Me)
End Sub

Public Function GetServicesList(Of T As IService)() As T() Implements IServicesProvider.GetServicesList        
    If TypeOf service Is T Then
        Dim serviceInterface As IService = CType(service, IService)
        Return New T() {DirectCast(serviceInterface, T)}
    End If
    Return New T() {}
End Function

C#


private Service service;

public void Init()
{
    instance = new Service(this);
}

public T[] GetServicesList<T>() where T : IService
{
    if (instance is T)
    {
        IService serviceInterface = (IService)service;
        return new T[] { (T)serviceInterface };
    }

    return new T[] { };
}                      

To save and build the solution, press F6.

Debugging and Testing

Start Smart Space client. In Visual Studio, select Debug > Start Debugging.

Tip: You must have the Smart Space client installed and running on your computer to debug. If you run without debugging, you cannot test your service in the Smart Space client.

Your service is successfully initialized and appears in the Smart Space palette under Services. In order to test the functionality exposed by the service, we need publish the service and create a consumer gadget, which will consume the functionality provided by the Alert Service.

Select Debug > Stop Debugging.

Building and Publishing

After you create, debug and test a service you can build it within Visual Studio, which makes it available for publishing.

From Visual Studio Solution Explorer, right-click the gadget and select Build Service.

Specify a location to store the service. The servicename.SmartSpaceService file is created, where servicename is the name of your service.

To make this service available to users, publish it to an existing Smart Space installation. Please see "Publishing a Gadget or Service" in the "Administrator's Help" document in Smart Space Online Help.

Creating a Consumer Gadget

Tip: For a basic tutotorial on creating, debugging and publishing a gadget see Creating Your First Gadget

Create an empty gadget project. For the sake of simplicity, specify as the namespace for the gadget, the namespace that was used for the service. Open the designer and add a TextBox contol named alertTxt to the gadget. Add a Button control named testAlertBtn, and set "Test Alert" as caption. Double-click the button, and add the following code inside the button's click event handler.

Visual Basic


Dim alertService As IAlertServiceVB = Nothing
Dim found() As IService = Me.Services.ServiceFactory.GetServices(Of IAlertServiceVB)()
If (Not (found Is Nothing)) And (found.Length > 0) Then
    alertService = CType(found(0), IAlertServiceVB)
Else
    Services.ToastManager.Show("Echo Service can not be found", "Error")
    Return
End If

alertService.displayAlert(Me.alertTxt.Text)

C#


IAlertService alertService = null;
IService[] found = this.Services.ServiceFactory.GetServices<IAlertService>();
if (found != null && found.Length > 0)
{
    alertService = (IAlertService)found[0];
}
else
{
    Services.ToastManager.Show("Echo Service can not be found", "Error");
    return;
}

alertService.displayAlert(this.alertTxt.Text);

The code above asks the framework to return all services that implement the IAlertService interface and then executes the displayAlert method that is exposed by that interface.

Now you can debug or publish the gadget as usual.