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
(4)
5,454 times
Add to favorites
4/27/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
VB.NET
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.1
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On

Imports System.Runtime.Serialization

'
'This code was auto-generated by Microsoft.Silverlight.ServiceReference, version 4.0.50401.0
'
Namespace ServiceReference
    
    <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0"),  _
     System.Runtime.Serialization.DataContractAttribute(Name:="Operation", [Namespace]:="http://schemas.datacontract.org/2004/07/SilverlightService")>  _
    Public Enum Operation As Integer
        
        <System.Runtime.Serialization.EnumMemberAttribute()>  _
        Add = 0
        
        <System.Runtime.Serialization.EnumMemberAttribute()>  _
        Subtract = 1
        
        <System.Runtime.Serialization.EnumMemberAttribute()>  _
        Multiply = 2
        
        <System.Runtime.Serialization.EnumMemberAttribute()>  _
        Divide = 3
    End Enum
    
    <System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"),  _
     System.ServiceModel.ServiceContractAttribute(ConfigurationName:="ServiceReference.Service")>  _
    Public Interface Service
        
        <System.ServiceModel.OperationContractAttribute(AsyncPattern:=true, Action:="http://tempuri.org/Service/Calculate", ReplyAction:="http://tempuri.org/Service/CalculateResponse")>  _
        Function BeginCalculate(ByVal op As ServiceReference.Operation, ByVal a As Integer, ByVal b As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
        
        Function EndCalculate(ByVal result As System.IAsyncResult) As Integer
    End Interface
    
    <System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")>  _
    Public Interface ServiceChannel
        Inherits ServiceReference.Service, System.ServiceModel.IClientChannel
    End Interface
    
    <System.Diagnostics.DebuggerStepThroughAttribute(),  _
     System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")>  _
    Partial Public Class CalculateCompletedEventArgs
        Inherits System.ComponentModel.AsyncCompletedEventArgs
        
        Private results() As Object
        
        Public Sub New(ByVal results() As Object, ByVal exception As System.Exception, ByVal cancelled As Boolean, ByVal userState As Object)
            MyBase.New(exception, cancelled, userState)
            Me.results = results
        End Sub
        
        Public ReadOnly Property Result() As Integer
            Get
                MyBase.RaiseExceptionIfNecessary
                Return CType(Me.results(0),Integer)
            End Get
        End Property
    End Class
    
    <System.Diagnostics.DebuggerStepThroughAttribute(),  _
     System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")>  _
    Partial Public Class ServiceClient
        Inherits System.ServiceModel.ClientBase(Of ServiceReference.Service)
        Implements ServiceReference.Service
        
        Private onBeginCalculateDelegate As BeginOperationDelegate
        
        Private onEndCalculateDelegate As EndOperationDelegate
        
        Private onCalculateCompletedDelegate As System.Threading.SendOrPostCallback
        
        Private onBeginOpenDelegate As BeginOperationDelegate
        
        Private onEndOpenDelegate As EndOperationDelegate
        
        Private onOpenCompletedDelegate As System.Threading.SendOrPostCallback
        
        Private onBeginCloseDelegate As BeginOperationDelegate
        
        Private onEndCloseDelegate As EndOperationDelegate
        
        Private onCloseCompletedDelegate As System.Threading.SendOrPostCallback
        
        Public Sub New()
            MyBase.New
        End Sub
        
        Public Sub New(ByVal endpointConfigurationName As String)
            MyBase.New(endpointConfigurationName)
        End Sub
        
        Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
        
        Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
        
        Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
            MyBase.New(binding, remoteAddress)
        End Sub
        
        Public Property CookieContainer() As System.Net.CookieContainer
            Get
                Dim httpCookieContainerManager As System.ServiceModel.Channels.IHttpCookieContainerManager = Me.InnerChannel.GetProperty(Of System.ServiceModel.Channels.IHttpCookieContainerManager)
                If (Not (httpCookieContainerManager) Is Nothing) Then
                    Return httpCookieContainerManager.CookieContainer
                Else
                    Return Nothing
                End If
            End Get
            Set
                Dim httpCookieContainerManager As System.ServiceModel.Channels.IHttpCookieContainerManager = Me.InnerChannel.GetProperty(Of System.ServiceModel.Channels.IHttpCookieContainerManager)
                If (Not (httpCookieContainerManager) Is Nothing) Then
                    httpCookieContainerManager.CookieContainer = value
                Else
                    Throw New System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC"& _ 
                            "ookieContainerBindingElement.")
                End If
            End Set
        End Property
        
        Public Event CalculateCompleted As System.EventHandler(Of CalculateCompletedEventArgs)
        
        Public Event OpenCompleted As System.EventHandler(Of System.ComponentModel.AsyncCompletedEventArgs)
        
        Public Event CloseCompleted As System.EventHandler(Of System.ComponentModel.AsyncCompletedEventArgs)
        
        <System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)>  _
        Function ServiceReference_Service_BeginCalculate(ByVal op As ServiceReference.Operation, ByVal a As Integer, ByVal b As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult Implements ServiceReference.Service.BeginCalculate
            Return MyBase.Channel.BeginCalculate(op, a, b, callback, asyncState)
        End Function
        
        <System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)>  _
        Function ServiceReference_Service_EndCalculate(ByVal result As System.IAsyncResult) As Integer Implements ServiceReference.Service.EndCalculate
            Return MyBase.Channel.EndCalculate(result)
        End Function
        
        Private Function OnBeginCalculate(ByVal inValues() As Object, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Dim op As ServiceReference.Operation = CType(inValues(0),ServiceReference.Operation)
            Dim a As Integer = CType(inValues(1),Integer)
            Dim b As Integer = CType(inValues(2),Integer)
            Return CType(Me,ServiceReference.Service).BeginCalculate(op, a, b, callback, asyncState)
        End Function
        
        Private Function OnEndCalculate(ByVal result As System.IAsyncResult) As Object()
            Dim retVal As Integer = CType(Me,ServiceReference.Service).EndCalculate(result)
            Return New Object() {retVal}
        End Function
        
        Private Sub OnCalculateCompleted(ByVal state As Object)
            If (Not (Me.CalculateCompletedEvent) Is Nothing) Then
                Dim e As InvokeAsyncCompletedEventArgs = CType(state,InvokeAsyncCompletedEventArgs)
                RaiseEvent CalculateCompleted(Me, New CalculateCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState))
            End If
        End Sub
        
        Public Overloads Sub CalculateAsync(ByVal op As ServiceReference.Operation, ByVal a As Integer, ByVal b As Integer)
            Me.CalculateAsync(op, a, b, Nothing)
        End Sub
        
        Public Overloads Sub CalculateAsync(ByVal op As ServiceReference.Operation, ByVal a As Integer, ByVal b As Integer, ByVal userState As Object)
            If (Me.onBeginCalculateDelegate Is Nothing) Then
                Me.onBeginCalculateDelegate = AddressOf Me.OnBeginCalculate
            End If
            If (Me.onEndCalculateDelegate Is Nothing) Then
                Me.onEndCalculateDelegate = AddressOf Me.OnEndCalculate
            End If
            If (Me.onCalculateCompletedDelegate Is Nothing) Then
                Me.onCalculateCompletedDelegate = AddressOf Me.OnCalculateCompleted
            End If
            MyBase.InvokeAsync(Me.onBeginCalculateDelegate, New Object() {op, a, b}, Me.onEndCalculateDelegate, Me.onCalculateCompletedDelegate, userState)
        End Sub
        
        Private Function OnBeginOpen(ByVal inValues() As Object, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Return CType(Me,System.ServiceModel.ICommunicationObject).BeginOpen(callback, asyncState)
        End Function
        
        Private Function OnEndOpen(ByVal result As System.IAsyncResult) As Object()
            CType(Me,System.ServiceModel.ICommunicationObject).EndOpen(result)
            Return Nothing
        End Function
        
        Private Sub OnOpenCompleted(ByVal state As Object)
            If (Not (Me.OpenCompletedEvent) Is Nothing) Then
                Dim e As InvokeAsyncCompletedEventArgs = CType(state,InvokeAsyncCompletedEventArgs)
                RaiseEvent OpenCompleted(Me, New System.ComponentModel.AsyncCompletedEventArgs(e.[Error], e.Cancelled, e.UserState))
            End If
        End Sub
        
        Public Overloads Sub OpenAsync()
            Me.OpenAsync(Nothing)
        End Sub
        
        Public Overloads Sub OpenAsync(ByVal userState As Object)
            If (Me.onBeginOpenDelegate Is Nothing) Then
                Me.onBeginOpenDelegate = AddressOf Me.OnBeginOpen
            End If
            If (Me.onEndOpenDelegate Is Nothing) Then
                Me.onEndOpenDelegate = AddressOf Me.OnEndOpen
            End If
            If (Me.onOpenCompletedDelegate Is Nothing) Then
                Me.onOpenCompletedDelegate = AddressOf Me.OnOpenCompleted
            End If
            MyBase.InvokeAsync(Me.onBeginOpenDelegate, Nothing, Me.onEndOpenDelegate, Me.onOpenCompletedDelegate, userState)
        End Sub
        
        Private Function OnBeginClose(ByVal inValues() As Object, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Return CType(Me,System.ServiceModel.ICommunicationObject).BeginClose(callback, asyncState)
        End Function
        
        Private Function OnEndClose(ByVal result As System.IAsyncResult) As Object()
            CType(Me,System.ServiceModel.ICommunicationObject).EndClose(result)
            Return Nothing
        End Function
        
        Private Sub OnCloseCompleted(ByVal state As Object)
            If (Not (Me.CloseCompletedEvent) Is Nothing) Then
                Dim e As InvokeAsyncCompletedEventArgs = CType(state,InvokeAsyncCompletedEventArgs)
                RaiseEvent CloseCompleted(Me, New System.ComponentModel.AsyncCompletedEventArgs(e.[Error], e.Cancelled, e.UserState))
            End If
        End Sub
        
        Public Overloads Sub CloseAsync()
            Me.CloseAsync(Nothing)
        End Sub
        
        Public Overloads Sub CloseAsync(ByVal userState As Object)
            If (Me.onBeginCloseDelegate Is Nothing) Then
                Me.onBeginCloseDelegate = AddressOf Me.OnBeginClose
            End If
            If (Me.onEndCloseDelegate Is Nothing) Then
                Me.onEndCloseDelegate = AddressOf Me.OnEndClose
            End If
            If (Me.onCloseCompletedDelegate Is Nothing) Then
                Me.onCloseCompletedDelegate = AddressOf Me.OnCloseCompleted
            End If
            MyBase.InvokeAsync(Me.onBeginCloseDelegate, Nothing, Me.onEndCloseDelegate, Me.onCloseCompletedDelegate, userState)
        End Sub
        
        Protected Overrides Function CreateChannel() As ServiceReference.Service
            Return New ServiceClientChannel(Me)
        End Function
        
        Private Class ServiceClientChannel
            Inherits ChannelBase(Of ServiceReference.Service)
            Implements ServiceReference.Service
            
            Public Sub New(ByVal client As System.ServiceModel.ClientBase(Of ServiceReference.Service))
                MyBase.New(client)
            End Sub
            
            Public Function BeginCalculate(ByVal op As ServiceReference.Operation, ByVal a As Integer, ByVal b As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult Implements ServiceReference.Service.BeginCalculate
                Dim _args((3) - 1) As Object
                _args(0) = op
                _args(1) = a
                _args(2) = b
                Dim _result As System.IAsyncResult = MyBase.BeginInvoke("Calculate", _args, callback, asyncState)
                Return _result
            End Function
            
            Public Function EndCalculate(ByVal result As System.IAsyncResult) As Integer Implements ServiceReference.Service.EndCalculate
                Dim _args((0) - 1) As Object
                Dim _result As Integer = CType(MyBase.EndInvoke("Calculate", _args, result),Integer)
                Return _result
            End Function
        End Class
    End Class
End Namespace