[2008] Trying to call a method asyncronously
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
Re: [2008] Trying to call a method asyncronously
Well to setup a delegate you simply do this (in this example we will pretend the method you are invoking just takes one string as an argument):
vb Code:
Delegate Sub MyDelegate(Byval SomeString As String)
And to invoke it you just do this:
vb.net Code:
Private Sub SomeExampleSub
Invoke(New SomeDelegate(AddressOf TheSubIWantToRun), TheStringIWantToPassIn)
End Sub
Where "TheSubIWantToRun" would be the name of the sub that you actually want to pass this string to and the sub that will do whatever it is you are trying to 'invoke'
Hope that helps :)
Re: [2008] Trying to call a method asyncronously
Im getting "overload resolution failed because no 'Invoke' is accessible" on the invoke part of the code.
Code:
Invoke(New DelCallBackFunction(AddressOf AsyncDBCallback), asyncState)
End Sub
Delegate Sub DelCallBackFunction(ByVal ar As IAsyncResult)
Re: [2008] Trying to call a method asyncronously
Can you post a bit more the code around the bit you just posted so we can see exactly where you are using it?
Re: [2008] Trying to call a method asyncronously
Hope this helps. Also in the msdn link they you might be able to get a gist of whats going on by reading the comments they have, to bad its in c# and they didnt provide a vb.net version
http://msdn.microsoft.com/en-us/library/cc720860.aspx
Code:
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())
Invoke(New DelCallBackFunction(AddressOf AsyncDBCallback), asyncState)
End Sub
Delegate Sub DelCallBackFunction(ByVal ar As IAsyncResult)
Private Sub AsyncDBCallback(ByVal ar As IAsyncResult)
'code that does stuff
End Sub
Private Class AsyncState(Of SourceType, ArgsType)
Private m_source As SourceType
Private m_args As ArgsType
Private asyncContext As AgentAsyncContext
'public properties for each of the above variable
End Class
Re: [2008] Trying to call a method asyncronously
Well that code you just posted is different to the section you posted in your last post lol
The invoke line in this code is not passing in the IAsyncResult object that is required by the delegate you are trying to invoke.
Re: [2008] Trying to call a method asyncronously
whoops my bad.
updated the code, I was messing around with guess I didnt change all the code back.
edit - also in the MSDN article this comment is what im having trouble implementing. That comment is place in the SubmittedMessageHandler right where that invoke code I put is.
// Call an asynchronous I/O method.
// Pass in this.AsyncIOCallback as the callback and
// asyncState as the state argument.
Re: [2008] Trying to call a method asyncronously
The class you're inheriting (RoutingAgent) doesnt seem to expose an Invoke method, so you wont be able to call it like that. However delegates have Invoke methods:
Code:
Dim caller As New DelCallBackFunction(AddressOf AsyncDBCallback)
caller.Invoke(asyncState)
Re: [2008] Trying to call a method asyncronously
Re: [2008] Trying to call a method asyncronously
Ohh I didnt realise Invoke was a method, I thought it was just a command/keyword (like Dim and New etc)... I guess just because it is normally always available because I'm used to always attempting to use it from within a form class. Although the fact its not shown in the same way in intellisense should have given me a clue... but what can I say. Learn something everyday :)