Team LiB   Previous Section   Next Section

4.3 Visual C++ Projects

VC++ web projects act more like nonweb VS.NET projects than like the managed web projects described earlier. All of the solution, project, and source files are kept on the local hard disk and not the web server. When you build a VC++ web project, all of the usual build and debug build directories are used, and not the local web cache folder. The only real difference between a nonweb project and a VC++ web project is that a VC++ web project has a final build step that copies the appropriate DLLs and content files to the web server.

4.3.1 Creating a New VC++ Web Project

Creating a new unmanaged web project is similar to creating a nonweb project. Unlike with managed web projects, you do not specify a remote web server in the New Project dialog—you just specify a folder on the local filesystem as usual. When you build an unmanaged web project, VS.NET communicates with IIS via DCOM (Distributed Component Object Model) and creates the appropriate web application for your project. (By default, it will use the project name, but you can change this in the Project Property Pages dialog—in the Web Deployment settings, the General section contains a Virtual Directory Name property that you can use to control where VS.NET will send the build output.)

The two basic types of VC++ web projects are ATL Server and ASP.NET Web Service (or Managed C++ Web Service as it was called in VS.NET 2002). Although they create different kinds of output, these projects interact with the web server in the same way.

ATL Server

An ATL Server project creates a new web application whose main executable is an ISAPI (Internet Server Application Programming Interface) extension DLL. This ISAPI extension responds dynamically to HTTP requests. There are two ATL Server project templates. ATL Server Project creates an ISAPI DLL that uses .srf files to create dynamic HTML UIs. ATL Server Web Service creates an ISAPI DLL that exposes a web service via SOAP (Simple Object Access Protocol). See ATL Internals, Second Edition (Addison-Wesley) for a more detailed discussion of ATL Server.

ASP.NET Web Service

An ASP.NET Web Service in Managed C++ is similar to ASP.NET Web Services in other managed languages. The ASP.NET Web Service template creates a project that provides a SOAP-based web service. The project builds a .NET assembly. It puts this assembly in the bin directory of a web application and then links a type in that assembly to an .asmx file (via the .asmx file's WebService directive).

.asmx Files

The documentation for .asmx files is scant. Their purpose is to map the URL for a web service onto the class that implements the service. The easiest way to see how they work is to look inside one, although that is easier said than done—VS.NET tries to stop you from editing their contents by always showing you the codebehind file instead of the .asmx file itself. (You can force it to open the .asmx file by right-clicking on the file in the Solution Explorer, selecting Open With, and choosing Source Code (Text) Editor.)

Most .asmx files contain just one line, a @WebService directive. This contains a Class attribute, which tells ASP.NET the name of the class that will handle web service requests directed to this endpoint. VS.NET places the class in a codebehind file (and it adds a Codebehind attribute to the directive so that it can find the relevant source file). ASP.NET also allows the source for the class to be placed inside the .asmx file itself, after the directive. (You can supply a Language attribute to tell ASP.NET which compiler it should use.) However, VS.NET doesn't make use of that—it always places the class definition in a codebehind file.

Here is a typical .asmx file generated by VS.NET:

<%@ WebService Language="c#" Codebehind="Svc1.asmx.cs"
    Class="WebSvc.Svc1" %>

It indicates that all web service requests directed to this file's URL will be handled by a class called WebSvc.Svc1, and the Codebehind hint tells VS.NET that this class is implemented in a file called Svc1.asmx.cs.

4.3.2 Files

VC++ web projects manage files in the typical VS.NET manner, keeping all of the source files in the project directory. Content files are copied to the web server automatically as part of the build process. (You can tell VS.NET which files are content by selecting the files in the Solution Explorer and setting their Content property to true.)

4.3.3 Building and Debugging

When a project is built, the files necessary for the web application are copied to the corresponding directory on the web server. If you need to deploy a VC++ project to another server, you will have to move the appropriate files by hand (as well as set up an appropriate IIS application).

When building an unmanaged project for debugging, all you need to do is make sure that you are building a Debug configuration. Otherwise, debugging is the same as any other project. See Chapter 3 for more detailed information about debugging.

    Team LiB   Previous Section   Next Section