Contents

Acknowledgments xiii Introduction xlvii

Part I: The C# Language 1

Chapter 1: .NET Architecture 3 The Relationship of C# to .NET 4 The 4 Platform Independence 4 Performance Improvement 4 Language Interoperability 5 A Closer Look at Intermediate Language 7 Support for Object Orientation and Interfaces 7 Distinct Value and Reference Types 8 Strong Data Typing 8 Error Handling with Exceptions 14 Use of Attributes 15 Assemblies 15 Private Assemblies 16 Shared Assemblies 16 Reflection 17 .NET Framework Classes 17 Namespaces 18 Creating .NET Applications Using C# 19 Creating ASP .NET Applications 19 Creating WindowsCOPYRIGHTED Forms MATERIAL 21 Using the Windows Presentation Foundation (WPF) 21 Windows Controls 21 Windows Services 21 Windows Communication Foundation (WCF) 22 The Role of C# in the .NET Enterprise Architecture 22 Summary 23

fftoc.inddtoc.indd xxvv 22/19/08/19/08 88:30:27:30:27 PPMM Contents

Chapter 2: C# Basics 25 Before We Start 25 Your First C# Program 26 The Code 26 Compiling and Running the Program 26 A Closer Look 27 Variables 29 Initialization of Variables 29 Type Inference 30 Variable Scope 31 Constants 34 Predefined Data Types 34 Value Types and Reference Types 35 CTS Types 36 Predefined Value Types 36 Predefined Reference Types 40 Flow Control 42 Conditional Statements 42 Loops 45 Jump Statements 49 Enumerations 50 Arrays 51 Namespaces 52 The using Directive 53 Namespace Aliases 54 The Main() Method 55 Multiple Main() Methods 55 Passing Arguments to Main() 56 More on Compiling C# Files 57 Console I/O 58 Using Comments 60 Internal Comments within the Source Files 60 XML Documentation 61 The C# Preprocessor Directives 63 #define and #undef 63 #if, #elif, #else, and #endif 64 #warning and #error 65 #region and #endregion 65 #line 65 #pragma 66 C# Programming Guidelines 66

xvi

fftoc.inddtoc.indd xvixvi 22/19/08/19/08 8:30:288:30:28 PMPM Contents

Rules for Identifiers 66 Usage Conventions 67 Summary 73

Chapter 3: Objects and Types 75 Classes and Structs 76 Class Members 76 Data Members 77 Function Members 77 Anonymous Types 91 Structs 92 Structs Are Value Types 93 Structs and Inheritance 94 Constructors for Structs 94 Partial Classes 95 Static Classes 96 The Object Class 97 System.Object Methods 97 The ToString() Method 98 Extension Methods 99 Summary 100

Chapter 4: Inheritance 101 Types of Inheritance 101 Implementation versus Interface Inheritance 101 Multiple Inheritance 102 Structs and Classes 102 Implementation Inheritance 103 Virtual Methods 104 Hiding Methods 104 Calling Base Versions of Functions 106 Abstract Classes and Functions 106 Sealed Classes and Methods 107 Constructors of Derived Classes 107 Modifiers 112 Visibility Modifiers 112 Other Modifiers 113 Interfaces 114 Defining and Implementing Interfaces 115 Derived Interfaces 118 Summary 120

xvii

fftoc.inddtoc.indd xviixvii 22/19/08/19/08 8:30:298:30:29 PMPM Contents

Chapter 5: Arrays 121 Simple Arrays 121 Array Declaration 121 Array Initialization 122 Accessing Array Elements 123 Using Reference Types 123 Multidimensional Arrays 125 Jagged Arrays 126 Array Class 127 Properties 127 Creating Arrays 127 Copying Arrays 128 Sorting 129 Array and Collection Interfaces 132 IEnumerable 132 ICollection 132 IList 132 Enumerations 133 IEnumerator Interface 134 foreach Statement 134 yield Statement 134 Summary 139

Chapter 6: Operators and Casts 141 Operators 141 Operator Shortcuts 143 The Conditional Operator 144 The checked and unchecked Operators 145 The is Operator 146 The as Operator 146 The sizeof Operator 146 The typeof Operator 146 Nullable Types and Operators 147 The Null Coalescing Operator 147 Operator Precedence 147 Type Safety 148 Type Conversions 149 Boxing and Unboxing 152 Comparing Objects for Equality 153 Comparing Reference Types for Equality 153 Comparing Value Types for Equality 154

xviii

fftoc.inddtoc.indd xviiixviii 22/19/08/19/08 8:30:298:30:29 PMPM Contents

Operator Overloading 155 How Operators Work 156 Operator Overloading Example: The Vector Struct 157 Which Operators Can You Overload? 163 User-Defined Casts 164 Implementing User-Defined Casts 165 Multiple Casting 171 Summary 175

Chapter 7: Delegates and Events 177 Delegates 177 Declaring Delegates in C# 178 Using Delegates in C# 179 Simple Delegate Example 182 BubbleSorter Example 184 Multicast Delegates 187 Anonymous Methods 190 Lambda Expressions 191 Covariance and Contra-variance 193 Events 194 The Receiver’s View of Events 195 Defining Events 197 Summary 201

Chapter 8: Strings and Regular Expressions 203 System.String 204 Building Strings 205 StringBuilder Members 208 Format Strings 209 Regular Expressions 214 Introduction to Regular Expressions 215 The RegularExpressionsPlayground Example 216 Displaying Results 219 Matches, Groups, and Captures 220 Summary 222

Chapter 9: Generics 223 Overview 223 Performance 224 Type Safety 225

xix

fftoc.inddtoc.indd xixxix 22/19/08/19/08 8:30:308:30:30 PMPM Contents

Binary Code Reuse 225 Code Bloat 226 Naming Guidelines 226 Creating Generic Classes 226 Generic Classes’ Features 231 Default Values 231 Constraints 232 Inheritance 234 Static Members 235 Generic Interfaces 235 Generic Methods 236 Generic Delegates 238 Implementing Methods Called by Delegates 238 Using Generic Delegates with the Array Class 240 Other Generic Framework Types 243 Nullable 243 EventHandler 244 ArraySegment 245 Summary 246

Chapter 10: Collections 247 Collection Interfaces and Types 247 Lists 250 Creating Lists 252 Read-Only Collections 261 Queues 261 Stacks 266 Linked Lists 268 Sorted Lists 275 Dictionaries 278 Key Type 278 Dictionary Example 280 Lookup 283 Other Dictionary Classes 284 HashSet 286 Bit Arrays 289 BitArray 289 BitVector32 291 Performance 294 Summary 296

xx

fftoc.inddtoc.indd xxxx 22/19/08/19/08 8:30:308:30:30 PMPM Contents

Chapter 11: Language Integrated Query 297 LINQ Overview 297 Query using List 298 Extension Methods 304 Lambda Expressions 306 LINQ Query 307 Deferred Query Execution 307 Standard Query Operators 309 Filtering 311 Filtering with Index 312 Type Filtering 312 Compound from 313 Sorting 314 Grouping 315 Grouping with Nested Objects 316 Join 317 Set Operations 318 Partitioning 319 Aggregate Operators 321 Conversion 322 Generation Operators 323 Expression Trees 324 LINQ Providers 327 Summary 328

Chapter 12: Memory Management and Pointers 329 Memory Management Under the Hood 329 Value Data Types 330 Reference Data Types 331 Garbage Collection 333 Freeing Unmanaged Resources 334 Destructors 335 The IDisposable Interface 336 Implementing IDisposable and a Destructor 337 Unsafe Code 339 Accessing Memory Directly with Pointers 339 Pointer Example: PointerPlayaround 347 Using Pointers to Optimize Performance 352 Summary 355

xxi

fftoc.inddtoc.indd xxixxi 22/19/08/19/08 8:30:308:30:30 PMPM Contents

Chapter 13: Reflection 357 Custom Attributes 358 Writing Custom Attributes 358 Custom Attribute Example: WhatsNewAttributes 362 Reflection 365 The System.Type Class 365 The TypeView Example 367 The Assembly Class 370 Completing the WhatsNewAttributes Example 371 Summary 376

Chapter 14: Errors and Exceptions 377 Exception Classes 378 Catching Exceptions 379 Implementing Multiple Catch Blocks 382 Catching Exceptions from Other Code 385 System.Exception Properties 386 What Happens If an Exception Isn’t Handled? 386 Nested try Blocks 387 User-Defined Exception Classes 389 Catching the User-Defined Exceptions 390 Throwing the User-Defined Exceptions 391 Defining the User-Defined Exception Classes 394 Summary 397

Part II: Visual Studio 399

Chapter 15: Visual Studio 2008 401 Working with Visual Studio 2008 401 Creating a Project 406 Solutions and Projects 412 Windows Application Code 415 Reading in Visual Studio 6 Projects 416 Exploring and Coding a Project 416 Building a Project 427 Debugging 430 Refactoring 434 Multi-Targeting 436 WPF, WCF, WF, and More 438

xxii

fftoc.inddtoc.indd xxiixxii 22/19/08/19/08 8:30:318:30:31 PMPM Contents

Building WPF Applications in Visual Studio 438 Building WF Applications in Visual Studio 439 Summary 441

Chapter 16: Deployment 443 Designing for Deployment 443 Deployment Options 444 Xcopy 444 Copy Web Tool 444 Publishing Web Sites 444 Deployment Projects 444 ClickOnce 444 Deployment Requirements 444 Deploying the .NET Runtime 446 Simple Deployment 446 Xcopy 447 Xcopy and Web Applications 447 Copy Web Tool 447 Publishing a Web Site 447 Installer Projects 448 What Is ? 449 Creating Installers 449 ClickOnce 458 ClickOnce Operation 458 Publishing an Application 459 ClickOnce Settings 459 Application Cache 460 Security 460 Advanced Options 460 Summary 466

Part III: Base Class Libraries 467

Chapter 17: Assemblies 469 What Are Assemblies? 469 Features of Assemblies 470 Assembly Structure 470 Assembly Manifests 472 Namespaces, Assemblies, and Components 472 Private and Shared Assemblies 472

xxiii

fftoc.inddtoc.indd xxiiixxiii 22/19/08/19/08 8:30:318:30:31 PMPM Contents

Satellite Assemblies 473 Viewing Assemblies 473 Creating Assemblies 474 Creating Modules and Assemblies 474 Assembly Attributes 475 Dynamic Loading and Creating Assemblies 478 Application Domains 481 Shared Assemblies 485 Strong Names 486 Integrity Using Strong Names 486 487 Creating a Shared Assembly 489 Create a Strong Name 489 Install the Shared Assembly 491 Using the Shared Assembly 491 Delayed Signing of Assemblies 492 References 493 Native Image Generator 494 Configuring .NET Applications 495 Configuration Categories 496 Configuring Directories for Assembly Searches 497 Versioning 499 Version Numbers 499 Getting the Version Programmatically 500 Application Configuration Files 500 Publisher Policy Files 504 Runtime Version 506 Summary 507

Chapter 18: Tracing and Events 509 Tracing 509 Trace Sources 511 Trace Switches 512 Trace Listeners 512 Filters 515 Asserts 516 Event Logging 517 Event-Logging Architecture 518 Event-Logging Classes 519 Creating an Event Source 521

xxiv

fftoc.inddtoc.indd xxivxxiv 22/19/08/19/08 8:30:328:30:32 PMPM Contents

Writing Event Logs 522 Resource Files 522 Event Log Listener 526 Performance Monitoring 527 Performance-Monitoring Classes 528 Performance Counter Builder 528 Adding PerformanceCounter Components 529 perfmon.exe 531 Summary 532

Chapter 19: Threading and Synchronization 533 Overview 534 Asynchronous Delegates 535 Polling 535 Wait Handle 536 Asynchronous Callback 537 The Thread Class 538 Passing Data to Threads 540 Background Threads 541 Thread Priority 542 Controlling Threads 543 Thread Pools 543 Threading Issues 545 Race Condition 545 Deadlock 548 Synchronization 549 lock Statement and Thread Safety 550 Interlocked 555 Monitor 557 Wait Handle 557 Mutex 559 Semaphore 560 Events 562 ReaderWriterLockSlim 564 Timers 568 COM Apartments 569 Event-Based Asynchronous Pattern 570 BackgroundWorker 571 Creating an Event-Based Asynchronous Component 576 Summary 581

xxv

fftoc.inddtoc.indd xxvxxv 22/19/08/19/08 8:30:328:30:32 PMPM Contents

Chapter 20: Security 583 Authentication and Authorization 583 Identity and Principal 583 Roles 585 Declarative Role-Based Security 585 Client Application Services 586 Encryption 591 Signature 594 Key Exchange and Secure Transfer 596 Access Control to Resources 599 602 Permissions 603 Code Groups 613 Code Access Permissions and Permissions Sets 618 Policy Levels: Machine, User, and Enterprise 621 Managing Security Policies 622 Managing Code Groups and Permissions 626 Turning Security On and Off 626 Creating a Code Group 626 Deleting a Code Group 627 Changing a Code Group’s Permissions 627 Creating and Applying Permissions Sets 628 Distributing Code Using a Strong Name 630 Distributing Code Using Certificates 631 Summary 637

Chapter 21: Localization 639 Namespace System.Globalization 639 Unicode Issues 640 Cultures and Regions 641 Cultures in Action 645 Sorting 650 Resources 651 Creating Resource Files 651 Resource File Generator 651 ResourceWriter 652 Using Resource Files 653 The System.Resources Namespace 659 Localization Using Visual Studio 659 Changing the Culture Programmatically 663 Using Custom Resource Messages 665

xxvi

fftoc.inddtoc.indd xxvixxvi 22/19/08/19/08 8:30:328:30:32 PMPM Contents

Automatic Fallback for Resources 666 Outsourcing Translations 666 Localization with ASP.NET 667 Localization with WPF 669 WPF Application 670 .NET Resources 670 Localization with XAML 671 A Custom Resource Reader 673 Creating a DatabaseResourceReader 674 Creating a DatabaseResourceSet 676 Creating a DatabaseResourceManager 676 Client Application for DatabaseResourceReader 677 Creating Custom Cultures 677 Summary 678

Chapter 22: Transactions 679 Overview 679 Transaction Phases 680 ACID Properties 681 Database and Entity Classes 681 Traditional Transactions 683 ADO.NET Transactions 683 System.EnterpriseServices 684 System.Transactions 685 Committable Transactions 687 Transaction Promotion 690 Dependent Transactions 692 Ambient Transactions 694 Isolation Level 701 Custom Resource Managers 703 Transactional Resources 704 Transactions with Windows Vista and Windows Server 2008 710 Summary 713

Chapter 23: Windows Services 715 What Is a Windows Service? 715 Windows Services Architecture 716 Service Program 717 Service Control Program 718 Service Configuration Program 718

xxvii

fftoc.inddtoc.indd xxviixxvii 22/19/08/19/08 8:30:338:30:33 PMPM Contents

System.ServiceProcess Namespace 718 Creating a Windows Service 719 A Class Library Using Sockets 719 TcpClient Example 722 Windows Service Project 725 Threading and Services 730 Service Installation 730 Installation Program 730 Monitoring and Controlling the Service 734 MMC Computer Management 735 net.exe 736 sc.exe 736 Visual Studio Server Explorer 737 ServiceController Class 737 Troubleshooting 745 Interactive Services 745 Event Logging 746 Power Events 746 Summary 748

Chapter 24: Interoperability 749 .NET and COM 750 Metadata 750 Freeing Memory 750 Interfaces 751 Method Binding 752 Data Types 753 Registration 753 Threading 753 Error Handling 754 Event Handling 754 Marshaling 755 Using a COM Component from a .NET Client 756 Creating a COM Component 756 Creating a Runtime Callable Wrapper 762 Using the RCW 763 Primary Interop Assemblies 764 Threading Issues 764 Adding Connection Points 765 Using ActiveX Controls in Windows Forms 768 Using COM Objects from within ASP .NET 771

xxviii

fftoc.inddtoc.indd xxviiixxviii 22/19/08/19/08 8:30:338:30:33 PMPM Contents

Using a .NET Component from a COM Client 771 COM Callable Wrapper 771 Creating a .NET Component 772 Creating a Type Library 772 COM Interop Attributes 774 COM Registration 777 Creating a COM Client 778 Adding Connection Points 780 Creating a Client with a Sink Object 781 Running Windows Forms Controls in Internet Explorer 782 Platform Invoke 783 Summary 787

Part IV: Data 789

Chapter 25: Manipulating Files and the Registry 791 Managing the File System 791 .NET Classes That Represent Files and Folders 792 The Path Class 795 Example: A File Browser 796 Moving, Copying, and Deleting Files 800 Example: FilePropertiesAndMovement 801 Looking at the Code for FilePropertiesAndMovement 802 Reading and Writing to Files 805 Reading a File 805 Writing to a File 807 Streams 808 Buffered Streams 810 Reading and Writing to Binary Files Using FileStream 810 Reading and Writing to Text Files 815 Reading Drive Information 822 File Security 824 Reading ACLs from a File 824 Reading ACLs from a Directory 825 Adding and Removing ACLs from a File 827 Reading and Writing to the Registry 828 The Registry 829 The .NET Registry Classes 830 Example: SelfPlacingWindow 833 Reading and Writing to Isolated Storage 839 Summary 844

xxix

fftoc.inddtoc.indd xxixxxix 22/19/08/19/08 8:30:338:30:33 PMPM Contents

Chapter 26: Data Access 845 ADO.NET Overview 846 Namespaces 846 Shared Classes 847 Database-Specific Classes 847 Using Database Connections 849 Managing Connection Strings 850 Using Connections Efficiently 852 Transactions 854 Commands 855 Executing Commands 856 Calling Stored Procedures 860 Fast Data Access: The Data Reader 863 Managing Data and Relationships: The DataSet Class 865 Data Tables 866 Data Relationships 873 Data Constraints 874 XML Schemas: Generating Code with XSD 877 Populating a DataSet 883 Populating a DataSet Class with a Data Adapter 883 Populating a DataSet from XML 884 Persisting DataSet Changes 884 Updating with Data Adapters 885 Writing XML Output 887 Working with ADO.NET 889 Tiered Development 889 Key Generation with SQL Server 890 Naming Conventions 892 Summary 894

Chapter 27: LINQ to SQL 895 LINQ to SQL and Visual Studio 2008 897 Calling the Products Table Using LINQ to SQL — Creating the Console Application 897 Adding a LINQ to SQL Class 898 Introducing the O/R Designer 899 Creating the Product Object 900 How Objects Map to LINQ Objects 902 The DataContext Object 903 The Table Object 907 Working Without the O/R Designer 907

xxx

fftoc.inddtoc.indd xxxxxx 22/19/08/19/08 8:30:338:30:33 PMPM Contents

Creating Your Own Custom Object 908 Querying with Your Custom Object and LINQ 908 Limiting the Columns Called with the Query 910 Working with Column Names 910 Creating Your Own DataContext Object 911 Custom Objects and the O/R Designer 912 Querying the Database 914 Using Query Expressions 914 Query Expressions in Detail 915 Filtering Using Expressions 916 Performing Joins 916 Grouping Items 918 Stored Procedures 919 Summary 920

Chapter 28: Manipulating XML 921 XML Standards Support in .NET 922 Introducing the System.Xml Namespace 922 Using System.Xml Classes 923 Reading and Writing Streamed XML 924 Using the XmlReader Class 924 Validating with XmlReader 928 Using the XmlWriter Class 930 Using the DOM in .NET 931 Using the XmlDocument Class 933 Using XPathNavigators 937 The System.Xml.XPath Namespace 937 The System.Xml.Xsl Namespace 942 XML and ADO.NET 948 Converting ADO.NET Data to XML 948 Converting XML to ADO.NET Data 954 Serializing Objects in XML 956 Serialization Without Source Code Access 963 Summary 965

Chapter 29: LINQ to XML 967 LINQ to XML and .NET 3.5 968 New Objects for Creating XML Documents 968 2008 Ventures Down Another Path 968 Namespaces and Prefixes 968

xxxi

fftoc.inddtoc.indd xxxixxxi 22/19/08/19/08 8:30:348:30:34 PMPM Contents

New XML Objects from the .NET Framework 3.5 969 XDocument 969 XElement 969 XNamespace 971 XComment 973 XAttribute 974 Using LINQ to Query XML Documents 974 Querying Static XML Documents 975 Querying Dynamic XML Documents 976 Working Around the XML Document 978 Reading from an XML Document 978 Writing to an XML Document 979 Using LINQ to SQL with LINQ to XML 981 Setting up the LINQ to SQL Components 981 Querying the Database and Outputting XML 982 Summary 983

Chapter 30: .NET Programming with SQL Server 985 .NET Runtime Host 986 .SqlServer.Server 987 User-Defined Types 988 Creating UDTs 988 Using UDTs 993 Using UDTs from Client-Side Code 994 Creating User-Defined Aggregates 996 Using User-Defined Aggregates 997 Stored Procedures 998 Creating Stored Procedures 998 Using Stored Procedures 999 User-Defined Functions 1000 Creating User-Defined Functions 1000 Using User-Defined Functions 1001 Triggers 1001 Creating Triggers 1002 Using Triggers 1003 XML Data Type 1003 Tables with XML Data 1003 Reading XML Values 1005 Query of Data 1008 XML Data Modification Language (XML DML) 1010

xxxii

fftoc.inddtoc.indd xxxiixxxii 22/19/08/19/08 8:30:348:30:34 PMPM Contents

XML Indexes 1010 Strongly Typed XML 1011 Summary 1013

Part V: Presentation 1015

Chapter 31: Windows Forms 1017 Creating a Windows Form Application 1018 Class Hierarchy 1023 Control Class 1023 Size and Location 1023 Appearance 1024 User Interaction 1024 Windows Functionality 1025 Miscellaneous Functionality 1026 Standard Controls and Components 1026 Button 1026 CheckBox 1028 RadioButton 1028 ComboBox, ListBox, and CheckedListBox 1029 DateTimePicker 1031 ErrorProvider 1031 HelpProvider 1032 ImageList 1033 Label 1033 ListView 1033 PictureBox 1035 ProgressBar 1035 TextBox, RichTextBox, and MaskedTextBox 1036 Panel 1037 FlowLayoutPanel and TableLayoutPanel 1037 SplitContainer 1038 TabControl and TabPages 1038 ToolStrip 1039 MenuStrip 1041 ContextMenuStrip 1042 ToolStripMenuItem 1042 ToolStripManager 1042 ToolStripContainer 1042 Forms 1043 Form Class 1043

xxxiii

fftoc.inddtoc.indd xxxiiixxxiii 22/19/08/19/08 8:30:348:30:34 PMPM Contents

Multiple Document Interface 1047 Custom Controls 1048 Summary 1059

Chapter 32: Data Binding 1061 The DataGridView Control 1061 Displaying Tabular Data 1062 Data Sources 1063 DataGridView Class Hierarchy 1072 Data Binding 1075 Simple Binding 1075 Data-Binding Objects 1076 Visual Studio .NET and Data Access 1080 Creating a Connection 1080 Selecting Data 1084 Updating the Data Source 1084 Other Common Requirements 1085 Summary 1092

Chapter 33: Graphics with GDI+ 1093 Understanding Drawing Principles 1094 GDI and GDI+ 1094 Drawing Shapes 1096 Painting Shapes Using OnPaint() 1099 Using the Clipping Region 1100 Measuring Coordinates and Areas 1102 Point and PointF 1102 Size and SizeF 1103 Rectangle and RectangleF 1105 Region 1106 A Note About Debugging 1106 Drawing Scrollable Windows 1107 World, Page, and Device Coordinates 1113 Colors 1114 Red-Green-Blue Values 1114 The Named Colors 1115 Graphics Display Modes and the Safety Palette 1115 The Safety Palette 1116 Pens and Brushes 1116 Brushes 1117 Pens 1117

xxxiv

fftoc.inddtoc.indd xxxivxxxiv 22/19/08/19/08 8:30:348:30:34 PMPM Contents

Drawing Shapes and Lines 1118 Displaying Images 1120 Issues When Manipulating Images 1123 Drawing Text 1123 Simple Text Example 1124 Fonts and Font Families 1125 Example: Enumerating Font Families 1126 Editing a Text Document: The CapsEditor Sample 1129 The Invalidate() Method 1133 Calculating Item Sizes and Document Size 1134 OnPaint() 1135 Coordinate Transforms 1137 Responding to User Input 1138 Printing 1141 Implementing Print and Print Preview 1143 Summary 1147

Chapter 34: Windows Presentation Foundation 1149 Overview 1149 XAML 1150 Cooperation of Designers and Developers 1154 Class Hierarchy 1155 Namespaces 1156 Shapes 1159 Transformation 1162 Brushes 1163 SolidColorBrush 1163 LinearGradientBrush 1164 RadialGradientBrush 1164 DrawingBrush 1165 ImageBrush 1165 VisualBrush 1166 Controls 1167 Simple Controls 1167 Content Controls 1168 Headered Content Controls 1170 Items Controls 1171 Headered Items Controls 1171 Layout 1172 StackPanel 1172 WrapPanel 1173

xxxv

fftoc.inddtoc.indd xxxvxxxv 22/19/08/19/08 8:30:358:30:35 PMPM Contents

Canvas 1173 DockPanel 1174 Grid 1175 Event Handling 1176 Styles, Templates, and Resources 1177 Styles 1177 Resources 1178 Triggers 1182 Templates 1184 Styling a ListBox 1192 Summary 1198

Chapter 35: Advanced WPF 1199 Data Binding 1199 Overview 1200 Binding with XAML 1200 Simple Object Binding 1203 Object Data Provider 1206 List Binding 1208 Value Conversion 1212 Adding List Items Dynamically 1213 Data Templates 1214 Binding to XML 1217 Binding Validation 1219 Command Bindings 1224 Animations 1228 Timeline 1229 Triggers 1233 Storyboard 1235 Adding 3-D Features in WPF 1237 Triangle 1238 Windows Forms Integration 1245 WPF Controls Within Windows Forms 1245 Windows Forms Controls Within WPF Applications 1247 WPF Browser Application 1249 Summary 1249

Chapter 36: Add-Ins 1251 System.AddIn Architecture 1251 Issues with Add-ins 1252 Pipeline Architecture 1253

xxxvi

fftoc.inddtoc.indd xxxvixxxvi 22/19/08/19/08 8:30:358:30:35 PMPM Contents

Discovery 1254 Activation and Isolation 1255 Contracts 1257 Lifetime 1258 Versioning 1259 Add-In Sample 1259 Calculator Contract 1261 Calculator Add-In View 1261 Calculator Add-In Adapter 1262 Calculator Add-In 1264 Calculator Host View 1265 Calculator Host Adapter 1265 Calculator Host 1267 Additional Add-Ins 1271 Summary 1271

Chapter 37: ASP.NET Pages 1273 ASP.NET Introduction 1274 State Management in ASP.NET 1274 ASP.NET Web Forms 1275 The ASP.NET Code Model 1278 ASP.NET Server Controls 1279 ADO.NET and Data Binding 1295 Updating the Event-Booking Application 1295 More on Data Binding 1302 Application Configuration 1308 Summary 1309

Chapter 38: ASP.NET Development 1311 User and Custom Controls 1312 User Controls 1312 Custom Controls 1318 Master Pages 1323 Accessing Master Page Content from Web Pages 1325 Nested Master Pages 1326 Master Pages in PCSDemoSite 1326 Site Navigation 1328 Navigation in PCSDemoSite 1330 Security 1331 Adding Forms Authentication Using the Security Wizard 1332 Implementing a Login System 1334

xxxvii

fftoc.inddtoc.indd xxxviixxxvii 22/19/08/19/08 8:30:358:30:35 PMPM Contents

Login Web Server Controls 1336 Securing Directories 1336 Security in PCSDemoSite 1337 Themes 1339 Applying Themes to Pages 1339 Defining Themes 1340 Themes in PCSDemoSite 1340 Web Parts 1344 Web Parts Application Components 1344 Web Parts Example 1346 Summary 1353

Chapter 39: ASP.NET AJAX 1355 What Is Ajax? 1356 What Is ASP.NET AJAX? 1358 Core Functionality 1359 ASP.NET AJAX Control Toolkit 1361 Using ASP.NET AJAX 1361 ASP.NET AJAX Web Site Example 1362 ASP.NET AJAX-Enabled Web Site Configuration 1365 Adding ASP.NET AJAX Functionality 1368 Using the AJAX Library 1374 Summary 1383

Chapter 40: Visual Studio Tools for Office 1385 VSTO Overview 1386 Project Types 1386 Project Features 1389 VSTO Fundamentals 1390 Office Object Model 1391 VSTO Namespaces 1391 Host Items and Host Controls 1392 Basic VSTO Project Structure 1394 The Globals Class 1397 Event Handling 1397 Building VSTO Solutions 1398 Managing Application-Level Add-Ins 1399 Interacting with Applications and Documents 1400 UI Customization 1401 Example Application 1405

xxxviii

fftoc.inddtoc.indd xxxviiixxxviii 22/19/08/19/08 8:30:358:30:35 PMPM Contents

VBA Interoperability 1415 Summary 1418

Part VI: Communication 1421

Chapter 41: Accessing the Internet 1423 The WebClient Class 1424 Downloading Files 1424 Basic Web Client Example 1424 Uploading Files 1426 WebRequest and WebResponse Classes 1426 Other WebRequest and WebResponse Features 1427 Displaying Output as an HTML Page 1429 Allowing Simple Web Browsing from Your Applications 1430 Launching Internet Explorer Instances 1432 Giving Your Application More IE-Type Features 1432 Printing Using the WebBrowser Control 1437 Displaying the Code of a Requested Page 1438 The Web Request and Web Response Hierarchy 1438 Utility Classes 1440 URIs 1440 IP Addresses and DNS Names 1441 Lower-Level Protocols 1443 Lower-Level Classes 1444 Summary 1453

Chapter 42: Windows Communication Foundation 1455 WCF Overview 1456 SOAP 1457 WSDL 1457 JSON 1458 Simple Service and Client 1458 Service Contract 1460 Service Implementation 1460 WCF Service Host and WCF Test Client 1461 Custom Service Host 1463 WCF Client 1464 Diagnostics 1466 Contracts 1467 Data Contract 1468

xxxix

fftoc.inddtoc.indd xxxixxxxix 22/19/08/19/08 8:30:358:30:35 PMPM Contents

Versioning 1468 Service Contract 1469 Message Contract 1470 Service Implementation 1471 Error Handling 1476 Binding 1477 Hosting 1480 Custom Hosting 1480 WAS Hosting 1481 Clients 1482 Duplex Communication 1484 Summary 1486

Chapter 43: Windows Workflow Foundation 1487 Hello World 1488 Activities 1489 IfElseActivity 1490 ParallelActivity 1491 CallExternalMethodActivity 1492 DelayActivity 1493 ListenActivity 1493 Activity Execution Model 1494 Custom Activities 1495 Activity Validation 1497 Themes and Designers 1498 ActivityToolboxItem and Icons 1500 Custom Composite Activities 1502 Workflows 1508 Sequential Workflows 1509 State Machine Workflows 1509 Passing Parameters to a Workflow 1511 Returning Results from a Workflow 1512 Binding Parameters to Activities 1513 The Workflow Runtime 1514 Workflow Services 1515 The Persistence Service 1517 The Tracking Service 1518 Custom Services 1520 Integration with Windows Communication Foundation 1521 Hosting Workflows 1524 The Workflow Designer 1526 Summary 1526

xl

fftoc.inddtoc.indd xlxl 22/19/08/19/08 8:30:368:30:36 PMPM Contents

Chapter 44: Enterprise Services 1527 Overview 1527 History 1528 Where to Use Enterprise Services 1528 Contexts 1529 Automatic Transactions 1529 Distributed Transactions 1530 Object Pooling 1530 Role-Based Security 1530 Queued Components 1530 Loosely Coupled Events 1530 Creating a Simple COM+ Application 1531 The ServicedComponent Class 1531 Sign the Assembly 1532 Assembly Attributes 1532 Creating the Component 1533 Deployment 1534 Automatic Deployment 1534 Manual Deployment 1534 Creating an Installer Package 1535 Component Services Explorer 1536 Client Application 1538 Transactions 1539 Transaction Attributes 1539 Transaction Results 1540 Sample Application 1540 Entity Classes 1541 The OrderControl Component 1543 The OrderData Component 1544 The OrderLineData Component 1546 Client Application 1548 Integrating WCF and Enterprise Services 1549 WCF Service Façade 1549 Client Application 1553 Summary 1554

Chapter 45: Message Queuing 1555 Overview 1555 When to Use Message Queuing 1556 Message Queuing Features 1557 Message Queuing Products 1558

xli

fftoc.inddtoc.indd xlixli 22/19/08/19/08 8:30:368:30:36 PMPM Contents

Message Queuing Architecture 1559 Messages 1559 Message Queue 1559 Message Queuing Administrative Tools 1560 Creating Message Queues 1560 Message Queue Properties 1561 Programming Message Queuing 1562 Creating a Message Queue 1562 Finding a Queue 1563 Opening Known Queues 1563 Sending a Message 1565 Receiving Messages 1567 Course Order Application 1569 Course Order Class Library 1570 Course Order Message Sender 1570 Sending Priority and Recoverable Messages 1571 Course Order Message Receiver 1573 Receiving Results 1576 Acknowledgment Queues 1576 Response Queues 1577 Transactional Queues 1577 Message Queuing with WCF 1579 Entity Classes with a Data Contract 1579 WCF Service Contract 1580 WCF Message Receiver Application 1581 WCF Message Sender Application 1583 Message Queue Installation 1585 Summary 1585

Chapter 46: Directory Services 1587 The Architecture of Active Directory 1588 Features 1588 Active Directory Concepts 1588 Characteristics of Active Directory Data 1592 Schema 1592 Administration Tools for Active Directory 1594 Active Directory Users and Computers 1594 ADSI Edit 1595 Programming Active Directory 1596 Classes in System.DirectoryServices 1598 Binding 1598 Cache 1605

xlii

fftoc.inddtoc.indd xliixlii 22/19/08/19/08 8:30:368:30:36 PMPM Contents

Creating New Objects 1605 Updating Directory Entries 1606 Accessing Native ADSI Objects 1607 Searching in Active Directory 1608 Searching for User Objects 1611 User Interface 1611 Get the Schema Naming Context 1612 Get the Property Names of the User Class 1613 Search for User Objects 1614 Account Management 1616 Display User Information 1618 Create a User 1618 Reset a Password 1618 Create a Group 1619 Add a User to a Group 1619 Finding Users 1619 DSML 1620 Classes in System.DirectoryServices.Protocols 1621 Searching for Active Directory Objects with DSML 1621 Summary 1623

Chapter 47: Peer-to-Peer Networking 1625 Peer-to-Peer Networking Overview 1625 Client-Server Architecture 1626 P2P Architecture 1627 P2P Architectural Challenges 1627 P2P Terminology 1628 P2P Solutions 1629 Microsoft Windows Peer-to-Peer Networking 1629 Peer Name Resolution Protocol (PNRP) 1629 People Near Me 1632 Building P2P Applications 1632 System.Net.PeerToPeer 1633 System.Net.PeerToPeer.Collaboration 1638 Summary 1642

Chapter 48: Syndication 1643 Overview of System.Servicemodel.Syndication 1643 Syndication Reader 1645 Offering Syndication Feeds 1647 Summary 1652

xliii

fftoc.inddtoc.indd xliiixliii 22/19/08/19/08 8:30:368:30:36 PMPM Contents

Part VII: Appendices 1653

Appendix A: ADO.NET 1655 Overview of the ADO.NET Entity Framework 1656 Entity Framework Layers 1657 Logical 1657 Conceptual 1659 Mapping 1660 Entities 1661 Object Context 1664 Relationships 1666 Table per Hierarchy 1666 Table per Type 1668 Object Query 1670 Updates 1674 Object Tracking 1674 Change Information 1675 Attaching and Detaching Entities 1677 Storing Entity Changes 1677 LINQ to Entities 1678 Summary 1679

Appendix B: C#, Visual Basic, and C++/CLI 1681 Namespaces 1682 Defining Types 1683 Reference Types 1683 Value Types 1684 Type Inference 1685 Interfaces 1685 Enumerations 1686 Methods 1687 Method Parameters and Return Types 1687 Parameter Modifiers 1688 Constructors 1689 Properties 1690 Object Initializers 1691 Extension Methods 1692 Static Members 1692 Arrays 1693 Control Statements 1694 if Statement 1694

xliv

fftoc.inddtoc.indd xlivxliv 22/19/08/19/08 8:30:378:30:37 PMPM Contents

Conditional Operator 1694 switch Statement 1694 Loops 1696 for Statement 1696 while and do . . . while Statements 1696 foreach Statement 1697 Exception Handling 1697 Inheritance 1699 Access Modifiers 1699 Keywords 1699 Resource Management 1701 IDisposable Interface Implementation 1701 Using Statement 1702 Override Finalize 1702 Delegates 1703 Events 1705 Generics 1707 LINQ Queries 1708 C++/CLI Mixing Native and Managed Code 1708 C# Specifics 1709 Summary 1710

Appendix C: Windows Vista and Windows Server 2008 1711 Vista Bridge 1711 User Account Control 1712 Applications Requiring Admin Privileges 1712 Shield Icon 1713 Directory Structure 1715 New Controls and Dialogs 1716 Command Link 1717 Task Dialog 1718 File Dialogs 1721 Search 1722 OLE DB Provider 1724 Advanced Query Syntax 1728 Summary 1729

Index 1731

xlv

fftoc.inddtoc.indd xlvxlv 22/19/08/19/08 8:30:378:30:37 PMPM fftoc.inddtoc.indd xlvixlvi 22/19/08/19/08 8:30:378:30:37 PMPM