< Configuring IIS for Web Service Hosting | Main | Proxy Class BeginXXX and EndXXX Issue >

 


 

 

Chapter 11 Part 8:

XML Web Services and the Network

 

 

What do we have in this chapter 11 Part 8?

  1. The ASP .NET Debugging Options in IIS

  2. Adding New C# Asynchronous Class Library Project

  3. Adding the System.Web Reference Manually

 

The ASP .NET Debugging Options in IIS

 

There are many occasions we need to troubleshoot the web applications which have several technologies tied together. For the ASP .NET hosting we can enable the server-side and client side debugging as shown in the following screenshots. It is accessed from the Your_Web_Site Properties page provided the ASP was installed and running properly. Make sure you disable these settings for the production or real deployment.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the Default Web Site IIS Home DIrectory Page - setting the debugging options

 

The following screenshot shows the debugging options for ASP .NET which are very useful in assisting the troubleshooting tasks during the development stage.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: setting the ASP .NET debugging and error messages through the IIS Default Web Site property page

 

Other info for debugging the web applications is through the IIS log file. Click the Web Site property page and there are several standard logging formats that you can choose from. Then click the Properties button.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: invoking the logging options for the Default Web Site IIS web server through the property page

 

We can customize the details of the logging properties using the General Properties and Extended Properties pages.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the IIS extended logging property page settings

 

The following screenshots shows the detail of the IIS web extended logging. Make sure you just select the desired options only because for high volume of traffic it will consume a lot of storage.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the IIS logging options with extended properties

 

The following screenshot shows the IIS log file physical path which can be opened by any text editor or exported to other format such as spreadsheet.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the physical IIS web server log files

 

The following screenshot shows a sample of the IIS log file content opened in the text editor.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: a sample of the IIS web server log file

 

Well, let get back to our project. We can actually publish the web service or application directly from the VS IDE. Select the web service project folder > Right-click mouse > Select the Publish menu. We are not completing this task in this exercise because we already publish the web service 'manually' (just click the Cancel button in the Publish Web page).

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: publishing the web service application from the Visual Studio IDE

 

Set the target location for the web service or application and then click the Publish button.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the Publish Web options for web service application accessed from Visual Studio IDE

 

Next, let continue developing our asynchronous web service project. We will add a new project that will demonstrate the asynchronous access to the web service. In the new implementation of the asynchronous web access, most of the tasks already have been automated using the event based asynchronous web access techniques however in this exercise we are using the old techniques which based on the early .NET class library. Create a new project in the current solution.

 

Adding New C# Asynchronous Class Library Project

 

Select the solution folder > Right-click mouse > Click Add menu > Click the New Project sub menu.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding a new C# project into the existing web service project solution

 

Select Class Library template and you can use the AsyncModuleCS as the project name if you want. Click the OK button.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: selecting the C# class library project template

 

Rename the file to AsyncModule to reflect the application to be developed. The class will be renamed automatically. This class will define the IHttpModule interface which includes the event handler and asynchronous callback.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: renaming the default given C# source file

 

Add the following using directives and delete the unused directives.

 

using System.Web;

using System.Threading;

using System.Text.RegularExpressions;

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding the using directives to the C# source file

 

Add/edit the following given code.

 

using System;

using System.Web;

using System.Threading;

using System.Text.RegularExpressions;

 

namespace AsyncModuleCS

{

    /// <summary>

    /// Demonstrates an async module in ASP.NET

    /// </summary>

    public class AsyncModule : IHttpModule

    {

        // static data members

        static int counter = 0;

        static int modCounter = 0;

        const string RedirectLocation = "accessdenied.aspx";

        // instance data members

        int instanceCounter;

        bool isAuthorized;

        HttpContext ctx;

        Regex locationRegex = new Regex(RedirectLocation, RegexOptions.IgnoreCase);

 

        public void Init(HttpApplication app)

        {

            // register for our async event

            app.AddOnAuthorizeRequestAsync(new BeginEventHandler(ref AsyncBeginHandler),

                new EndEventHandler(ref AsyncEndHandler));

            // add a sync EndRequest handler to explain what happened if access is denied       

            app.EndRequest += new EventHandler(ref OnEndRequest);

 

            int modCount = Interlocked.Increment(ref modCounter);

            TraceHelper.WriteLine("Initializing the module instance", modCount);

        }

 

        // Required on this interface

        public void Dispose()

        { }

 

        public IAsyncResult AsyncBeginHandler(object src, EventArgs e, AsyncCallback cb, object extraData)

        {

            HttpApplication app = (HttpApplication)src;

 

            // per-request init

            instanceCounter = Interlocked.Increment(ref counter);

            isAuthorized = false;

            ctx = app.Context;

            // let anyone into the access denied page

            Match m = locationRegex.Match(ctx.Request.FilePath);

            // is it something other than accessdenied.aspx? Check authorization...                                  

            if (!m.Success)

            {

                // create service proxy, async result, and callback

                AsyncModuleCS.localhost.Service1 authServ = new AsyncModuleCS.localhost.Service1();

 

                MyAsyncResult myAr = new MyAsyncResult(cb, authServ, instanceCounter);

                AsyncCallback myCb = new AsyncCallback(ref WebServiceAsyncCallback);

                // start async call

                TraceHelper.WriteLine("Begin the authorization...", instanceCounter);

                authServ.BeginIsAuthorized(ctx.User.Identity.Name, myCb, myAr);

                return myAr;

            }

            else // otherwise, complete synchronously

            {

                isAuthorized = true;

                TraceHelper.WriteLine("Request for accessdenied asp page, skipping...", instanceCounter);

                return new MyAsyncResult(null, null, true, true, null, instanceCounter);

            }

        }

 

        public void AsyncEndHandler(IAsyncResult ar)

        {

            MyAsyncResult myAr = (MyAsyncResult)ar;

            TraceHelper.WriteLine("End the authorization...", instanceCounter);

            // if this user isn't authorized, then bail out

            if (!isAuthorized)

            {

                ctx.Response.StatusCode = 403;

                ctx.ApplicationInstance.CompleteRequest();

                TraceHelper.WriteLine("Request Rejected!", instanceCounter);

            }

 

            // clear variables

            ctx = null;

            instanceCounter = -1;

            isAuthorized = false;

            myAr.EndOp(); // Throw if necessary

        }

 

        public void WebServiceAsyncCallback(IAsyncResult ar)

        {

            MyAsyncResult myAr = (MyAsyncResult)ar.AsyncState;

            AsyncModuleCS.localhost.Service1 authServ = (AsyncModuleCS.localhost.Service1)myAr.AsyncState;

            // get back result

            isAuthorized = authServ.EndIsAuthorized(ar);

            TraceHelper.WriteLine("WebService call completed...", instanceCounter);

            // tell ASP.NET we're done

            int cnt = instanceCounter;

            TraceHelper.WriteLine("Begin notifing ASP.NET...", cnt);

            myAr.Complete(false, isAuthorized, null);

            TraceHelper.WriteLine("End notifing ASP.NET...", cnt);

        }

 

        public void OnEndRequest(object src, EventArgs E)

        {

            HttpApplication app = (HttpApplication)src;

            // did we block someone?

            if (app.Response.StatusCode == 403)

                app.Response.Redirect(RedirectLocation, false);

        }

    }

}

 

Adding the System.Web Reference Manually

 

Many times we have encountered the unresolved System.Net namespace when using VStudio 2008 which also happens in VStudio 2005. So, you can see the IHttpModule interface in the code cannot be resolved and under the References folder, there is no System.Web namespace although we already included the using System.web namespace using directive. So we need to include it manually.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the un-resolved IHttpModule interface

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the un-resolved System.Web namespace

 

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

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: adding the .NET reference

 

In the .NET page, select the System.Web component and click OK button.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: selecting the System.NET reference

 

Now, you can see the System.Web namespace in the References folder and any reference to the namespace should be resolved.

 

The C# Asynchronous Web Service Access with ASP .NET WEB Service application development Program Example: the System.NEt reference seen in Solution Explorer

 

Next we will resolve the reference to the web service created in the previous project.

 

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

 

 

 


< Configuring IIS for Web Service Hosting | Main | Proxy Class BeginXXX and EndXXX Issue >