IContentFormatter Interface

Provides the methods by which the distributor interacts with a custom content formatter. A content formatter is used to format notification data for display.

Espace de noms: Microsoft.SqlServer.NotificationServices
Assembly: Microsoft.SqlServer.NotificationServices (in microsoft.sqlserver.notificationservices.dll)

Syntaxe

'Déclaration
Public Interface IContentFormatter
public interface IContentFormatter
public interface class IContentFormatter
public interface IContentFormatter
public interface IContentFormatter

Notes

This interface provides a framework for developing a custom content formatter. Any custom content formatters that you develop must implement this interface to interact with an instance of Notification Services.

The IContentFormatter interface provides the following methods:

  • Initialize allows the distributor to initialize the content formatter and prepare it to start running.

  • FormatContent allows the distributor to pass notification data to the content formatter, so that it can be formatted and then output for use by a delivery protocol. This method processes regular or multicast notifications one at a time, and digest notifications as a group.

  • Close allows the distributor to shut down the content formatter.

The sequence of calls begins with Initialize, followed by a series of FormatContent calls, one for each notification to be formatted. Close is the last method in the sequence to be called. After Close has returned, FormatContent is not called again, unless the calling sequence is started over with a new call to Initialize.

To create a custom content formatter, you must create a class that implements IContentFormatter, and then implement the interface's methods. You then implement these methods as appropriate for your application. For example, if your application provides both regular and digest notifications, your content formatter must be able to correctly format both types of notifications.

For more information about custom content formatters, see Développement d'un module de formatage de contenu personnalisé.

Exemple

Use the following interface declaration to start developing your custom content formatter.

public interface IContentFormatter
{
    void Initialize(
            StringDictionary arguments,
            Boolean digest);
    String FormatContent(
            String subscriberLocale,
            String deviceTypeName,
            RecipientInfo recipientInfo,
            Hashtable[] rawContent);
    void Close();
}

In the following code example, the content formatter concatenates notification text blocks with localized notification values to produce formatted notification content:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.SqlServer.NotificationServices;

namespace AdventureWorks.Notifications.ContentFormatter
{
    //Using the IContentFormatter Interface sample
    public class StringFormatter : IContentFormatter
    {
        private bool digest = false;
        string introText = null;
        string midText = null;
        string endText = null;
        string browseBackUrl = null;

        public StringFormatter()
        {
            //Add constructor logic here if required.
        }

        // Implement the IContentFormatter.Initialize method.
        public void Initialize(StringDictionary arguments,
            bool digest)
        {
            this.digest = digest;

            // This content formatter requires four arguments:
            // the three text blocks to insert as notification
            // text, and the browse-back URL to display.
            if (4 == arguments.Count)
            {
                this.introText = arguments["introText"];
                this.midText = arguments["midText"];
                this.endText = arguments["endText"];
                this.browseBackUrl = arguments["URL"];
            }
            else
            {
                throw new ArgumentException("Inadequate number of arguments supplied.");
            }

            // Validate the values of the 
            // arguments here if needed.
        }

        // Format the notification content.
        public string FormatContent(
            string subscriberLocale,
            string deviceTypeName,
            RecipientInfo recipientInfo,
            Hashtable[] rawContent)
        {
            StringBuilder contentString = new StringBuilder();

            try
            {
                StringWriter writer = new StringWriter(contentString);
                CultureInfo recipientCulture = GetCulture(subscriberLocale);
      
                foreach (Hashtable notification in rawContent) 
                {
                    writer.Write(introText);
                    writer.Write(LocalizeNotificationData(notification["StockSymbol"], recipientCulture));
                    writer.Write(midText);
                    writer.Write(LocalizeNotificationData(notification["StockPrice"], recipientCulture));
                    writer.Write(endText);
                    writer.Write(browseBackUrl);
                }
                writer.Close();
            }
            catch (Exception e) 
            {
                // Add code to handle errors here.
            }
            return contentString.ToString();
        }

        // Gets the culture information for the recipient.
        private CultureInfo GetCulture(string subscriberLocale) 
        {
            CultureInfo culture = null;
            culture = CultureInfo.CreateSpecificCulture(subscriberLocale);
            return culture;
        }

        // Localizes the notification data.
        private string LocalizeNotificationData(object notificationField, CultureInfo recipientCulture)
        {
            string localizedString;
            IFormattable formattable = notificationField as IFormattable;
            if (formattable != null)
            {
                const string DEFAULT_FORMAT = null;
                localizedString = formattable.ToString(DEFAULT_FORMAT, recipientCulture);
            }
            else
            {
                localizedString = notificationField.ToString();
            }
            return localizedString;
        }


        // Implement the IContentFormatter.Close method.
        public void Close() 
        {
            // Release any system resources here.
        }
    }

}

Plateformes

Plateformes de développement

Pour obtenir la liste des plateformes prises en charge, consultez Configuration matérielle et logicielle requise pour l'installation de SQL Server 2005.

Plateformes cibles

Pour obtenir la liste des plateformes prises en charge, consultez Configuration matérielle et logicielle requise pour l'installation de SQL Server 2005.

Voir aussi

Référence

IContentFormatter Members
Microsoft.SqlServer.NotificationServices Namespace