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>
//------------------------------------------------------------------------------

// 
// This code was auto-generated by Microsoft.Silverlight.ServiceReference, version 4.0.50401.0
// 
namespace SilverlightCalculator.ServiceReference {
    using System.Runtime.Serialization;
    
    
    [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 : int {
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Add = 0,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Subtract = 1,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Multiply = 2,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Divide = 3,
    }
    
    [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")]
        System.IAsyncResult BeginCalculate(SilverlightCalculator.ServiceReference.Operation op, int a, int b, System.AsyncCallback callback, object asyncState);
        
        int EndCalculate(System.IAsyncResult result);
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface ServiceChannel : SilverlightCalculator.ServiceReference.Service, System.ServiceModel.IClientChannel {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class CalculateCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
        
        private object[] results;
        
        public CalculateCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
                base(exception, cancelled, userState) {
            this.results = results;
        }
        
        public int Result {
            get {
                base.RaiseExceptionIfNecessary();
                return ((int)(this.results[0]));
            }
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class ServiceClient : System.ServiceModel.ClientBase<SilverlightCalculator.ServiceReference.Service>, SilverlightCalculator.ServiceReference.Service {
        
        private BeginOperationDelegate onBeginCalculateDelegate;
        
        private EndOperationDelegate onEndCalculateDelegate;
        
        private System.Threading.SendOrPostCallback onCalculateCompletedDelegate;
        
        private BeginOperationDelegate onBeginOpenDelegate;
        
        private EndOperationDelegate onEndOpenDelegate;
        
        private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
        
        private BeginOperationDelegate onBeginCloseDelegate;
        
        private EndOperationDelegate onEndCloseDelegate;
        
        private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
        
        public ServiceClient() {
        }
        
        public ServiceClient(string endpointConfigurationName) : 
                base(endpointConfigurationName) {
        }
        
        public ServiceClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public ServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public ServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress) {
        }
        
        public System.Net.CookieContainer CookieContainer {
            get {
                System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
                if ((httpCookieContainerManager != null)) {
                    return httpCookieContainerManager.CookieContainer;
                }
                else {
                    return null;
                }
            }
            set {
                System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
                if ((httpCookieContainerManager != null)) {
                    httpCookieContainerManager.CookieContainer = value;
                }
                else {
                    throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
                            "ookieContainerBindingElement.");
                }
            }
        }
        
        public event System.EventHandler<CalculateCompletedEventArgs> CalculateCompleted;
        
        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
        
        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.IAsyncResult SilverlightCalculator.ServiceReference.Service.BeginCalculate(SilverlightCalculator.ServiceReference.Operation op, int a, int b, System.AsyncCallback callback, object asyncState) {
            return base.Channel.BeginCalculate(op, a, b, callback, asyncState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        int SilverlightCalculator.ServiceReference.Service.EndCalculate(System.IAsyncResult result) {
            return base.Channel.EndCalculate(result);
        }
        
        private System.IAsyncResult OnBeginCalculate(object[] inValues, System.AsyncCallback callback, object asyncState) {
            SilverlightCalculator.ServiceReference.Operation op = ((SilverlightCalculator.ServiceReference.Operation)(inValues[0]));
            int a = ((int)(inValues[1]));
            int b = ((int)(inValues[2]));
            return ((SilverlightCalculator.ServiceReference.Service)(this)).BeginCalculate(op, a, b, callback, asyncState);
        }
        
        private object[] OnEndCalculate(System.IAsyncResult result) {
            int retVal = ((SilverlightCalculator.ServiceReference.Service)(this)).EndCalculate(result);
            return new object[] {
                    retVal};
        }
        
        private void OnCalculateCompleted(object state) {
            if ((this.CalculateCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.CalculateCompleted(this, new CalculateCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void CalculateAsync(SilverlightCalculator.ServiceReference.Operation op, int a, int b) {
            this.CalculateAsync(op, a, b, null);
        }
        
        public void CalculateAsync(SilverlightCalculator.ServiceReference.Operation op, int a, int b, object userState) {
            if ((this.onBeginCalculateDelegate == null)) {
                this.onBeginCalculateDelegate = new BeginOperationDelegate(this.OnBeginCalculate);
            }
            if ((this.onEndCalculateDelegate == null)) {
                this.onEndCalculateDelegate = new EndOperationDelegate(this.OnEndCalculate);
            }
            if ((this.onCalculateCompletedDelegate == null)) {
                this.onCalculateCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCalculateCompleted);
            }
            base.InvokeAsync(this.onBeginCalculateDelegate, new object[] {
                        op,
                        a,
                        b}, this.onEndCalculateDelegate, this.onCalculateCompletedDelegate, userState);
        }
        
        private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
            return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
        }
        
        private object[] OnEndOpen(System.IAsyncResult result) {
            ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
            return null;
        }
        
        private void OnOpenCompleted(object state) {
            if ((this.OpenCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void OpenAsync() {
            this.OpenAsync(null);
        }
        
        public void OpenAsync(object userState) {
            if ((this.onBeginOpenDelegate == null)) {
                this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
            }
            if ((this.onEndOpenDelegate == null)) {
                this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
            }
            if ((this.onOpenCompletedDelegate == null)) {
                this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
            }
            base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
        }
        
        private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
            return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
        }
        
        private object[] OnEndClose(System.IAsyncResult result) {
            ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
            return null;
        }
        
        private void OnCloseCompleted(object state) {
            if ((this.CloseCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void CloseAsync() {
            this.CloseAsync(null);
        }
        
        public void CloseAsync(object userState) {
            if ((this.onBeginCloseDelegate == null)) {
                this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
            }
            if ((this.onEndCloseDelegate == null)) {
                this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
            }
            if ((this.onCloseCompletedDelegate == null)) {
                this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
            }
            base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
        }
        
        protected override SilverlightCalculator.ServiceReference.Service CreateChannel() {
            return new ServiceClientChannel(this);
        }
        
        private class ServiceClientChannel : ChannelBase<SilverlightCalculator.ServiceReference.Service>, SilverlightCalculator.ServiceReference.Service {
            
            public ServiceClientChannel(System.ServiceModel.ClientBase<SilverlightCalculator.ServiceReference.Service> client) : 
                    base(client) {
            }
            
            public System.IAsyncResult BeginCalculate(SilverlightCalculator.ServiceReference.Operation op, int a, int b, System.AsyncCallback callback, object asyncState) {
                object[] _args = new object[3];
                _args[0] = op;
                _args[1] = a;
                _args[2] = b;
                System.IAsyncResult _result = base.BeginInvoke("Calculate", _args, callback, asyncState);
                return _result;
            }
            
            public int EndCalculate(System.IAsyncResult result) {
                object[] _args = new object[0];
                int _result = ((int)(base.EndInvoke("Calculate", _args, result)));
                return _result;
            }
        }
    }
}