im making a transport agent for exchange and am getting hung up on how to call a method asyncronously. Im doing this based on an msdn article but like usual they didnt provide the part of the code i need to figure out.
the article is in C# but i converted it to vb in my project.
http://msdn.microsoft.com/en-us/library/cc720860.aspx
I know I need to setup a delegate for the method and then invoke that delegate, but im not 100% on how to implement this.
Here is the code. AsyncDBCallback is the method i am trying to call. Any help would be appreciated.
Code:Imports Microsoft.Exchange.Data.Transport.Routing Imports Microsoft.Exchange.Data.Transport Namespace TestTransportAgent NotInheritable Class TestAgentFactory Inherits RoutingAgentFactory Public Overrides Function CreateAgent(ByVal server As Microsoft.Exchange.Data.Transport.SmtpServer) As Microsoft.Exchange.Data.Transport.Routing.RoutingAgent Return New MyTestAgent End Function End Class Public Class MyTestAgent Inherits RoutingAgent Private Sub SubmittedMessageHandler(ByVal source As SubmittedMessageEventSource, ByVal e As QueuedMessageEventArgs) Handles Me.OnSubmittedMessage Dim asyncState As New AsyncState(Of SubmittedMessageEventSource, QueuedMessageEventArgs)(source, e, Me.GetAgentAsyncContext()) Dim caller As New DelCallBackFunction(AddressOf AsyncDBCallback) 'not sure how to implement it from thsi point Dim result As IAsyncResult = caller.BeginInvoke( End Sub Delegate Sub DelCallBackFunction(ByVal ar As IAsyncResult) Private Sub AsyncDBCallback(ByVal ar As IAsyncResult) Dim asyncState As AsyncState(Of SubmittedMessageEventSource, QueuedMessageEventArgs) = DirectCast(ar.AsyncState, AsyncState(Of SubmittedMessageEventSource, QueuedMessageEventArgs)) 'Check message and send to web service If asyncState.Args.MailItem.Recipients.Contains("[email protected]") And asyncState.Args.MailItem.Message.Subject.Contains("chicken") Then Dim testt As webservice.Service = New webservice.Service Dim msg As String = asyncState.Args.MailItem.Message.Body.ToString Try testt.HelloWorld(msg) Catch ex As Exception Throw New Exception(Err.Description & " " & ex.Message) End Try ElseIf asyncState.Args.MailItem.Recipients.Contains("[email protected]") Then Dim testt As webservice.Service = New webservice.Service testt.HelloWorld("secondstatement") End If asyncState.Complete() End Sub Private Class AsyncState(Of SourceType, ArgsType) Private m_source As SourceType Private m_args As ArgsType Private asyncContext As AgentAsyncContext Public Sub New(ByVal source As SourceType, ByVal args As ArgsType, ByVal asyncContext As AgentAsyncContext) Me.m_source = source Me.m_args = args Me.asyncContext = asyncContext End Sub Public ReadOnly Property Source() As SourceType Get Return Me.m_source End Get End Property Public ReadOnly Property Args() As ArgsType Get Return Me.m_args End Get End Property Public Sub Complete() Me.asyncContext.Complete() Me.m_source = Nothing Me.m_args = Nothing Me.asyncContext = Nothing End Sub End Class End Class End Namespace




Reply With Quote