Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro CSharp And The .NET 2.0 Platform (2005) [eng]

.pdf
Скачиваний:
92
Добавлен:
16.08.2013
Размер:
10.35 Mб
Скачать

xxx C O N T E N T S

Understanding the Application/Session Distinction . . . . . . . . . . . . . . . . . . . . . . 897

Maintaining Application-Level State Data . . . . . . . . . . . . . . . . . . . . . . . . 898

Modifying Application Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899

Handling Web Application Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900

Working with the Application Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901

Fun with Data Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901

Modifying the *.aspx File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903

Maintaining Session Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906

Additional Members of HttpSessionState. . . . . . . . . . . . . . . . . . . . . . . . . 908

Understanding Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909

Creating Cookies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909

Reading Incoming Cookie Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911

Configuring Your ASP.NET Web Application Using Web.config . . . . . . . . . . . . . . 912

Enabling Tracing via <trace> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913

Customizing Error Output via <customErrors> . . . . . . . . . . . . . . . . . . . . 914

Options for Storing State via <sessionState> . . . . . . . . . . . . . . . . . . . . . 915

The ASP.NET 2.0 Site Administration Utility . . . . . . . . . . . . . . . . . . . . . . . 916

Configuration Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918

CHAPTER 25 Understanding XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . .

919

The Role of XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919

Benefits of XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919

Defining an XML Web Service Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920

The Building Blocks of an XML Web Service . . . . . . . . . . . . . . . . . . . . . . 921

Previewing XML Web Service Discovery . . . . . . . . . . . . . . . . . . . . . . . . . 921

Previewing XML Web Service Description . . . . . . . . . . . . . . . . . . . . . . . . 921

Previewing the Transport Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922

The .NET XML Web Service Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922

Examining the System.Web.Services Namespace. . . . . . . . . . . . . . . . . . 922

Building an XML Web Service by Hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923

Testing Your XML Web Service Using WebDev.WebServer.exe . . . . . . . . 924

Testing Your Web Service Using IIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925

Viewing the WSDL Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925

The Autogenerated Test Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925

Providing a Custom Test Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925

Building an XML Web Service Using Visual Studio 2005 . . . . . . . . . . . . . . . . . . 926

Implementing the TellFortune() Web Method . . . . . . . . . . . . . . . . . . . . . . 928

The Role of the WebService Base Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929

Understanding the [WebService] Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929

The Effect of the Namespace and Description Properties . . . . . . . . . . . . 930

The Name Property. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930

C O N T E N T S xxxi

Understanding the [WebServiceBinding] Attribute. . . . . . . . . . . . . . . . . . . . . . . 931

Ignoring BP 1.1 Conformance Verification . . . . . . . . . . . . . . . . . . . . . . . . 932

Disabling BP 1.1 Conformance Verification . . . . . . . . . . . . . . . . . . . . . . . 932

Understanding the [WebMethod] Attribute. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932

Documenting a Web Method via the Description Property. . . . . . . . . . . . 932

Avoiding WSDL Name Clashes via the MessageName Property . . . . . . . 933

Building Stateful Web Services via the EnableSession Property . . . . . . . 933

Exploring the Web Service Description Language (WSDL) . . . . . . . . . . . . . . . . 935

Defining a WSDL Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936

The <types> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937

The <message> Element. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938

The <portType> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938

The <binding> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939

The <service> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939

Revisiting the XML Web Service Wire Protocols . . . . . . . . . . . . . . . . . . . . . . . . 940

HTTP GET and HTTP POST Bindings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940

SOAP Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941

The wsdl.exe Command-Line Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942

Transforming WSDL into a Server-Side XML Web Service Skeleton . . . . 943

Transforming WSDL into a Client-Side Proxy . . . . . . . . . . . . . . . . . . . . . . 944

Examining the Proxy Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944

The Default Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945

Synchronous Invocation Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946

Asynchronous Invocation Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946

Building the Client Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947

Generating Proxy Code Using Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . 947

Exposing Custom Types from Web Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 948

Exposing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949

Exposing Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949

Exposing ADO.NET DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950

A Windows Forms Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951

Client-Side Type Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952

Understanding the Discovery Service Protocol (UDDI). . . . . . . . . . . . . . . . . . . . 953

Interacting with UDDI via VS .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954

INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955

About the Author

ANDREW TROELSEN is a Microsoft MVP (Visual C#) and a partner, trainer, and consultant with Intertech Training (http://www.IntertechTraining.com), a .NET and J2EE developer education center. He is the author of numerous books, including Developer’s Workshop to COM and ATL 3.0 (Wordware Publishing, 2000), COM and .NET Interoperability (Apress, 2002), Visual Basic .NET and the .NET Platform: An Advanced Guide (Apress, 2001), and the award-winning C# and the .NET Platform (Apress, 2003). Andrew has also authored numerous articles on .NET for MSDN online and MacTech (where he explored the platform-independent aspects of the .NET platform), and he is a frequent speaker at various .NET conferences and user groups.

Andrew currently lives in Minneapolis, Minnesota, with his wife, Amanda. He spends his free time waiting for the Wild to win the Stanley Cup, the Vikings to win the Super Bowl (before he retires would be nice), and the Timberwolves to grab numerous NBA championship titles.

xxxiii

About the Technical

Reviewer

GAVIN SMYTH is a professional software engineer with more years’ experience in development than he cares to admit, on projects ranging from device drivers to multihost applications; under platforms as diverse as “bare metal,” real-time operating systems, Unix, and Windows; and in languages including assembler, C++, Ada, and C#, among a good many others. He has worked for clients such as BT and Nortel, and is currently employed by Microsoft. Gavin has published a few pieces of technical prose as well (EXE and Wrox, where are you now?), but finds criticizing other people’s work much more fulfilling. Beyond that, when he’s not battling weeds and ants in the garden, he tries to persuade LEGO robots to do what he wants them to do (it’s for the kids’ benefit—honest).

xxxv

Acknowledgments

Completing the third edition of this book would have been completely impossible without the assistance and talent offered by numerous individuals. First of all, many thanks to the entire Apress crew. As always, each of you did an outstanding job massaging my raw manuscript into a polished product. Next, I must thank my technical reviewer, Gavin Smyth (aka Eagle Eye), who did a truly wonderful job of keeping me honest. Of course, any remaining errors (spelling, coding, or otherwise) that may have snuck into this book are my sole responsibility.

Thanks to my friends and family who (yet again) tolerated my lack of time and sometimes grumpy demeanor. More thanks to my friends and coworkers at Intertech Training. Your support (directly and indirectly) is greatly appreciated. Finally, thanks to my wife, Mandy, and “all the kids” for their love and encouragement.

xxxvii

Introduction

Iremember a time years ago when I proposed a book to Apress regarding a forthcoming software SDK code-named Next Generation Windows Services (NGWS). As you may be aware, NGWS eventually became what we now know as the .NET platform. My research of the C# programming language and the .NET platform took place in parallel with the authoring of the initial manuscript. It was a fantastic project; however, I must confess that it was more than a bit nerve-racking writing about a technology that was undergoing drastic changes over the course of its development. Thankfully, after many sleepless nights, the first edition of C# and the .NET Platform was published in conjunction with the release of .NET 1.0 Beta 2, circa the summer of 2001.

Since that point, I have been extremely happy and grateful to see that this text was very well received by the press and, most important, by readers. Over the years it was nominated as a Jolt Award finalist (I lost . . . crap!) and for the 2003 Referenceware Excellence Award in the programming book category (I won? Cool!).

The second edition of this text (C# and the .NET Platform, Second Edition) provided me the opportunity to expand upon the existing content with regard to version 1.1 of the .NET platform. Although the second edition of the book did offer a number of new topics, a number of chapters and examples were unable to make it into the final product.

Now that the book has entered its third edition, I am happy to say that the manuscript contains (almost) all of the topics and examples I was unable to cram into the previous versions. Not only does this edition of the text account for the numerous bells and whistles brought about by .NET 2.0 but it also incorporates a number of chapters that have long been written but not yet published (such as content on the common intermediate language, or CIL).

As with the earlier editions, this third edition presents the C# programming language and .NET base class libraries using a friendly and approachable tone. I have never understood the need some technical authors have to spit out prose that reads more like a GRE vocabulary study guide than a readable book. As well, this new edition remains focused on providing you with the information you need to build software solutions today, rather than spending too much time examining esoteric details that few individuals will ever actually care about.

We’re a Team, You and I

Technology authors write for a demanding group of people (I should know—I’m one of them). You know that building software solutions using any platform is extremely detailed and is very specific to your department, company, client base, and subject matter. Perhaps you work in the electronic publishing industry, develop systems for the state or local government, or work at NASA or a branch of the military. Speaking for myself, I have developed children’s educational software, various n-tier systems, and numerous projects within the medical and financial industries. The chances are almost 100 percent that the code you write at your place of employment has little to do with the code I write at mine (unless we happened to work together previously!).

Therefore, in this book, I have deliberately chosen to avoid creating examples that tie the example code to a specific industry or vein of programming. Given this, I choose to explain C#, OOP, the CLR, and the .NET 2.0 base class libraries using industry-agnostic examples. Rather than having every blessed example fill a grid with data, calculate payroll, or whatnot, I’ll stick to subject matter we

xxxix