< Adding New VB .NET Module (DLL) | Main | Editing the Web.config File & Misc. >

 


 

 

Chapter 11 Part 14:

XML Web Services and the Network

 

 

What do we have in this chapter 11 Part 14?

  1. Resolving .NET System.Web Namespace Manually

  2. The BeginXXX and EndXXX Proxy Class Issue

  3. Adding Web Service Reference

 

Resolving .NET System.Web Namespace Manually

 

Well, there is unresolved namespace for the IHttpModule interface although we already imported the System.web namespace.

 

The VB .NET Asynchronous Web Service Program Example: the unresolved namespace

 

This issue can be verified through the References page of the project property.

 

The VB .NET Asynchronous Web Service Program Example: invoking the project property page

 

There is no System.Web namespace. So we need to add it manually.

 

The VB .NET Asynchronous Web Service Program Example: the vb .net project property page

 

Select the AsyncModuleVB project folder > Right-click mouse > Select Add Reference menu.

 

The VB .NET Asynchronous Web Service Program Example: adding a reference to the .net project to resolve the .NET reference

 

Find and select the System.Web component and click OK. The IHttpModule interface should be resolved.

 

The VB .NET Asynchronous Web Service Program Example: the .NET Add Reference page

 

The BeginXXX and EndXXX Proxy Class Issue

 

Next we need to add a reference to the web service created earlier or normally called a proxy class. However, the new implementation is using the event based reference which does not provide the BeginXXX and EndXXX methods anymore. In order to use the BeginXXX and EndXXX we need to modify the project property before adding the web service reference. This is based on this Microsoft fuckback.

Firstly we need to unload the project or close the solution. Then, open the project file, AsyncModuleVB.vbproj file in any unformatted text editor.

 

The VB .NET Asynchronous Web Service Program Example: opening the .nET project file

 

 

The VB .NET Asynchronous Web Service Program Example: opening the .NET project file in text file

 

Add the following property setting inside the <PropertyGroup> and save the project file.

<WebReference_EnableLegacyEventingModel>true</WebReference_EnableLegacyEventingModel>

 

The VB .NET Asynchronous Web Service Program Example: the .NET project file opened in text file

 

Adding Web Service Reference

 

Then, reopen the solution. Select the project folder > Right-click mouse > Select Add Service Reference menu.

 

The VB .NET Asynchronous Web Service Program Example: adding the web service reference to the .NET project

 

In the Add Service Reference, click the Advanced button.

 

The VB .NET Asynchronous Web Service Program Example: the Add Service Reference page

 

In the Service Reference Settings page, click the Add Web Reference button. We are going to add a web service in this case.

 

The VB .NET Asynchronous Web Service Program Example: the Service Reference Settings page

 

In the Add Web Reference page, click the first link, Web Services in this solution.

 

The VB .NET Asynchronous Web Service Program Example: the Add Reference page with the web service browsing page

 

Then, click the Service1 link.

 

The VB .NET Asynchronous Web Service Program Example: selecting the desired published web service

 

The exposed web service methods will be displayed. You can test the exposed methods as done previously.

 

The VB .NET Asynchronous Web Service Program Example: testing the web service exposed methods

 

Rename the web reference from localhost to AuthorizationService and click the Web Reference button.

 

The VB .NET Asynchronous Web Service Program Example: renaming the web service reference name and aading it to the existing .NET project

 

The web service reference or proxy class should be visible in the Solution Explorer.

 

The VB .NET Asynchronous Web Service Program Example: the added web service visible in the Visual Studio Solution Explorer

 

You can view the auto generated proxy class file under the Web References folder in the project folder.

 

The VB .NET Asynchronous Web Service Program Example: opening the web service file (proxy class)

 

The following is the proxy class file (Reference.vb) content. There are also WSDL file which provide the definition for the web service, the map and discovery files. The discovery file used for the web service 'advertisement'.

 

'------------------------------------------------------------------------------

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

'------------------------------------------------------------------------------

 

Option Strict Off

Option Explicit On

 

Imports System

Imports System.ComponentModel

Imports System.Diagnostics

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.Xml.Serialization

 

'

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

 

Namespace AuthorizationService

 

    '''<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://tempuri.org/")>  _

    Partial Public Class Service1

        Inherits System.Web.Services.Protocols.SoapHttpClientProtocol

 

        Private SumOperationCompleted As System.Threading.SendOrPostCallback       

        Private IsAuthorizedOperationCompleted As System.Threading.SendOrPostCallback       

        Private useDefaultCredentialsSetExplicitly As Boolean

 

        '''<remarks/>

        Public Sub New()

            MyBase.New

            Me.Url = Global.AsyncModuleVB.My.MySettings.Default.AsyncModuleVB_AuthorizationService_Service1

            If (Me.IsLocalFileSystemWebService(Me.Url) = true) Then

                Me.UseDefaultCredentials = true

                Me.useDefaultCredentialsSetExplicitly = false

            Else

                Me.useDefaultCredentialsSetExplicitly = true

            End If

        End Sub

 

        Public Shadows Property Url() As String

            Get

                Return MyBase.Url

            End Get

            Set

                If (((Me.IsLocalFileSystemWebService(MyBase.Url) = true)  _

                            AndAlso (Me.useDefaultCredentialsSetExplicitly = false))  _

                            AndAlso (Me.IsLocalFileSystemWebService(value) = false)) Then

                    MyBase.UseDefaultCredentials = false

                End If

                MyBase.Url = value

            End Set

        End Property

 

        Public Shadows Property UseDefaultCredentials() As Boolean

            Get

                Return MyBase.UseDefaultCredentials

            End Get

            Set

                MyBase.UseDefaultCredentials = value

                Me.useDefaultCredentialsSetExplicitly = true

            End Set

        End Property

 

        '''<remarks/>

        Public Event SumCompleted As SumCompletedEventHandler

 

        '''<remarks/>

        Public Event IsAuthorizedCompleted As IsAuthorizedCompletedEventHandler

 

        '''<remarks/>

        <System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Sum", RequestNamespace:="http://tempuri.org/", ResponseNamespace:="http://tempuri.org/", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _

        Public Function Sum(ByVal lhs As Integer, ByVal rhs As Integer) As Integer

            Dim results() As Object = Me.Invoke("Sum", New Object() {lhs, rhs})

            Return CType(results(0),Integer)

        End Function

 

        '''<remarks/>

        Public Function BeginSum(ByVal lhs As Integer, ByVal rhs As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult

            Return Me.BeginInvoke("Sum", New Object() {lhs, rhs}, callback, asyncState)

        End Function

 

        '''<remarks/>

        Public Function EndSum(ByVal asyncResult As System.IAsyncResult) As Integer

            Dim results() As Object = Me.EndInvoke(asyncResult)

            Return CType(results(0),Integer)

        End Function

 

        '''<remarks/>

        Public Overloads Sub SumAsync(ByVal lhs As Integer, ByVal rhs As Integer)

            Me.SumAsync(lhs, rhs, Nothing)

        End Sub

 

        '''<remarks/>

        Public Overloads Sub SumAsync(ByVal lhs As Integer, ByVal rhs As Integer, ByVal userState As Object)

            If (Me.SumOperationCompleted Is Nothing) Then

                Me.SumOperationCompleted = AddressOf Me.OnSumOperationCompleted

            End If

            Me.InvokeAsync("Sum", New Object() {lhs, rhs}, Me.SumOperationCompleted, userState)

        End Sub

 

        Private Sub OnSumOperationCompleted(ByVal arg As Object)

            If (Not (Me.SumCompletedEvent) Is Nothing) Then

                Dim invokeArgs As System.Web.Services.Protocols.InvokeCompletedEventArgs = CType(arg,System.Web.Services.Protocols.InvokeCompletedEventArgs)

                RaiseEvent SumCompleted(Me, New SumCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState))

            End If

        End Sub

 

        '''<remarks/>

        <System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/IsAuthorized", RequestNamespace:="http://tempuri.org/", ResponseNamespace:="http://tempuri.org/", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _

        Public Function IsAuthorized(ByVal userName As String) As Boolean

            Dim results() As Object = Me.Invoke("IsAuthorized", New Object() {userName})

            Return CType(results(0),Boolean)

        End Function

 

        '''<remarks/>

        Public Function BeginIsAuthorized(ByVal userName As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult

            Return Me.BeginInvoke("IsAuthorized", New Object() {userName}, callback, asyncState)

        End Function

 

        '''<remarks/>

        Public Function EndIsAuthorized(ByVal asyncResult As System.IAsyncResult) As Boolean

            Dim results() As Object = Me.EndInvoke(asyncResult)

            Return CType(results(0),Boolean)

        End Function

 

        '''<remarks/>

        Public Overloads Sub IsAuthorizedAsync(ByVal userName As String)

            Me.IsAuthorizedAsync(userName, Nothing)

        End Sub

 

        '''<remarks/>

        Public Overloads Sub IsAuthorizedAsync(ByVal userName As String, ByVal userState As Object)

            If (Me.IsAuthorizedOperationCompleted Is Nothing) Then

                Me.IsAuthorizedOperationCompleted = AddressOf Me.OnIsAuthorizedOperationCompleted

            End If

            Me.InvokeAsync("IsAuthorized", New Object() {userName}, Me.IsAuthorizedOperationCompleted, userState)

        End Sub

 

        Private Sub OnIsAuthorizedOperationCompleted(ByVal arg As Object)

            If (Not (Me.IsAuthorizedCompletedEvent) Is Nothing) Then

                Dim invokeArgs As System.Web.Services.Protocols.InvokeCompletedEventArgs = CType(arg,System.Web.Services.Protocols.InvokeCompletedEventArgs)

                RaiseEvent IsAuthorizedCompleted(Me, New IsAuthorizedCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState))

            End If

        End Sub

 

        '''<remarks/>

        Public Shadows Sub CancelAsync(ByVal userState As Object)

            MyBase.CancelAsync(userState)

        End Sub

 

        Private Function IsLocalFileSystemWebService(ByVal url As String) As Boolean

            If ((url Is Nothing)  _

                        OrElse (url Is String.Empty)) Then

                Return false

            End If

            Dim wsUri As System.Uri = New System.Uri(url)

            If ((wsUri.Port >= 1024)  _

                        AndAlso (String.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) = 0)) Then

                Return true

            End If

            Return false

        End Function

    End Class

 

    '''<remarks/>

    <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")>  _

    Public Delegate Sub SumCompletedEventHandler(ByVal sender As Object, ByVal e As SumCompletedEventArgs)

 

    '''<remarks/>

    <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053"),  _

     System.Diagnostics.DebuggerStepThroughAttribute(),  _

     System.ComponentModel.DesignerCategoryAttribute("code")>  _

    Partial Public Class SumCompletedEventArgs

        Inherits System.ComponentModel.AsyncCompletedEventArgs

 

        Private results() As Object

 

        Friend 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

 

        '''<remarks/>

        Public ReadOnly Property Result() As Integer

            Get

                Me.RaiseExceptionIfNecessary

                Return CType(Me.results(0),Integer)

            End Get

        End Property

    End Class

 

    '''<remarks/>

    <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")>  _

    Public Delegate Sub IsAuthorizedCompletedEventHandler(ByVal sender As Object, ByVal e As IsAuthorizedCompletedEventArgs)

 

    '''<remarks/>

    <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053"),  _

     System.Diagnostics.DebuggerStepThroughAttribute(),  _

     System.ComponentModel.DesignerCategoryAttribute("code")>  _

    Partial Public Class IsAuthorizedCompletedEventArgs

        Inherits System.ComponentModel.AsyncCompletedEventArgs

 

        Private results() As Object

 

        Friend 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

 

        '''<remarks/>

        Public ReadOnly Property Result() As Boolean

            Get

                Me.RaiseExceptionIfNecessary

                Return CType(Me.results(0),Boolean)

            End Get

        End Property

    End Class

End Namespace

 

The following Figure is the screenshot for the auto generated proxy class. You can edit the content however, when the reference is re-generated, the modification will be lost.

 

The VB .NET Asynchronous Web Service Program Example: the proxy class file content

 

 

 


< Adding New VB .NET Module (DLL) | Main | Editing the Web.config File & Misc. >