日期:2012-09-30  浏览次数:20391 次

事务性组件示例


简单的计算器远算不上工作量繁重的业务应用程序,因此我们现在考虑带有对象池的适于 COM+ 事务性组件的应用程序。
最容易管理和配置的组件是由 ServicedComponent 导出的托管代码组件,如以下 C# 示例所示:
using System;using System.Reflection;using System.Runtime.InteropServices;using System.EnterpriseServices;using System.Data;using System.Data.SqlClient;[assembly: ApplicationName("SCTrans")][assembly: ApplicationActivation(ActivationOption.Server,    SoapVRoot="SCTrans")][assembly: AssemblyKeyFile("SCTrans.snk")]namespace SCTrans{  public interface ISCTrans  {   string CountUp (string Key);  }  [ObjectPooling(MinPoolSize=0, MaxPoolSize=25)]  [JustInTimeActivation(true)]  [ClassInterface(ClassInterfaceType.AutoDual)]  [TransactionAttribute(TransactionOption.RequiresNew)]  public class SCTransSQLNC : ServicedComponent, ISCTrans  {   [AutoComplete]   public string CountUp (string Key)   {      _command = new SqlCommand("", _connection);      _command.CommandType = CommandType.Text;      _command.Connection.Open();     _command.CommandText = "UPDATE CallCount WITH (ROWLOCK) SET       CallCount = CallCount + 1 WHERE Machine='" + Key + "'";     _command.ExecuteNonQuery();      _command.Connection.Close();     _numcalls++;     return (_numcalls + " NC " + _guid);   }    protected override bool CanBePooled()   {     return true;    }   private int _numcalls = 0;   private string _guid = Guid.NewGuid().ToString();   private SqlConnection _connection =    new SqlConnection("user id=MyUser;password=My!Password;   database=SoapTest;server=MyServer");   private SqlCommand _command;      }}

要建立并运行此 C# 组件,在完成编辑连接值以连接到 Microsoft SQL Server™ 数据库之后,需要使用 sn.exe 生成 sctrans.snk 加强名称关键字文件,然后在 using 语句中使用程序集引用对其进行编译。如果您在服务器上进行部署,应使用 gacutil.exe(如果正在使用 SDK)或通过 .NET 框架用户界面将程序集放入 GAC,然后运行 regsvcs.exe,注册 COM+ 托管组件。Regsvcs.exe 将使用以下属性,将组件发布为服务器上的 SOAP 端点和服务器(进程外)激活:
[assembly: ApplicationActivation(ActivationOption.Server,    SoapVRoot="CSSoapSQL")]

此组件在每种方法调用中使用不同的事务,具有一个自动完成方法,并被配置为进行缓冲。使用托管和非托管 COM+ 组件时,对象池和事务将如所预期的那样通过 SOAP 运行。例如,如果使用下列 VBScript 通过 SOAP 访问以下 ServicedComponent
mon = "soap:wsdl=http://jnoss3/sctrans/SCTrans.SCTransSQLNC.soap?WSDL"WScript.Echo(mon)for i = 1 to 2 set c = GetObject(mon) for j = 1 to 10  WScript.Echo i & " " & j & " " & c.CountUp("SCWKONC")  nextnext

将显示以下输出内容:
C:\moniker>actscwkoMicrosoft (R) Windows Script Host Version 5.6Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.soap:wsdl=http://jnoss3/sctrans/SCTrans.SCTransSQLNC.soap?WSDL1 1 486 NC 6e41f32f-74be-45f0-94c0-989e7e1c5672 1 2 487 NC 6e41f32f-74be-45f0-94c0-989e7e1c5672 1 3 488 NC 6e41f32f-74be-45f0-94c0-989e7e1c5672 1 4 489 NC 6e41f32f-74be-45f0-94c0-989e7e1c5672 1 5 490 NC 6e41f32f-74be-45f0-94c0-989e7e1c5672 1 6  8 NC af26b53b-4a1f-48c8-8880-518c2b55a7ce 1 7  9 NC af26b53b-4a1f-48c8-8880-518c2b55a7ce 1 8 10 NC af26b53b-4a1f-48c8-8880-518c2b55a7ce 1 9 494 NC 6e41f32f-74be-45f0-94c0-989e7e1c5672 1 10 495 NC 6e41f32f-74be-45f0-94c0-989e7e1c5672 2 1 13 NC af26b53b-4a1f-48c8-8880-518c2b55a7ce 2 2 14 NC af26b53b-4a1f-48c8-8880-518c2b55a7ce 2 3 15 NC af26b53b-4a1f-48c8-8880-518c2b55a7ce 2 4 499 NC 6e41f32f-7