次の方法で共有


サービス コンポーネントの例

クライアントおよびサーバーの 2 つの部分を持つサービス コンポーネントの例を次に示します。この例では、Account クラスは、ServicedComponent クラスから派生します。これにより、Account オブジェクトのコンテキストは COM+ で管理されます。この例では、次の属性が適用されます。

  • TransactionAttribute   Account クラスに適用してトランザクションを Required に設定します。この属性を適用することは、Windows のコントロール パネルから利用可能なコンポーネント サービス管理ツールを使用して COM+ コンポーネントにトランザクション サポートを設定することと同じです。
  • AutoCompleteAttribute   Post メソッドに適用します。この属性は、メソッドの実行中に未処理例外が発生した場合に自動的にトランザクションの SetAbort 関数を呼び出すように、ランタイムに指示します。この属性を適用しなかった場合、ランタイムは SetComplete 関数を呼び出します。

この例で使用されている属性の他に、COM+ 登録情報を指定するためにアセンブリ レベルのさまざまな属性が使用されます。サービス コンポーネントには厳密な名前を割り当てる必要があります。また、サービス コンポーネントは、手動登録のためにグローバル アセンブリ キャッシュ (GAC) に配置します。アセンブリの追加情報については、「厳密な名前付きアセンブリ」を参照してください。

メモ   GAC に配置されているアセンブリの場合は、動的登録は使用できません。サーバー アプリケーションを作成した場合は、サーバー アプリケーションを使用する前に、Windows インストーラを使用して、サーバー アプリケーションが依存するすべてのアセンブリを GAC に追加する必要があります。追加しないと、アプリケーションによって例外が生成されます。

BankComponent サーバー

Imports System.EnterpriseServices
Imports System.Runtime.CompilerServices
Imports System.Reflection

' Supply the COM+ application name. 
<assembly: ApplicationName("BankComponent")>
' Supply a strong-named assembly.
<assembly: AssemblyKeyFileAttribute("BankComponent.snk")>

Namespace BankComponent
      <Transaction(TransactionOption.Required)> _
      Public Class Account 
Inherits ServicedComponent
            <AutoComplete()> _
            Public Sub  Post(accountNum As Integer, amount As Double)
                  ' Updates the database; no need to call SetComplete.
                  ' Calls SetComplete automatically if no exception is generated.
            End Sub 
      End Class 
End Namespace 
[C#]
using System.EnterpriseServices;
using System.Runtime.CompilerServices;
using System.Reflection;

// Supply the COM+ application name.
[assembly: ApplicationName("BankComponent")]
// Supply a strong-named assembly.
[assembly: AssemblyKeyFileAttribute("BankComponent.snk")]

namespace BankComponent
{
      [Transaction(TransactionOption.Required)]
      public class Account : ServicedComponent
      {
            [AutoComplete] 
            public bool Post(int accountNum, double amount)
            {
                /* Updates the database; no need to call SetComplete.
                   Calls SetComplete automatically if no exception is
                   generated. */
            return false;     
            } 
      }
}

BankComponent クライアント

Imports BankComponent
Public Class Client 
   Shared Sub Main()
      Dim Account As New Account()
      ' Post money into the account. 
      Account.Post(5, 100)
   End Sub
End Class
[C#]
using BankComponent;
namespace BankComponentConsoleClient
{
      class Client
      {
            public static int Main() 
            {
                  try
                  {
                        Account act = new Account();
                        // Post money into the account.
                        act.Post(5, 100);
                        return(0);
                  }
                  catch
                  {
                        return(1);
                  }
            }
      }
}

Makefile.bat

このサーバーおよびクライアントは、次のようにコンパイルできます。

sn –k BankComponent.snk
vbc /t:library /r:System.EnterpriseServices.dll Bank.vb
vbc /r:Bank.dll /r:System.EnterpriseServices.dll BankClient.vb
[C#]
sn –k BankComponent.snk
csc /t:library /r:System.EnterpriseServices.dll Bank.cs
csc /r:Bank.dll BankClient.cs

参照

サービス コンポーネントの作成 | サービス コンポーネントの概要 | COM+ サービスを設定する属性の適用 | サービス コンポーネントの登録 | 利用可能な COM+ サービスの概要 | ServicedComponent クラス | System.EnterpriseServices