出于几个原因,我需要在COM+框架4中创建一个.Net组件,目的是在自己的进程(dllhost.exe)中托管组件,从而使用ActivationOption.Server。
我的组件代码需要在对象激活(由工作线程维护)之间持久化数据。此工作线程及其数据保存在我的基类的静态(共享)成员中。共享数据与调用方、其安全上下文、事务等无关。此外,工作线程对数据执行后台处理。
当dllhost进程被释放时,我需要清理数据并有序地终止工作线程。由于没有静态(共享)析构函数,我不知道如何执行。在继承ServicedComponent时,有什么可以实现的吗?还有其他想法吗?谢谢。
下面是一些开始的代码:
Imports System.EnterpriseServices
<Assembly: ApplicationName("MySender")>
<Assembly: ApplicationActivation(ActivationOption.Server)>
<ClassInterface(ClassInterfaceType.None), ProgId("MySender.Sender")> _
<Transaction(EnterpriseServices.TransactionOption.NotSupported)> _
Public Class Sender
Inherits ServicedComponent
Implements SomeLib.IMsgSender
Shared worker As myWorker
Shared sync As New Object
Public Sub MyInstanceMethod(msg as string) Implements SomeLib.IMsgSender.SendMessage
SyncLock sync
If worker Is Nothing Then
worker = New myWorker
worker.StartThread()
End If
End SyncLock
worker.Process(msg)
End Sub
'Something like this does not exist!'
Shared Sub Dispose()
SyncLock sync
If worker IsNot Nothing Then
worker.StopThread()
End If
End SyncLock
End Sub
End Class发布于 2015-10-03 00:33:47
AppDomain.ProcessExit事件将在卸载域之前触发。如果运行的代码不需要太长时间,可以这样使用:
Imports System.EnterpriseServices
<Assembly: ApplicationName("MySender")>
<Assembly: ApplicationActivation(ActivationOption.Server)>
<ClassInterface(ClassInterfaceType.None), ProgId("MySender.Sender")> _
<Transaction(EnterpriseServices.TransactionOption.NotSupported)> _
Public Class Sender
Shared Sub New
AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf MyDisposalCode
End Sub
'....
Shared Sub MyDisposalCode(sender as Object, e as EventArgs)
'My disposal code
End Sub
End Class需要注意的是,.Net将在这段代码上强制执行2秒超时。
https://stackoverflow.com/questions/32659436
复制相似问题