IContentFormatter Interface

ディストリビュータがカスタム コンテンツ フォーマッタと対話する場合に使用するメソッドを提供します。コンテンツ フォーマッタは、通知データを表示する目的でフォーマットするときに使用します。

名前空間: Microsoft.SqlServer.NotificationServices
アセンブリ: Microsoft.SqlServer.NotificationServices (microsoft.sqlserver.notificationservices.dll 内)

構文

'宣言
Public Interface IContentFormatter
public interface IContentFormatter
public interface class IContentFormatter
public interface IContentFormatter
public interface IContentFormatter

解説

このインターフェイスは、カスタム コンテンツ フォーマッタを開発するためのフレームワークです。Notification Services のインスタンスと対話するには、カスタム コンテンツ フォーマッタにこのインスタンスを実装する必要があります。

IContentFormatter インターフェイスには、次のメソッドが用意されています。

  • Initialize では、ディストリビュータが、コンテンツ フォーマッタを初期化し、実行開始を準備することができます。

  • FormatContent では、ディストリビュータがコンテンツ フォーマッタに通知データを渡すことができます。これにより、通知データをフォーマットし、配信プロトコル用に出力できます。このメソッドでは、標準通知やマルチキャスト通知は一度に 1 つずつ処理され、ダイジェスト通知はグループとして処理されます。

  • Close では、ディストリビュータがコンテンツ フォーマッタをシャットダウンできます。

呼び出しのシーケンスは、Initialize で始まり、次に、フォーマットされる通知ごとに一連の FormatContent が呼び出されます。Close は、このシーケンスで最後に呼び出されるメソッドです。Close が完了した後、新しい Initialize を使用して一連のメソッド呼び出しをやり直さない限り、FormatContent が再度呼び出されることはありません。

カスタム コンテンツ フォーマッタを作成するには、IContentFormatter を実装するクラスを作成してから、インターフェイスのメソッドを実装する必要があります。次に、これらのメソッドを必要に応じてアプリケーションに実装します。たとえば、アプリケーションで標準の通知とダイジェスト通知の両方を使用する場合、コンテンツ フォーマッタでどちらの通知も正しくフォーマットできる必要があります。

カスタム コンテンツ フォーマッタの詳細については、「カスタム コンテンツ フォーマッタの開発」を参照してください。

使用例

カスタム コンテンツ フォーマッタを作成するには、次のインターフェイス宣言を使用します。

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

次のコード例では、コンテンツ フォーマッタは、通知のテキスト ブロックとローカライズされた通知の値を連結して、フォーマット済みの通知コンテンツを生成しています。

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.
        }
    }

}

プラットフォーム

開発プラットフォーム

サポートされているプラットフォームの一覧については、「SQL Server 2005 のインストールに必要なハードウェアおよびソフトウェア」を参照してください。

対象プラットフォーム

サポートされているプラットフォームの一覧については、「SQL Server 2005 のインストールに必要なハードウェアおよびソフトウェア」を参照してください。

参照

関連項目

IContentFormatter Members
Microsoft.SqlServer.NotificationServices Namespace