A Simple Example of a Disconnected Service Agent in VB



In my previous post, I introduced the “Disconnected Application Block”, which is part of the Smart Client Software Factory.   As I stated earlier, I think this provides a powerful way of implementing “Store and Forward” for both desktop and mobile applications.

In this post, I’ve provided a simple example example in VB of a form, service agent and dummy business component which is a bit simpler than the “Quickstarts.DisconnectedAgent” example it is based on.

The form “MainForm” has a command button for submitting requests and another command button for dispatching (running) the queued requests (depending on if the network is available or not).

Public Class MainForm
    Private WithEvents moManager As RequestManager = DatabaseRequestManagerIntializer.Initialize("QueueDatabase")

    Private moMyCalculator As New CalculatorServiceAgent(moManager.Instance.RequestQueue)

    Private Sub cmdDispatchMessages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDispatchMessages.Click
    End Sub

    Private Sub cmdSubmitLocalRequest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmitLocalRequest.Click
        moMyCalculator.Add(1, 1)
    End Sub

End Class


The Service Agent class contains the logic for queuing the request.

The code in the Add method creates a “behaviour” object to determine how the message should behaving in a queuing situation.  It is then used to create a request, which is placed on the “Request Queue”.

This example differs from those provided in the QuickStart in that it uses the “ObjectProxyFactory”.  This factory allows me to invoke a real object instead of a Web Service.   In this case IntegerCalculatorOnlineServiceProxy.

Public Class CalculatorServiceAgent

    Private moRequestQueue As IRequestQueue


    Public Sub New(ByVal requestQueue As IRequestQueue)
        moRequestQueue = requestQueue
    End Sub
    ''' <summary>
    ''' Enqueues a request to the <c>Add</c> web service method through the agent.
    ''' </summary>
    ''' <returns>The unique identifier associated with the request that was enqueued.</returns>
    Public Function Add(ByVal a As Int32, ByVal b As Int32) As Guid

        Dim behavior As New OfflineBehavior()
        behavior.ProxyFactoryType = GetType(ObjectProxyFactory)
        behavior.MaxRetries = 1
        behavior.Stamps = 1
        behavior.Expiration = DateTime.Now + New TimeSpan(0, 0, 30)

        Dim request As New Request()
        request.MethodName = "Add"
        request.Behavior = behavior
        request.CallParameters = New Object() {a, b}

        request.OnlineProxyType = GetType(IntegerCalculatorServiceOnlineProxy)
        request.Endpoint = ""  ' Setting this to blank is important.  If sent to nothing it wont work


    End Function


End Class


And finally, the business logic:

''' <summary>
''' The proxy class is used to invoke the "real" business logic that does the work
''' </summary>
Public Class IntegerCalculatorServiceOnlineProxy
    Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer

        ' This could be a call out to a 3rd party component
        Return a + b

    End Function
End Class

This is nice, but what if I want my code to be informed of the result? 

This will be covered in the next post.


One response »

  1. Pingback: Return Callbacks and the Disconnected Service Agent « An Original Idea

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s