< ASP Debug & C# Asynchronous Class Library Project | Main | DLL & C# Asynchronous Class Library Project >

 


 

 

Chapter 11 Part 9:

XML Web Services and the Network

 

 

What do we have in this chapter 11 Part 9?

  1. The Proxy Class BeginXXX and EndXXX Issue

  2. Adding the Web Service Reference to the Existing C# Project

 

The Proxy Class BeginXXX and EndXXX Issue

 

Before adding the web service reference, based on the feedback to Microsoft, there no more BeginXXX and EndXXX methods in the auto generated web service reference file. The solution is to add the following property to the project file manually.

<WebReference_EnableLegacyEventingModel>true</WebReference_EnableLegacyEventingModel>

Firstly, close the solution or unload the project. Find the AsyncModuleCS project file. Open it in any text editor.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: opening the C# project file in unformatted  text editor

 

Add the previously mentioned property inside the <PropertyGroup> tag. Don’t forget to save the file.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the C# project file content

 

Adding the Web Service Reference to the Existing C# Project

 

Reload or re-open the project solution again. Select the project folder > Right-click mouse > Select Add Service Reference.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding a web service reference or a proxy class to the existing C# project

 

In the Add Service Reference page, click the Advanced button. We are going to add the web service reference.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the Add Service Reference page

 

In the Service Reference Settings page, click Add Web Reference button.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the Service Reference Settings page

 

We are given three options here. In our case, both the first and second links are valid. Select the first link.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the web service options for .NET project: in the current solution, local web server or local network/Internet domain

 

Any available web service will be visible now. Click the Service1 link.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: selecting the web service, Service1

 

The methods available in the web service Service1 will be exposed. At this stage you can also text the exposed methods directly as previously done through browser.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the web service exposed methods

 

Next, leave the Web reference name to the default given: localhost and click the Add Reference button.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: testing the web service exposed methods and then adding the web service reference to the existing C# project

 

The web service reference will be visible in the AsyncModuleCS project under the Web References folder.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the web service reference seen in the Solution Explorer of the Visual Studio IDE

 

Let see, what have been generated. We have a source file (Reference.cs) also called the class proxy code, the Web Services Description Language (Service1.wsdl) file used to describe the web service (network service in general), Service1, the map file (Reference.map) and the discovery file (Service1.disco) used for the web service discovery.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: opening the proxy class file, Reference.cs in Visual Studio IDE

 

The following code is the content of the Reference.cs. Well, now we are ready to use those methods available in the web service in our program 'as if' they are normal methods.

 

 

 

 

 

//------------------------------------------------------------------------------

// <auto-generated>

//     This code was generated by a tool.

//     Runtime Version:2.0.50727.3053

//

//     Changes to this file may cause incorrect behavior and will be lost if

//     the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------

 

//

// This source code was auto-generated by Microsoft.VSDesigner, Version 2.0.50727.3053.

//

#pragma warning disable 1591

 

namespace AsyncModuleCS.localhost {

    using System.Diagnostics;

    using System.Web.Services;

    using System.ComponentModel;

    using System.Web.Services.Protocols;

    using System;

    using System.Xml.Serialization;

 

    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    [System.Web.Services.WebServiceBindingAttribute(Name="Service1Soap", Namespace="http://localhost/")]

    public partial class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol {

        private System.Threading.SendOrPostCallback SumOperationCompleted;

        private System.Threading.SendOrPostCallback IsAuthorizedOperationCompleted;

        private bool useDefaultCredentialsSetExplicitly;

 

        /// <remarks/>

        public Service1() {

            this.Url = global::AsyncModuleCS.Properties.Settings.Default.AsyncModuleCS_localhost_Service1;

            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {

                this.UseDefaultCredentials = true;

                this.useDefaultCredentialsSetExplicitly = false;

            }

            else {

                this.useDefaultCredentialsSetExplicitly = true;

            }

        }

       

        public new string Url {

            get {

                return base.Url;

            }

            set {

                if ((((this.IsLocalFileSystemWebService(base.Url) == true)

                            && (this.useDefaultCredentialsSetExplicitly == false))

                            && (this.IsLocalFileSystemWebService(value) == false))) {

                    base.UseDefaultCredentials = false;

                }

                base.Url = value;

            }

        }

       

        public new bool UseDefaultCredentials {

            get {

                return base.UseDefaultCredentials;

            }

            set {

                base.UseDefaultCredentials = value;

                this.useDefaultCredentialsSetExplicitly = true;

            }

        }

 

        /// <remarks/>

        public event SumCompletedEventHandler SumCompleted;

        /// <remarks/>

        public event IsAuthorizedCompletedEventHandler IsAuthorizedCompleted;

        /// <remarks/>

        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost/Sum", RequestNamespace="http://localhost/", ResponseNamespace="http://localhost/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

        public int Sum(int lhs, int rhs) {

            object[ ] results = this.Invoke("Sum", new object[ ] {

                        lhs,

                        rhs});

            return ((int)(results[0]));

        }

 

        /// <remarks/>

        public System.IAsyncResult BeginSum(int lhs, int rhs, System.AsyncCallback callback, object asyncState) {

            return this.BeginInvoke("Sum", new object[ ] {

                        lhs,

                        rhs}, callback, asyncState);

        }

 

        /// <remarks/>

        public int EndSum(System.IAsyncResult asyncResult) {

            object[ ] results = this.EndInvoke(asyncResult);

            return ((int)(results[0]));

        }

 

        /// <remarks/>

        public void SumAsync(int lhs, int rhs) {

            this.SumAsync(lhs, rhs, null);

        }

 

        /// <remarks/>

        public void SumAsync(int lhs, int rhs, object userState) {

            if ((this.SumOperationCompleted == null)) {

                this.SumOperationCompleted = new System.Threading.SendOrPostCallback(this.OnSumOperationCompleted);

            }

            this.InvokeAsync("Sum", new object[ ] {

                        lhs,

                        rhs}, this.SumOperationCompleted, userState);

        }

 

        private void OnSumOperationCompleted(object arg) {

            if ((this.SumCompleted != null)) {

                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));

                this.SumCompleted(this, new SumCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));

            }

        }

 

        /// <remarks/>

        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost/IsAuthorized", RequestNamespace="http://localhost/", ResponseNamespace="http://localhost/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

        public bool IsAuthorized(string userName) {

            object[ ] results = this.Invoke("IsAuthorized", new object[ ] {

                        userName});

            return ((bool)(results[0]));

        }

 

        /// <remarks/>

        public System.IAsyncResult BeginIsAuthorized(string userName, System.AsyncCallback callback, object asyncState) {

            return this.BeginInvoke("IsAuthorized", new object[ ] {

                        userName}, callback, asyncState);

        }

 

        /// <remarks/>

        public bool EndIsAuthorized(System.IAsyncResult asyncResult) {

            object[ ] results = this.EndInvoke(asyncResult);

            return ((bool)(results[0]));

        }

 

        /// <remarks/>

        public void IsAuthorizedAsync(string userName) {

            this.IsAuthorizedAsync(userName, null);

        }

 

        /// <remarks/>

        public void IsAuthorizedAsync(string userName, object userState) {

            if ((this.IsAuthorizedOperationCompleted == null)) {

                this.IsAuthorizedOperationCompleted = new System.Threading.SendOrPostCallback(this.OnIsAuthorizedOperationCompleted);

            }

            this.InvokeAsync("IsAuthorized", new object[ ] {

                        userName}, this.IsAuthorizedOperationCompleted, userState);

        }

 

        private void OnIsAuthorizedOperationCompleted(object arg) {

            if ((this.IsAuthorizedCompleted != null)) {

                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));

                this.IsAuthorizedCompleted(this, new IsAuthorizedCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));

            }

        }

 

        /// <remarks/>

        public new void CancelAsync(object userState) {

            base.CancelAsync(userState);

        }

 

        private bool IsLocalFileSystemWebService(string url) {

            if (((url == null)

                        || (url == string.Empty))) {

                return false;

            }

            System.Uri wsUri = new System.Uri(url);

            if (((wsUri.Port >= 1024)

                        && (string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0))) {

                return true;

            }

            return false;

        }

    }

 

    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")]

    public delegate void SumCompletedEventHandler(object sender, SumCompletedEventArgs e);

 

    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    public partial class SumCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

 

        private object[ ] results;

 

        internal SumCompletedEventArgs(object[ ] results, System.Exception exception, bool cancelled, object userState) :

                base(exception, cancelled, userState) {

            this.results = results;

        }

 

        /// <remarks/>

        public int Result {

            get {

                this.RaiseExceptionIfNecessary();

                return ((int)(this.results[0]));

            }

        }

    }

 

    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")]

    public delegate void IsAuthorizedCompletedEventHandler(object sender, IsAuthorizedCompletedEventArgs e);

 

    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    public partial class IsAuthorizedCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

 

        private object[ ] results;

 

        internal IsAuthorizedCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :

                base(exception, cancelled, userState) {

            this.results = results;

        }

 

        /// <remarks/>

        public bool Result {

            get {

                this.RaiseExceptionIfNecessary();

                return ((bool)(this.results[0]));

            }

        }

    }

}

 

#pragma warning restore 1591

 

 

 


< ASP Debug & C# Asynchronous Class Library Project | Main | DLL & C# Asynchronous Class Library Project >