共用方式為


服務元件範例

下列範例是包含用戶端和伺服器兩個部分的服務元件。在這個範例中,Account 類別是從 ServicedComponent 類別衍生而來,可確保 Account 物件的內容是裝載在 COM+ 中。在這個範例中會套用下列屬性:

  • TransactionAttribute 套用於 Account 類別,可將交易設定成 Required,相當於 (從 Windows 控制台) 使用元件服務管理工具,在 COM+ 元件上設定交易支援。
  • AutoCompleteAttribute 套用於 Post 方法。如果在方法的執行期間產生未處理的例外狀況,這個屬性會指示 Runtime 自動在交易上呼叫 SetAbort 函式;否則的話,Runtime 會呼叫 SetComplete 函式。

除了在這個範例中使用的屬性以外,也會使用不同組件層級的屬性來提供 COM+ 註冊資訊。服務元件必須具有強式名稱,而且應該放在全域組件快取 (GAC) 中,才能進行手動註冊。如需組件的詳細資訊,請參閱強式名稱的組件

注意 放置在 GAC 中的組件不能使用動態註冊。如果您建立伺服器應用程式,在使用它之前,必須使用 Windows Installer,將組件和組件所根據的任何組件加入至 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+ 服務摘要 | ServicedComponent | System.EnterpriseServices 命名空間