WCF Azure Silverlight Calculator

This sample creates a simple WCF calculator service and builds a Silverlight client for it. The client is then hosted alonside the service in the Azure fabric.

4 Star
5,454 times
Add to favorites
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Windows
Imports System.Windows.Controls
Imports ServiceReference
Imports System.Net
Imports System.Net.Browser

Namespace SilverlightCalculator
	Partial Public Class MainPage
		Inherits UserControl
		Private Sub Do_Operation(ByVal sender As Object, ByVal e As RoutedEventArgs)
			flushDisplay = True
			Dim number As Integer = Integer.Parse(display.Text)
			Dim newOperation As String = CStr((CType(sender, Button)).Content)

			If newOperation = "=" Then
				Dim proxy As New ServiceClient()
				AddHandler proxy.CalculateCompleted, AddressOf proxy_CalculateCompleted

				Select Case previousOperation
					Case "+"
						proxy.CalculateAsync(Operation.Add, value, number)
					Case "-"
						proxy.CalculateAsync(Operation.Subtract, value, number)
					Case "*"
						proxy.CalculateAsync(Operation.Multiply, value, number)
					Case "/"
						proxy.CalculateAsync(Operation.Divide, value, number)
				End Select
				previousOperation = newOperation
				value = number
			End If

		End Sub

		Private Sub proxy_CalculateCompleted(ByVal sender As Object, ByVal e As CalculateCompletedEventArgs)
			If e.Error Is Nothing Then
				value = e.Result
				display.Text = value.ToString()
			End If
		End Sub

		#Region "UI methods"

		Public Sub New()
			WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp)
		End Sub

		Private Sub Add_Didgit(ByVal sender As Object, ByVal e As RoutedEventArgs)
			If flushDisplay Then
				display.Text = ""
				flushDisplay = False
			End If
			display.Text += (CType(sender, Button)).Content
		End Sub

		Private previousOperation As String
		Private value As Integer
		Private flushDisplay As Boolean = False
		#End Region

	End Class
End Namespace