VB.NET 编写/部署 COM+服务
华芸智森 2005-02-28 06:03:28
我以前还以为NET不能做COM+(大家千万别笑我!!)。。
后来看了一些资料。做了一个例子,供大家参考一下。。。
1.生成一个强名称.
SN.EXE -k SGKEY.SNK
将生成的SGKEY.SNK COPY 到 BIN 目录和工作目录下。
'*******************************************************
'*******************************************************
2.编写类.
Imports System.EnterpriseServices
'//
'//定义接口,目的是在COM+中显示
Public Interface F_COMPlusServices
Property myName() As String
Function DoTransaction() As String
Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String
Sub QueueTest()
End Interface
'//此类名前面的属性通知 COM+,您要将事务属性设置为“需要”。
'//添加此行代码相当于打开 COM+ 应用程序管理单元,并手动设置此属性。
<TransactionAttribute(TransactionOption.Required)> Public Class COMPlusServices
Inherits ServicedComponent '如果不包含此行,将无法使此组件在 COM+ 下运行
Implements F_COMPlusServices
Dim M_Name As String
Public Sub New()
MyBase.New()
End Sub
Public Property myName() As String Implements F_COMPlusServices.myName
Get
Return M_Name
End Get
Set(ByVal Value As String)
M_Name = Value
End Set
End Property
'//在此方法前面加上 <AutoComplete()> 属性很重要。
'//这表示只要此方法中没有出现异常,当它结束时就会自动调用 SetComplete。
'//如果该方法中存在异常,则 .NET 运行时将自动调用 SetAbort 方法
<AutoComplete()> Public Function DoTransaction() As String Implements F_COMPlusServices.DoTransaction
Return ("COM+ 成功")
End Function
<AutoComplete()> Public Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String Implements F_COMPlusServices.AddStr
Return Str1 & Str2
End Function
Public Sub QueueTest() Implements F_COMPlusServices.QueueTest
System.Diagnostics.EventLog.WriteEntry("COMPlusServces", "队列测试", Diagnostics.EventLogEntryType.Error)
End Sub
End Class
'*******************************************************
'*******************************************************
3.修改 AssemblyInfo , 注意,提供具有强名称的程序集
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.EnterpriseServices
' 程序集的常规信息通过下列
' 属性集控制。更改这些属性值可
' 修改与程序集关联的信息。
' 检查程序集的属性值
<Assembly: AssemblyTitle("")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("")>
<Assembly: AssemblyCopyright("")>
<Assembly: AssemblyTrademark("")>
<Assembly: CLSCompliant(True)>
<Assembly: AssemblyKeyFileAttribute("SGKEY.SNK")> ' 提供具有强名称的程序集
<Assembly: ApplicationNameAttribute("ComPlusExample")> ' 提供 COM+ 应用程序名
'//将 COM+ 应用程序作为服务器程序(进程外)。这是排队组件的要求。
<Assembly: ApplicationActivationAttribute(ActivationOption.Server)>
'//在组件中添加排队支持。使组件可访问 MSMQ 队列,并使其侦听自己的队列以处理消息。
<Assembly: ApplicationQueuingAttribute(Enabled:=True, QueueListenerEnabled:=True)>
'如果该项目向 COM 公开,则下列 GUID 用于类型库的 ID
<Assembly: Guid("CDC18D05-9CFA-4C07-8219-711DB7174A38")>
' 程序集的版本信息由下列 4 个值组成:
'
' 主版本
' 次版本
' 内部版本号
' 修订号
'
' 您可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,方法是按
' 如下所示使用 '*':
<Assembly: AssemblyVersion("1.0.*")>
'*******************************************************
'*******************************************************
4.将程序集添加到全局程序集缓存中
gacutil /i NETDHCOM.dll
'*******************************************************
'*******************************************************
5.将程序集添加到全局程序集缓存中
regsvcs /fc NETDHCOM.dll
'*******************************************************
'*******************************************************
6.将DLL部署到COM+服务中
如果这步你不知道,放200分,会有很多人来回答人你的。
:)
'*******************************************************
'*******************************************************
VB6 调用示例:
Dim M As Object
Private Sub Command1_Click()
With M
MsgBox .AddStr("123", "345")
End With
End Sub
Private Sub Form_Load()
Set M = CreateObject("NETDHCOM.COMPlusServices", "mstop")
End Sub
'*******************************************************
'*******************************************************
VBNET调用示例
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 32)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(136, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(56, 104)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(224, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(368, 293)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objCOMPlus As New Object
objCOMPlus = CreateObject("NETDHCOM.COMPlusServices", "mstop")
TextBox1.Text = objCOMPlus.AddStr("123", "456")
objCOMPlus.mYNAME = "ASDFASdf"
MsgBox(objCOMPlus.MYNAME)
End Sub
End Class