Working with Legacy Code 699
Total Page:16
File Type:pdf, Size:1020Kb
14 0789728222 Ch10 4/7/03 11:08 AM Page 695 O BJECTIVES This chapter covers the following Microsoft-specified objectives for the “Creating User Services” section of Exam 70-315, “Developing and Implementing Web Applications with Microsoft Visual C# .NET and Microsoft Visual Studio .NET”: Incorporate existing code into ASP.NET pages. Add Web server controls, HTML server con- trols, user controls, and HTML code to ASP.NET pages. • Instantiate and invoke an ActiveX control. • Instantiate and invoke Web services or component • Instantiate and invoke a COM or COM+ component. • Call native functions by using platform invoke. Although the .NET Framework can handle nearly all of your application development needs, most organizations have already accumulated a large amount of useful code before they begin using the .NET Framework. It doesn’t make sense to simply throw away this legacy code and rewrite everything from scratch. Fortunately, if you’ve followed rec- ommendations to encapsulate your code into com- ponents over the years, you don’t need to abandon old code to start getting the benefits of the .NET Framework. Instead, you can make use of the .NET Framework’s interoperability features to use several types of legacy code: CHAPTER . ASP pages and ASP.NET pages can co-exist. 10 . ActiveX controls can be placed on Web forms. Component Object Model (COM) and COM+ components can be instantiated and Working with invoked by .NET code. The .NET platform invoke capability (usually Legacy Code referred to as PInvoke) can be used to call the Windows application programming interface (API). 14 0789728222 Ch10 4/7/03 11:08 AM Page 696 O BJECTIVES O UTLINE . By using these interoperability features, you can Introduction 698 ease your migration to .NET development. Making use of legacy components from .NET code means that you can migrate an application piece-by-piece Incorporating Existing Code 699 rather than trying to do it all at once. Running ASP and ASP.NET Together 699 Converting ASP Pages to ASP.NET 701 Using Late-Bound COM Components 702 Using ActiveX Controls 707 Using COM Components 711 Understanding Runtime Callable Wrappers 712 Using the Type Library Importer Tool (tlbimp.exe) 714 Using COM Components Directly 717 Using COM+ Components 719 Using Platform Invoke 723 Chapter Summary 726 Apply Your Knowledge 728 14 0789728222 Ch10 4/7/03 11:08 AM Page 697 S TUDY S TRATEGIES . If you have an existing ASP application, import . If you have an existing COM or COM+ object to the pages to an ASP.NET application and make work with, create a runtime callable wrapper for the changes necessary to run them under the object to investigate the conversion ASP.NET. process. If you don’t have any existing objects, . Convert several ActiveX controls for use in the you can build one with an older version of .NET Framework. Try using the Visual Studio Visual Basic or Visual C++. .NET tools for converting the ActiveX controls. Experiment with PInvoke to invoke some com- mon Windows API calls. 14 0789728222 Ch10 4/7/03 11:08 AM Page 698 698 Part I DEVELOPING WEB APPLICATIONS INTRODUCTION Migrating to a new development platform can be a painful process. In extreme cases, you might have to throw away the results of years of work when you decide it’s time for a new set of tools. This can make switching to a new platform a very difficult decision. Fortunately, Microsoft recognized the need to provide easy migra- tion paths from previous versions of its tools to the .NET world. In particular, if you heeded the advice to use COM for inter- component communications and to design your applications as sets of COM servers and clients, you’ll find the upgrade path to .NET smooth. That’s because the .NET Framework includes good support for interoperating with existing COM-based code. From .NET components, you can easily instantiate and call COM components such as ActiveX controls or COM libraries. (In fact, interoperability works in the other direction, too, with COM com- ponents able to call .NET code, although I won’t cover those tech- niques here.) Combine this with an existing modular architecture, and you get an easy migration path: Move one module at a time from COM to .NET, and use the .NET interoperability features so that the components can continue to talk to one another. In this chapter, you’ll learn about the facilities that the .NET Framework provides for using COM components and other legacy code. In particular, you’ll learn about the tools and techniques that are necessary to call ActiveX controls, COM components, and Windows API code from the .NET Framework. You’ll also see how you can move an existing ASP application to ASP.NET. 14 0789728222 Ch10 4/7/03 11:08 AM Page 699 Chapter 10 WORKING WITH LEGACY CODE 699 INCORPORATING EXISTING CODE Incorporate existing code into ASP.NET pages. Sometimes you have the luxury of starting a new project from scratch, and sometimes you don’t. Many organizations will be imple- menting ASP.NET on the same servers that already host existing ASP applications. Fortunately, ASP and ASP.NET work fine togeth- er. You can continue to run existing ASP pages on your ASP.NET servers, convert the pages to the new format, or move COM compo- nents from ASP pages to ASP.NET pages. In this section, you’ll learn about this level of interoperability. Running ASP and ASP.NET Together ASP and ASP.NET run perfectly well together on the same server. That’s a fundamental consequence of the architecture of the two sys- tems. When you install Internet Information Services (IIS), it associ- ates each file extension that the server understands with a particular application. ASP pages are handled by asp.dll (usually present in the System32\inetsrv folder of the Windows installation directory), whereas ASP.NET pages are handled by aspnet_isapi.dll (usually present in the Microsoft .NET Framework installation directory). Thus there’s no confusion on the part of the server between the two file types, and no need to worry that old pages will be executed incorrectly after you install ASP.NET. ASP pages and ASP.NET pages can even be incorporated into the same ASP.NET application, as you’ll see in the Step by Step 10.1. STEP BY STEP 10.1 Running ASP and ASP.NET Pages Together 1. Open a Visual C# ASP.NET Web application in the Visual Studio .NET IDE. 2. Add a new Web form to your Visual C# .NET project. Name the new Web form StepByStep10_1.aspx. continues 14 0789728222 Ch10 4/7/03 11:08 AM Page 700 700 Part I DEVELOPING WEB APPLICATIONS continued 3. Add two Label controls (lblDate and lblSession) and a HyperLink control to the Web form. Set the Text property of the HyperLink control to “Go to Classic ASP page” and set its NavigateUrl property to StepByStep10_1.asp. 4. Add a new Text File to your Visual C# .NET project. Name the new text file StepByStep10_1.asp. This will make the file an ASP file. 5. Make sure that the ASP file is in HTML view in the designer and add this code to it: <html> <head> <title>Classic ASP page</title> <%@ Language=VBScript %> </head> <body> <% Dim vToday Dim sSession vToday = Date() sSession = Session(“RandomVar”) %> <p>Today’s date is: <%= vToday %></p> <p>The random session variable is: <%= sSession %></p> <p> <a href=”StepByStep10_1.aspx”> Return to ASP.NET page</a> </p> </body> </html> 6. Double-click the ASP.NET Web form and enter the fol- lowing code to handle the Page Load event: private void Page_Load(object sender, System.EventArgs e) { lblDate.Text = “Today’s date is: “ + DateTime.Today; Random r = new Random(); Session[“RandomVar”] = r.Next(1, 1000); lblSession.Text = “The random session variable is: “ + Session[“RandomVar”]; } 14 0789728222 Ch10 4/7/03 11:08 AM Page 701 Chapter 10 WORKING WITH LEGACY CODE 701 7. Set the ASP.NET Web form as the start page for the pro- ject. No Shared State If you run this example, you’ll discover one of the TIP 8. Run the project to display the ASP.NET page. Click the major problems in using ASP and hyperlink to go to the ASP page. You can click the hyper- ASP.NET pages together in the link on that page to return to the original page. same application. Session and EXAM application state is not shared between the two types of pages. If you set a session or application Being able to run both types of pages on the same server is a useful variable in ASP.NET code, there’s no technique, but in the long run you should probably want to migrate way to retrieve it from ASP code, all of your ASP code to ASP.NET code. That will allow you to make and vice versa. use of the improved features of ASP.NET in the applications. Converting ASP Pages to ASP.NET One strategy for migrating an existing ASP application to ASP.NET is to rename the existing files so that they have the aspx extension instead of the asp extension. As soon as you do this, the pages will be delivered by ASP.NET instead of ASP. The syntax of ASP.NET pages is very close to the syntax of ASP pages. But it’s not identical. Here’s a partial list of things you might need to change if you want to convert an existing ASP page to run as an ASP.NET page. á In ASP, you could declare global variables and methods in <%...%> blocks, and they would be visible to all code on the page.