< Proxy Class BeginXXX and EndXXX Issue | Main | IIS Web Log, ASP .NET Config & Temp Files >

 


 

 

Chapter 11 Part 10:

XML Web Services and the Network

 

 

What do we have in this chapter 11 Part 10?

  1. Completing the C# Asynchronous Class Library Project

  2. Build and Run the Whole Project

  3. Loading the DLL for the Web Service Application

  4. The DLL and the bin Folder

 

Completing the C# Asynchronous Class Library Project

 

Next, we are going to add two more C# source files which contain classes to process our own asynchronous implementation. Add MyAsyncResult C# class which will be used as the IAsyncResult interface implementation.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding new item into current C# project

 

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding new class into the current C# project

 

Add/edit the code as shown below.

 

using System;

using System.Threading;

using System.Diagnostics;

 

namespace AsyncModuleCS

{

    /// <summary>

    /// Implementation of IAsyncResult

    /// </summary>

    public class MyAsyncResult : IAsyncResult

    {

        // data members

        AsyncCallback cb;

        object state;

        bool completed;

        bool completedSync;

        object result;

        Exception err;

        int counter;

 

        public MyAsyncResult(AsyncCallback callb, object st, int cnt)

        {

            this.cb = callb;

            this.state = st;

            this.counter = cnt;

        }

 

        // constructor with predefined results,

        // useful for errors that occur before async work is done                 

        public MyAsyncResult(AsyncCallback callb, object st, bool complete, object res, Exception e, int cnt)

        {

            this.cb = callb;

            this.state = st;

            this.completed = complete;

            this.counter = cnt;

            this.completedSync = this.completed;

            this.result = res;

            this.err = e;

 

            if (null != cb)

                cb(this);

 

            if (this.completed)

                TraceHelper.WriteLine("MyAsyncResult completed sync...", counter);

        }

         // http://msdn.microsoft.com/en-us/library/system.iasyncresult_members.aspx

        public object AsyncState

        {

            get

            {

                return state;

            }

        }

 

        public bool CompletedSynchronously

        {

            get

            {

                return completedSync;

            }

        }

 

        public WaitHandle AsyncWaitHandle

        {

            get

            {

                return null;

            }

        }

 

        public bool IsCompleted

        {

            get

            {

                return completed;

            }

        }

 

        // non IAsyncResult Stuff

        public Exception AsyncError

        {

            get

            {

                return err;

            }

        }

 

        public void Complete(bool sync, object res, Exception e)

        {

            TraceHelper.WriteLine("MyAsyncResult.Complete was called...", counter);

            completed = true;

            completedSync = sync;

            result = res;

            err = e;

            if (null != cb)

                cb(this);

        }

 

        public object EndOp()

        {

            if (err != null)

                throw new Exception("AsyncOperation failed lor!", err);

 

            return result;

        }

    }

}

 

Then, add another class, TraceHelper which just to dump messages in the Output window during the application debug process.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: invoking the adding new item menu in the Visual Studio IDE

 

 

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

 

Add/edit the code as shown below.

 

using System;

using System.Threading;

using System.Diagnostics;

 

namespace AsyncModuleCS

{

    /// <summary>

    /// Helper class for displaying trace information

    /// </summary>

    public class TraceHelper

    {

        public static void WriteLine(string msg, int cnt)

        {

            int tid = Thread.CurrentThread.ManagedThreadId;

            Trace.WriteLine(string.Format("ThreadId: {0} Counter: {1} Msg: {2}", tid, cnt, msg));

        }

    }

}

 

Then, you can try building the project and make sure there is no error.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: building C# project individually

 

The next step is to create two asp pages (or HTML pages) for the access denied and default web page. These pages are created for the AuthServiceCS project. The access denied page will be fictitiously used for the access denied user and the default page is a standard default web page. Firstly, add the accessdevied.aspx page.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: invoking the adding new item menu for C# project in Visual Studio IDE

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding an aspx web form into existing C# project

 

Add the following simple code.

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>The access denied page</title>

</head>

<body>

<center><h1>Access Denied from AsyncModule</h1></center>

</body>

</html>

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the aspx source code

 

Next, add the default.aspx page.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding another aspx page into the existing C# project

 

Add/edit the following code.

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title></title>

</head>

<body>

<%

    Response.Write("Hello, " + Server.HtmlEncode(User.Identity.Name));

%>

</body>

</html>

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the aspx source code

 

You can test the page directly from the VStudio editor. While the ASP page is opened, right click mouse > Select View in Browser context menu.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: testing the asp page directly in the Visual Studio IDE

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the asp page opened in browser

 

Build and Run the Whole Project

 

Well, now we are ready to build ad run the whole project. Firstly, build the solution which includes both projects. Make sure there is no error.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: building C# project solution

 

Then, run the project without debugging.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: running the C# project solution without debugging

 

The following page should be expected. However in this project we are not concern about the web pages but we are going to see the thread used for the asynchronous operation when there are many accesses to the web site.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the asp page opened in Internet browser

 

Next, let set the default.aspx page as the first web page to be served when user accesses the site. Use the IIS property page as done previously. In the Documents page, add default.aspx page and optionally you can move it to the first position.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding the default first page document of the IIS web site

 

Next, test the web access using browser. It looks fine.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: testing the web access and web page directly from the browser of the localhost

 

Loading the DLL for the Web Service Application

 

Well, now we are facing the 'problem' on how to load or register the asynchronous module (DLL) during the web page/web site access for the IIS web server. In this case we can use the Web.config which is automatically generated for the web related project. Open the Web.config and add the module inside the <httpModules> tag (the add httpModules element reference from MSDN).

<add name="AsyncAuthorizationCs" type="AsyncModuleCS.AsyncModule,AsyncModuleCS"/>

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding the httpModules in the web.config file

 

Next, add the authorization element to deny certain user.

 

    <authorization>

      <deny users="?" />

    </authorization>

 

And the path for the accessdenied page redirection for all user.

 

<!-- let everyone get to access denied -->

  <location path="accessdenied.aspx">

    <system.web>

      <authorization>

        <allow users="*" />

      </authorization>

    </system.web>

  </location>

 

The following is the screenshot for the previous ASP .NET schema settings.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the edited web.config file

 

Then, run the solution in debug mode.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: running the C# project in debug mode

 

The DLL and the bin Folder

 

Huh! Another error lor. The DLL assembly for the asynchronous module cannot be found.

 

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

 

So we need to copy those module assemblies into the \bin folder of the AuthServiceCS. We will do this manually. Copy and paste the .DLL and .PDB files from the \bin\Debug\ folder of the AsyncModuleCS into the \bin folder of the AuthServiceCS. Normally only the .DLL file needs to be copied, however in our case, the DLL is the debug version instead of release version, so it is better to copy all the files under the bin folder.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: copying the DLL and related files under the /bin folder of the C# project

 

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: pasting the DLL module or class into the web service /bin folder of the web service application

 

Then, re-run the solution in the Debug mode.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: running the C# project in debug mode

 

The ASP .NET development server will be launched together with the default page of the web site. We are not concern about the page but the messages generated in the Output window.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the asp web site default page

 

Launch another several browsers and use the similar URL shown below. Notice the VStudio Output window at the bottom. Take note that your default ASP Development Server port number may be different so change accordingly.

http://localhost:1045/

However, the default port number used by ASP .NET development server can be changed through the project property page as shown below.

 

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

 

A sample from the Output window messages are reproduced as follows for three web access. Notice the same thread id that reflects the asynchronous accesses.

...

'WebDev.WebServer.EXE' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll'

'WebDev.WebServer.EXE' (Managed): Loaded 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\7a70190c\43af316\assembly\dl3\df0c2c42\b27c4274_897fc901\AsyncModuleCS.DLL', Symbols loaded.

ThreadId: 4 Counter: 1 Msg: Initializing the module instance

'WebDev.WebServer.EXE' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\SMDiagnostics\3.0.0.0__b77a5c561934e089\SMDiagnostics.dll'

'WebDev.WebServer.EXE' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll'

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

'WebDev.WebServer.EXE' (Managed): Loaded 'dq0jaolv'

ThreadId: 4 Counter: 1 Msg: Begin the authorization...

ThreadId: 4 Counter: 2 Msg: Initializing the module instance

ThreadId: 4 Counter: 2 Msg: Begin the authorization...

ThreadId: 4 Counter: 3 Msg: Initializing the module instance

ThreadId: 4 Counter: 3 Msg: Begin the authorization...

ThreadId: 4 Counter: 4 Msg: Initializing the module instance

ThreadId: 4 Counter: 4 Msg: Begin the authorization...

ThreadId: 4 Counter: 5 Msg: Initializing the module instance

ThreadId: 4 Counter: 5 Msg: Begin the authorization...

ThreadId: 4 Counter: 6 Msg: Initializing the module instance

ThreadId: 4 Counter: 6 Msg: Begin the authorization...

ThreadId: 4 Counter: 7 Msg: Initializing the module instance

ThreadId: 4 Counter: 7 Msg: Begin the authorization...

ThreadId: 4 Counter: 8 Msg: Initializing the module instance

ThreadId: 4 Counter: 8 Msg: Begin the authorization...

ThreadId: 4 Counter: 9 Msg: Initializing the module instance

ThreadId: 4 Counter: 9 Msg: Begin the authorization...

ThreadId: 4 Counter: 10 Msg: Initializing the module instance

ThreadId: 4 Counter: 10 Msg: Begin the authorization...

ThreadId: 4 Counter: 11 Msg: Initializing the module instance

ThreadId: 4 Counter: 11 Msg: Begin the authorization...

ThreadId: 4 Counter: 12 Msg: Initializing the module instance

ThreadId: 4 Counter: 12 Msg: Begin the authorization...

ThreadId: 4 Counter: 13 Msg: Initializing the module instance

ThreadId: 4 Counter: 13 Msg: Begin the authorization...

ThreadId: 4 Counter: 14 Msg: Initializing the module instance

ThreadId: 4 Counter: 14 Msg: Begin the authorization...

ThreadId: 4 Counter: 15 Msg: Initializing the module instance

ThreadId: 4 Counter: 15 Msg: Begin the authorization...

ThreadId: 4 Counter: 16 Msg: Initializing the module instance

ThreadId: 4 Counter: 16 Msg: Begin the authorization...

ThreadId: 4 Counter: 17 Msg: Initializing the module instance

ThreadId: 4 Counter: 17 Msg: Begin the authorization...

ThreadId: 4 Counter: 18 Msg: Initializing the module instance

ThreadId: 4 Counter: 18 Msg: Begin the authorization...

ThreadId: 4 Counter: 19 Msg: Initializing the module instance

ThreadId: 4 Counter: 19 Msg: Begin the authorization...

ThreadId: 4 Counter: 20 Msg: Initializing the module instance

ThreadId: 4 Counter: 20 Msg: Begin the authorization...

ThreadId: 4 Counter: 21 Msg: Initializing the module instance

ThreadId: 4 Counter: 21 Msg: Begin the authorization...

ThreadId: 4 Counter: 22 Msg: Initializing the module instance

ThreadId: 4 Counter: 22 Msg: Begin the authorization...

ThreadId: 4 Counter: 23 Msg: Initializing the module instance

ThreadId: 4 Counter: 23 Msg: Begin the authorization...

ThreadId: 4 Counter: 24 Msg: Initializing the module instance

ThreadId: 4 Counter: 24 Msg: Begin the authorization...

ThreadId: 4 Counter: 25 Msg: Initializing the module instance

ThreadId: 4 Counter: 25 Msg: Begin the authorization...

ThreadId: 4 Counter: 26 Msg: Initializing the module instance

ThreadId: 4 Counter: 26 Msg: Begin the authorization...

The thread 0x9cc has exited with code 0 (0x0).

ThreadId: 4 Counter: 27 Msg: Initializing the module instance

ThreadId: 4 Counter: 27 Msg: Begin the authorization...

ThreadId: 4 Counter: 28 Msg: Initializing the module instance

ThreadId: 4 Counter: 28 Msg: Begin the authorization...

When you close the project and then access the web service through browser, the following page will be displayed after time-out.

 

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

 

You may want to review the Article ID: 821268 regarding the Contention, poor performance, and deadlocks when you make Web service requests from ASP.NET applications because in this exercise we are using IIS on Windows XP Pro which known to have many limitations.

 

 

 


< Proxy Class BeginXXX and EndXXX Issue | Main | IIS Web Log, ASP .NET Config & Temp Files >