Implementing asynchronous calls and remoting events

These questions are based on Cert-70-554VB – MSCert: UPGRADE – MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer Using the Microsoft .NET Framework: Part 2 – Section 1 Microsoft Developer Self-Test Software Practice Test.


Objective: Implementing asynchronous calls and remoting events
SubObjective: Call Web methods asynchronously


Item Number: Cert-70-554VB.4.1.1
Single Answer, Multiple Choice


You are an enterprise application developer at AccuTech Corp. You are developing an ASP.NET 2.0 Web application for customers to renew their existing insurance policies. The Web application consumes a Web service named InsuranceSolutionService.


The InsuranceSolutionService Web service is defined as follows:


Public Class InsuranceSolutionService
  Inherits WebService
                <WebMethod()> _
                Public Function RenewPolicy(ByVal number As Integer, _
                  ByVal amount As Single, ByVal duration As Integer) As Boolean
                                ‘Implementation code
                End Function
End Class


One of the Web pages, RenewPolicy.aspx, has a button for customers to renew their existing policies. When a user clicks the RenewPolicyButton Web control in the client Web application, the RenewPolicy method should be invoked before displaying the next Web page to the user. You enable the asynchronous operations on the page.


The RenewPolicy method takes a significant amount of time to complete execution. While the RenewPolicy method is executing, the user should be taken to the next page in the Web application. Which code should you use to invoke the RenewPolicy Web method?



  1. Protected Sub RenewPolicyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                    Dim proxy As New InsuranceSolutionService()
                    proxy.RenewPolicy(policyNumber, amount, 365)
                    Server.Transfer(“RenewalProcess.aspx”)
    End Sub


  2. Protected Sub RenewPolicyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                    Dim proxy As New InsuranceSolutionService()
                    AddHandler proxy.RenewPolicyCompleted, AddressOf RenewPolicyCompleted
                    proxy.RenewPolicyAsync(policyNumber, amount, 365)
                    Server.Transfer(“RenewalProcess.aspx”)
    End Sub

    Protected Sub RenewPolicyCompleted(ByVal sender As Object, _
      ByVal args As RenewPolicyCompletedEventArgs)
                    ‘process the results
    End Sub


  3. Protected Sub RenewPolicyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                    Dim proxy As New InsuranceSolutionService()
                    Dim aResult As IAsyncResult = _
                      proxy.BeginRenewPolicy(policyNumber, amount, 365, Nothing, Nothing)
                    aResult.AsyncWaitHandle.WaitOne()
                    proxy.EndRenewPolicy(aResult)
                    Server.Transfer(“RenewalProcess.aspx”)
    End Sub


  4. Protected Sub RenewPolicyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                    Dim proxy As New InsuranceSolutionService()
                    proxy.RenewPolicy( policyNumber, amount, 365 )
                    Response.Redirect( “RenewalProcess.aspx” )
    End Sub

Answer:
B. Protected Sub RenewPolicyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                Dim proxy As New InsuranceSolutionService()
                AddHandler proxy.RenewPolicyCompleted, AddressOf RenewPolicyCompleted
                proxy.RenewPolicyAsync(policyNumber, amount, 365)
                Server.Transfer(“RenewalProcess.aspx”)
End Sub


Protected Sub RenewPolicyCompleted(ByVal sender As Object, _
  ByVal args As RenewPolicyCompletedEventArgs)
                ‘process the results
End Sub


Tutorial:
You should use the following code to invoke the RenewPolicy method asynchronously and take the user to the next page in the meantime:


Protected Sub RenewPolicyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                Dim proxy As New InsuranceSolutionService()
                AddHandler proxy.RenewPolicyCompleted, AddressOf RenewPolicyCompleted
                proxy.RenewPolicyAsync(policyNumber, amount, 365)
                Server.Transfer(“RenewalProcess.aspx”)
End Sub


Protected Sub RenewPolicyCompleted(ByVal sender As Object, _
  ByVal args As RenewPolicyCompletedEventArgs)
                ‘process the results
End Sub


This code uses the RenewPolicyCompleted event and invokes the RenewPolicyAsync method for asynchronous programming. The code then invokes the Transfer method on the Server object to transfer processing to the next page while waiting for the asynchronous result.


In the .NET Framework 2.0, Web methods can be called asynchronously using the proxy method XXXAsync and the XXXCompleted event. For example, if the synchronous method was named GetData, then the asynchronous method wo

Like what you see? Share it.Google+LinkedInFacebookRedditTwitterEmail
cmadmin

ABOUT THE AUTHOR

Posted in Archive|

Comment: