Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#

The Unit Test of Silverlight Applications with Asynchronous Service Callbacks

Rate me:
Please Sign up or sign in to vote.
4.88/5 (6 votes)
18 Feb 2011CPOL13 min read 50.5K   751   17  
This article presents an example on unit testing of Silverlight applications with asynchronous service callbacks.
//------------------------------------------------------------------------------
// <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.50826.0
// 
namespace SilverlightApplication.StudentService {
    using System.Runtime.Serialization;
    
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="Student", Namespace="http://schemas.datacontract.org/2004/07/SilverlightHostWebApplication")]
    public partial class Student : object, System.ComponentModel.INotifyPropertyChanged {
        
        private string IDField;
        
        private string NameField;
        
        private System.DateTime EnrollmentDateField;
        
        private int ScoreField;
        
        private string GenderField;
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        public string ID {
            get {
                return this.IDField;
            }
            set {
                if ((object.ReferenceEquals(this.IDField, value) != true)) {
                    this.IDField = value;
                    this.RaisePropertyChanged("ID");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        public string Name {
            get {
                return this.NameField;
            }
            set {
                if ((object.ReferenceEquals(this.NameField, value) != true)) {
                    this.NameField = value;
                    this.RaisePropertyChanged("Name");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute(Order=2)]
        public System.DateTime EnrollmentDate {
            get {
                return this.EnrollmentDateField;
            }
            set {
                if ((this.EnrollmentDateField.Equals(value) != true)) {
                    this.EnrollmentDateField = value;
                    this.RaisePropertyChanged("EnrollmentDate");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute(Order=3)]
        public int Score {
            get {
                return this.ScoreField;
            }
            set {
                if ((this.ScoreField.Equals(value) != true)) {
                    this.ScoreField = value;
                    this.RaisePropertyChanged("Score");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute(Order=4)]
        public string Gender {
            get {
                return this.GenderField;
            }
            set {
                if ((object.ReferenceEquals(this.GenderField, value) != true)) {
                    this.GenderField = value;
                    this.RaisePropertyChanged("Gender");
                }
            }
        }
        
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="StudentService.StudentService")]
    public interface StudentService {
        
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/StudentService/GenerateStudents", ReplyAction="http://tempuri.org/StudentService/GenerateStudentsResponse")]
        System.IAsyncResult BeginGenerateStudents(int NoOfStudents, System.AsyncCallback callback, object asyncState);
        
        System.Collections.Generic.List<SilverlightApplication.StudentService.Student> EndGenerateStudents(System.IAsyncResult result);
        
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/StudentService/GenerateStudentsWithError", ReplyAction="http://tempuri.org/StudentService/GenerateStudentsWithErrorResponse")]
        System.IAsyncResult BeginGenerateStudentsWithError(int NoOfStudents, System.AsyncCallback callback, object asyncState);
        
        System.Collections.Generic.List<SilverlightApplication.StudentService.Student> EndGenerateStudentsWithError(System.IAsyncResult result);
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface StudentServiceChannel : SilverlightApplication.StudentService.StudentService, System.ServiceModel.IClientChannel {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class GenerateStudentsCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
        
        private object[] results;
        
        public GenerateStudentsCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
                base(exception, cancelled, userState) {
            this.results = results;
        }
        
        public System.Collections.Generic.List<SilverlightApplication.StudentService.Student> Result {
            get {
                base.RaiseExceptionIfNecessary();
                return ((System.Collections.Generic.List<SilverlightApplication.StudentService.Student>)(this.results[0]));
            }
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class GenerateStudentsWithErrorCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
        
        private object[] results;
        
        public GenerateStudentsWithErrorCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
                base(exception, cancelled, userState) {
            this.results = results;
        }
        
        public System.Collections.Generic.List<SilverlightApplication.StudentService.Student> Result {
            get {
                base.RaiseExceptionIfNecessary();
                return ((System.Collections.Generic.List<SilverlightApplication.StudentService.Student>)(this.results[0]));
            }
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class StudentServiceClient : System.ServiceModel.ClientBase<SilverlightApplication.StudentService.StudentService>, SilverlightApplication.StudentService.StudentService {
        
        private BeginOperationDelegate onBeginGenerateStudentsDelegate;
        
        private EndOperationDelegate onEndGenerateStudentsDelegate;
        
        private System.Threading.SendOrPostCallback onGenerateStudentsCompletedDelegate;
        
        private BeginOperationDelegate onBeginGenerateStudentsWithErrorDelegate;
        
        private EndOperationDelegate onEndGenerateStudentsWithErrorDelegate;
        
        private System.Threading.SendOrPostCallback onGenerateStudentsWithErrorCompletedDelegate;
        
        private BeginOperationDelegate onBeginOpenDelegate;
        
        private EndOperationDelegate onEndOpenDelegate;
        
        private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
        
        private BeginOperationDelegate onBeginCloseDelegate;
        
        private EndOperationDelegate onEndCloseDelegate;
        
        private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
        
        public StudentServiceClient() {
        }
        
        public StudentServiceClient(string endpointConfigurationName) : 
                base(endpointConfigurationName) {
        }
        
        public StudentServiceClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public StudentServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public StudentServiceClient(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<GenerateStudentsCompletedEventArgs> GenerateStudentsCompleted;
        
        public event System.EventHandler<GenerateStudentsWithErrorCompletedEventArgs> GenerateStudentsWithErrorCompleted;
        
        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 SilverlightApplication.StudentService.StudentService.BeginGenerateStudents(int NoOfStudents, System.AsyncCallback callback, object asyncState) {
            return base.Channel.BeginGenerateStudents(NoOfStudents, callback, asyncState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.Collections.Generic.List<SilverlightApplication.StudentService.Student> SilverlightApplication.StudentService.StudentService.EndGenerateStudents(System.IAsyncResult result) {
            return base.Channel.EndGenerateStudents(result);
        }
        
        private System.IAsyncResult OnBeginGenerateStudents(object[] inValues, System.AsyncCallback callback, object asyncState) {
            int NoOfStudents = ((int)(inValues[0]));
            return ((SilverlightApplication.StudentService.StudentService)(this)).BeginGenerateStudents(NoOfStudents, callback, asyncState);
        }
        
        private object[] OnEndGenerateStudents(System.IAsyncResult result) {
            System.Collections.Generic.List<SilverlightApplication.StudentService.Student> retVal = ((SilverlightApplication.StudentService.StudentService)(this)).EndGenerateStudents(result);
            return new object[] {
                    retVal};
        }
        
        private void OnGenerateStudentsCompleted(object state) {
            if ((this.GenerateStudentsCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.GenerateStudentsCompleted(this, new GenerateStudentsCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void GenerateStudentsAsync(int NoOfStudents) {
            this.GenerateStudentsAsync(NoOfStudents, null);
        }
        
        public void GenerateStudentsAsync(int NoOfStudents, object userState) {
            if ((this.onBeginGenerateStudentsDelegate == null)) {
                this.onBeginGenerateStudentsDelegate = new BeginOperationDelegate(this.OnBeginGenerateStudents);
            }
            if ((this.onEndGenerateStudentsDelegate == null)) {
                this.onEndGenerateStudentsDelegate = new EndOperationDelegate(this.OnEndGenerateStudents);
            }
            if ((this.onGenerateStudentsCompletedDelegate == null)) {
                this.onGenerateStudentsCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGenerateStudentsCompleted);
            }
            base.InvokeAsync(this.onBeginGenerateStudentsDelegate, new object[] {
                        NoOfStudents}, this.onEndGenerateStudentsDelegate, this.onGenerateStudentsCompletedDelegate, userState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.IAsyncResult SilverlightApplication.StudentService.StudentService.BeginGenerateStudentsWithError(int NoOfStudents, System.AsyncCallback callback, object asyncState) {
            return base.Channel.BeginGenerateStudentsWithError(NoOfStudents, callback, asyncState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.Collections.Generic.List<SilverlightApplication.StudentService.Student> SilverlightApplication.StudentService.StudentService.EndGenerateStudentsWithError(System.IAsyncResult result) {
            return base.Channel.EndGenerateStudentsWithError(result);
        }
        
        private System.IAsyncResult OnBeginGenerateStudentsWithError(object[] inValues, System.AsyncCallback callback, object asyncState) {
            int NoOfStudents = ((int)(inValues[0]));
            return ((SilverlightApplication.StudentService.StudentService)(this)).BeginGenerateStudentsWithError(NoOfStudents, callback, asyncState);
        }
        
        private object[] OnEndGenerateStudentsWithError(System.IAsyncResult result) {
            System.Collections.Generic.List<SilverlightApplication.StudentService.Student> retVal = ((SilverlightApplication.StudentService.StudentService)(this)).EndGenerateStudentsWithError(result);
            return new object[] {
                    retVal};
        }
        
        private void OnGenerateStudentsWithErrorCompleted(object state) {
            if ((this.GenerateStudentsWithErrorCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.GenerateStudentsWithErrorCompleted(this, new GenerateStudentsWithErrorCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void GenerateStudentsWithErrorAsync(int NoOfStudents) {
            this.GenerateStudentsWithErrorAsync(NoOfStudents, null);
        }
        
        public void GenerateStudentsWithErrorAsync(int NoOfStudents, object userState) {
            if ((this.onBeginGenerateStudentsWithErrorDelegate == null)) {
                this.onBeginGenerateStudentsWithErrorDelegate = new BeginOperationDelegate(this.OnBeginGenerateStudentsWithError);
            }
            if ((this.onEndGenerateStudentsWithErrorDelegate == null)) {
                this.onEndGenerateStudentsWithErrorDelegate = new EndOperationDelegate(this.OnEndGenerateStudentsWithError);
            }
            if ((this.onGenerateStudentsWithErrorCompletedDelegate == null)) {
                this.onGenerateStudentsWithErrorCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGenerateStudentsWithErrorCompleted);
            }
            base.InvokeAsync(this.onBeginGenerateStudentsWithErrorDelegate, new object[] {
                        NoOfStudents}, this.onEndGenerateStudentsWithErrorDelegate, this.onGenerateStudentsWithErrorCompletedDelegate, 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 SilverlightApplication.StudentService.StudentService CreateChannel() {
            return new StudentServiceClientChannel(this);
        }
        
        private class StudentServiceClientChannel : ChannelBase<SilverlightApplication.StudentService.StudentService>, SilverlightApplication.StudentService.StudentService {
            
            public StudentServiceClientChannel(System.ServiceModel.ClientBase<SilverlightApplication.StudentService.StudentService> client) : 
                    base(client) {
            }
            
            public System.IAsyncResult BeginGenerateStudents(int NoOfStudents, System.AsyncCallback callback, object asyncState) {
                object[] _args = new object[1];
                _args[0] = NoOfStudents;
                System.IAsyncResult _result = base.BeginInvoke("GenerateStudents", _args, callback, asyncState);
                return _result;
            }
            
            public System.Collections.Generic.List<SilverlightApplication.StudentService.Student> EndGenerateStudents(System.IAsyncResult result) {
                object[] _args = new object[0];
                System.Collections.Generic.List<SilverlightApplication.StudentService.Student> _result = ((System.Collections.Generic.List<SilverlightApplication.StudentService.Student>)(base.EndInvoke("GenerateStudents", _args, result)));
                return _result;
            }
            
            public System.IAsyncResult BeginGenerateStudentsWithError(int NoOfStudents, System.AsyncCallback callback, object asyncState) {
                object[] _args = new object[1];
                _args[0] = NoOfStudents;
                System.IAsyncResult _result = base.BeginInvoke("GenerateStudentsWithError", _args, callback, asyncState);
                return _result;
            }
            
            public System.Collections.Generic.List<SilverlightApplication.StudentService.Student> EndGenerateStudentsWithError(System.IAsyncResult result) {
                object[] _args = new object[0];
                System.Collections.Generic.List<SilverlightApplication.StudentService.Student> _result = ((System.Collections.Generic.List<SilverlightApplication.StudentService.Student>)(base.EndInvoke("GenerateStudentsWithError", _args, result)));
                return _result;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

Comments and Discussions