Tải bản đầy đủ (.pdf) (1,283 trang)

professional visual basic 2010 and .net 4

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (32.66 MB, 1,283 trang )

www.it-ebooks.info
PROFESSIONAL
Visual Basic® 2010 and .NET 4
Bill Sheldon
Billy Hollis
Kent Sharkey
Jonathan Marbutt
Rob Windsor
Gastón C. Hillar
www.it-ebooks.info
Professional Visual Basic® 2010 and .NET 4
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-50224-2
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of
the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through
payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923,
(978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions
Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or
online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect
to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without
limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional


materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the
understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional
assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author
shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation
and/or a potential source of further information does not mean that the author or the publisher endorses the information the
organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web
sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United
States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic books.
Library of Congress Control Number: 2010921246
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries,
and may not be used without written permission. Visual Basic is a registered trademark of Microsoft Corporation in the United
States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not
associated with any product or vendor mentioned in this book.
www.it-ebooks.info
PROFESSIONAL
VISUAL BASIC® 2010 AND .NET 4
INTRODUCTION xxxv
PART I LANGUAGE CONSTRUCTS AND ENVIRONMENT 
CHAPTER 1 Visual Studio 2010 3
CHAPTER 2 Objects and Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
CHAPTER 3 Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137
CHAPTER 4 The Common Language Runtime 211
CHAPTER 5 Declarative Programming with Visual Basic . . . . . . . . . . . . . . . . . . . . . . 253
CHAPTER 6 Exception Handling and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
CHAPTER 7 Test-Driven Development 287
PART II BUSINESS OBJECTS AND DATA ACCESS 

CHAPTER 8 Arrays, Collections, and Generics 311
CHAPTER 9 Using XML with Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
CHAPTER 10 ADO.NET and LINQ 389
CHAPTER 11 Data Access with the Entity Framework . . . . . . . . . . . . . . . . . . . . . . . . . 437
CHAPTER 12 Working with SQL Server 457
CHAPTER 13 Services (XML/WCF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
PART III SMART CLIENT APPLICATIONS 
CHAPTER 14 Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
CHAPTER 15 Advanced Windows Forms 575
CHAPTER 16 User Controls Combining WPF and Windows Forms . . . . . . . . . . . . . . 599
CHAPTER 17 WPF Desktop Applications 617
CHAPTER 18 Expression Blend 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
CHAPTER 19 Silverlight 679
www.it-ebooks.info
PART IV INTERNET APPLICATIONS 
CHAPTER 20 Silverlight and Services 699
CHAPTER 21 Working with ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
CHAPTER 22 ASP.NET Advanced Features 751
CHAPTER 23 ASP.NET MVC 787
CHAPTER 24 SharePoint 2010 Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
PART V LIBRARIES AND SPECIALIZED TOPICS 
CHAPTER 25 Visual Studio Tools for Oce 843
CHAPTER 26 Windows Workflow Foundation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
CHAPTER 27 Localization 905
CHAPTER 28 COM-Interop 929
CHAPTER 29 Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
CHAPTER 30 Application Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
CHAPTER 31 Assemblies and Reflection 1001
CHAPTER 32 Security in the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1021
CHAPTER 33 Parallel Programming Using Tasks and Threads . . . . . . . . . . . . . . . . . .1051

CHAPTER 34 Deployment 1101
APPENDIX A The Visual Basic Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1137
APPENDIX B Visual Basic Power Packs Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151
APPENDIX C Workflow 2008 Specifics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1165
APPENDIX D Enterprise Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1185
APPENDIX E Programming for the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205
INDEX 1233
www.it-ebooks.info
ABOUT THE AUTHORS
BILL SHELDON is a software architect and engineer, originally from Baltimore, Maryland.
Holding a degree in computer science from the Illinois Institute of Technology (IIT), Bill has
been actively employed as a software engineer since resigning his commission with the United
States Navy. He is a Microsoft MVP for Visual Basic employed in Carlsbad, California. Bill also
works as an instructor for .NET courses at the University of California San Diego Extension. In
addition to writing books, Bill has published dozens of articles, including the Developer Update
Newsletter, SQL Server Magazine feature articles, and other Penton publications. He is an established online
presenter for MSDN and speaks at live events such as VSLive, DevConnections, Office Developers Conference,
and community events such as user groups and code camp. Bill is an avid cyclist and is active in the fight against
diabetes. Bill can be tracked down through his blog:
www.nerdnotes.net/blog or via Twitter: NerdNotes.
BILLY HOLLIS is an author and software consultant based in Nashville, Tennessee. Billy was
co-author of the first book ever published on Visual Basic .NET, as well as many other
books on software development. He is a member of the Microsoft Regional Director program
and a Microsoft MVP. In 2002, Billy was selected as one of the original .NET “Software
Legends.” He is heavily involved with consulting, training, and development on the .NET
platform, focusing on architecture, smart-client development, commercial packages, and
user-interface technologies. He regularly speaks on software development at major conferences all over the
world, including Microsoft’s PDC and TechEd events, DevConnections, VSLive, and architecture events such
as the Patterns and Practices Architect Summit.
KENT SHARKEY is an independent consultant who lives and codes in Comox, British

Columbia. Before going solo, Kent worked at Microsoft as a technical evangelist and content
strategist, promoting the use of .NET technologies. He lives with his wife, Margaret, and three
“children” — Squirrel, Cica, and Toffee.
JONATHAN MARBUTT is Vice President of Development for WayCool Software, Inc., based
in Birmingham, AL. He has been working professionally in software development since
1996, where he has covered various Microsoft technologies from VB6 to .NET. Over the
recent years, Jonathan has been developing with Silverlight to build Rich Internet Line of
Business applications for the non-profit sector. Through this development, he is beginning
to focus on User Experience (UX) by utilizing Microsoft products like Expression Blend
and technologies like Silverlight. For more information,contact Jonathan at
www.jmtechware.com.
ROB WINDSOR is a developer, trainer, writer and Senior Consultant with ObjectSharp
Consulting — a Microsoft Gold Partner based in Toronto, Canada. He has over fifteen years
experience developing rich-client and web applications with Delphi, VB, C# and VB.NET and
is currently spending a majority of his time working with SharePoint. Rob is a member of both
the INETA Speakers Bureau and the MSDN Canada Speakers Bureau and is a regular speaker
at conferences, code camps, and user groups across North America and Europe. He is President
of the Toronto Visual Basic User Group and has been recognized as a Microsoft Most Valuable
Professional for his involvement in the developer community.
www.it-ebooks.info
GASTÓN C. HILLAR has been working with computers since he was eight. He began
programming with the legendary Texas TI-99/4A and Commodore 64 home computers
in the early 80s. He has worked as developer, architect, and project manager for many
companies in Buenos Aires, Argentina. Now, he is an independent IT consultant working
for several Spanish, German, and Latin American companies, and a freelance author. He is
always looking for new adventures around the world.
Gastón is the author of more than forty books in Spanish and has written two books in English. He
contributes to Dr. Dobb’s Go Parallel programming portal at
www.ddj.com/go-parallel/, Dr. Dobb’s at
, and is a guest blogger at Intel Software Network at .

He lives with his wife, Vanesa, and his son, Kevin. When not tinkering with computers, he enjoys
developing and playing with wireless virtual reality devices and electronics toys with his father, his son, and
his nephew Nico.
You can reach him at:

You can follow him on Twitter at: />Gastón’s blog is at:
ABOUT THE TECHNICAL EDITORS
DIANNE SIEBOLD is a software developer and writer specializing in VB, C#, .NET Framework, WCF, ADO,
and SQL Server. She has worked for a number of Microsoft partners writing enterprise applications with an
emphasis on services and data access. Currently, she works for Microsoft writing developer documentation in
the Dynamics product group. Reach her by e-mail at

DOUG PARSONS is a .NET Software Architect and professional Technical Editor who specializes in C#,
SQL Server, and numerous architectural paradigms. Over the course of his career, he has worked on a
myriad of projects; most notably, however, was the United States 2008 Presidential Campaign website of
John McCain. He is currently employed by NJI New Media, writing software for clients of a predominantly
political nature. He can be reached by email at

DOUG WATERFIELD is a software architect and engineer who lives with his family in Avon, Indiana.
Since earning a degree in computer science from Rose-Hulman Institute of Technology, Doug has been
designing and developing commercial and enterprise applications in a wide variety of technologies. Doug
led development teams and departments for several firms before becoming an independent consultant with a
focus on .NET technologies. He is a retired officer in the US Army Reserve and serves as a volunteer leader
in Cub Scouts and Boy Scouts.
www.it-ebooks.info
ACKNOWLEDGMENTS
AS WITH ANY MAJOR PROJECT PUTTING A BOOK OF this size and scope together is a team effort and
we as authors were really lucky to have such a great support team helping to keep us on track and focused.
I’d like to publicly call out and thank our editor, Adaobi, who managed to turn around several of the late
chapters (those would be mine) in an incredibly short time, and our technical editors, in particular Dianne,

who did an outstanding job. They found hundreds of issues so that our readers wouldn’t need to, and
helped ensure that everything worked and flowed in a logical fashion.
—Bill Sheldon
WHILE WRITING can be a solitary activity, writing for a book definitely is not, and I have many people to
thank for getting me here.
Thank you to all my coworkers who picked up the slack while I worked on this (Oh, wait — I work alone —
never mind). Thank you to my co-authors, and my fine editors: Adaobi Obi Tulton, Paul Reese, and Dianne
Siebold. If there is any quality here, it is likely due to their hard work.
I am definitely grateful to all the people who regularly put up with my negativity, and help me to become
better: Eileen, Chris, Tharun, Kraig and Duncan. Thank you, thank you, thank you.
And finally, thanks to all of you that have selected this book. I hope it helps.
—Kent Sharkey
THANKS TO BETH MASSI for being too busy to work on this project and thanks to the people at Wrox for
accepting Beth’s suggestion that I would be a suitable replacement.
As a first time author, I have to give special thanks to Adaobi Obi Tulton, Dianne Siebold, Doug Parsons,
Doug Waterfield, and Luann Rouff for helping me make my work appear as if it was written by a
professional author. I’m sure it wasn’t easy.
Finally, I’d like to thank those who helped me advance professionally to the point that this opportunity was
even possible: Craig Flanagan, Sasha Krsmanovic, Jean-Rene Roy, Mark Dunn, Carl Franklin, Richard
Campbell, all the Canadian RDs, and everyone at ObjectSharp.
—Rob Windsor
I WISH TO ACKNOWLEDGE Paul Reese and Adaobi Obi Tulton; they had a lot of patience and they allowed
me to make the necessary changes to my chapter in order to include the most appropriate information about
the new parallel programming features introduced in .NET Framework 4 and Visual Basic 2010.
Special thanks go to my wife, Vanesa S. Olsen, because she understood that I needed to work with many
computers at the same time to test each code snippet.
—Gastón C. Hillar
www.it-ebooks.info
CONTENTS
INTRODUCTION xxxv

PART I: LANGUAGE CONSTRUCTS AND ENVIRONMENT
CHAPTER 1: VISUAL STUDIO 2010 3
Visual Studio 2010: Express through Ultimate 4
Visual Basic Keywords and Syntax 7
Console Applications 10
Creating a Project from a Project Template 11
The Solution Explorer 14
Project Properties 15
Assembly Information Screen 16
Compiler Settings 17
Debug Properties 21
References 22
Resources 24
Settings 25
Other Project Property Tabs 28
Project ProVB_VS2010 28
Form Properties Set in Code 31
Additional Components for Visual Studio 39
Enhancing a Sample Application 40
Customizing the Code 41
Building Applications 45
Reusing Your First Windows Form 52
Useful Features of Visual Studio 2010 54
Build Configurations 54
The Task List 56
The Command Window 57
Server Explorer 57
Recording and Using Macros in Visual Studio 2010 58
Class Diagrams 60
Application Lifecycle Management 61

Performance Tools 64
Summary 66
www.it-ebooks.info
CONTENTS
xvi
CHAPTER 2: OBJECTS AND VISUAL BASIC 67
Object-Oriented Terminology 68
Objects, Classes, and Instances 68
Composition of an Object 69
System.Object 72
Working With Visual Basic Types 72
Value and Reference Types 73
Primitive Types 75
Commands: Conditional 76
If Then 77
Comparison Operators 77
Select Case 79
Value Types (Structures) 79
Boolean 80
Integer Types 81
Unsigned Types 82
Decimal Types 82
Char and Byte 85
DateTime 85
Reference Types (Classes) 86
The Object Class 86
The String Class 87
XML Literals 91
The DBNull Class and IsDBNull Function 92
Parameter Passing 93

ParamArray 94
Variable Scope 94
Working with Objects 95
Objects Declaration and Instantiation 95
Object References 96
Dereferencing Objects 97
Early Binding versus Late Binding 97
Data Type Conversions 98
Performing Explicit Conversions 99
Creating Classes 103
Basic Classes 103
Handling Events 113
Handling Multiple Events 113
www.it-ebooks.info
CONTENTS
xvii
The WithEvents Keyword 114
Raising Events 114
Declaring and Raising Custom Events 115
Receiving Events with WithEvents 116
Receiving Events with AddHandler 117
Constructor Methods 119
Termination and Cleanup 119
Advanced Concepts 120
Overloading Methods 121
Overloading Constructor Methods 123
Shared Methods, Variables, and Events 124
Operator Overloading 128
Delegates 130
Classes versus Components 134

Lambdas 135
Summary 136
CHAPTER 3: CUSTOM OBJECTS 137
Inheritance 138
Implementing Inheritance 139
Interacting with the Base Class, Your Class, and Your Object 159
Simple Constructors 164
Creating an Abstract Base Class 175
Multiple Interfaces 177
Object Interfaces 177
Secondary Interfaces 178
Abstraction 184
Encapsulation 187
Polymorphism 189
Method Signatures 189
Inheritance 198
When to Use Inheritance 198
How Deep to Go? 205
Summary 208
CHAPTER 4: THE COMMON LANGUAGE RUNTIME 211
Elements of a .NET Application 212
Modules 212
Assemblies 213
Types 214
www.it-ebooks.info
CONTENTS
xviii
Versioning and Deployment 214
Better Support for Versioning 214
Major.Minor.Build.Revision 215

Better Deployment 215
Cross-Language Integration 216
The Common Type System 216
Metadata 217
Better Support for Metadata 218
Attributes 218
The Reflection API 220
IL Disassembler 221
Memory Management 221
Traditional Garbage Collection 222
Faster Memory Allocation for Objects 228
Garbage Collector Optimizations 229
Namespaces 231
What Is a Namespace? 231
Namespaces and References 234
Common Namespaces 236
Importing and Aliasing Namespaces 238
Aliasing Namespaces 239
Referencing Namespaces in ASP.NET 240
Creating Your Own Namespaces 240
The My Keyword 242
My.Application 243
My.Computer 246
My.Forms Namespace 249
My.Resources 249
My.User 250
Extending the My Namespace 250
Summary 252
CHAPTER 5: DECLARATIVE PROGRAMMING
WITH VISUAL BASIC 253

Declarative Programming and Visual Basic 254
Using XAML to Create a Window 255
XAML Syntax 258
XAML Language Basics 259
XAML Directives 261
Using XAML to Declare a Workflow 262
Summary 264
www.it-ebooks.info
CONTENTS
xix
CHAPTER 6: EXCEPTION HANDLING AND DEBUGGING 267
New in Visual Studio 2010 Team System: Historical Debugging 267
Notes on Compatibility with VB6 268
Exceptions in .NET 268
Important Properties and Methods of an Exception 268
Structured Exception-Handling Keywords 269
The Try, Catch, and Finally Keywords 270
The Throw Keyword 271
Throwing a New Exception 272
The Exit Try Statement 273
Nested Try Structures 274
Using Exception Properties 275
The Message Property 276
The InnerException and TargetSite Properties 276
Interoperability with VB6-Style Error Handling 279
Error Logging 280
The Event Log 280
Events, Methods, and Properties 281
Writing to Trace Files 283
Summary 284

CHAPTER 7: TESTDRIVEN DEVELOPMENT 287
When and How to Test 288
Using Assertions 288
TDD Tools in Visual Studio 290
UnitTesting Walk-Through 291
Creating a Test 291
Running a Test 293
Testing Data Access Code 295
Using the Generate from Usage Feature 302
Other Visual Studio Editions 305
Third Party Testing Frameworks 306
Summary 307
PART II: BUSINESS OBJECTS AND DATA ACCESS
CHAPTER 8: ARRAYS, COLLECTIONS, AND GENERICS 311
Arrays 312
Multidimensional Arrays 313
The UBound Function 314
www.it-ebooks.info
CONTENTS
xx
The ReDim Statement 314
The Preserve Keyword 315
Collections 315
Generics 320
Using Generics 321
Nullable Types 322
Generic Types 322
Generic Methods 326
Creating Generics 327
Generic Types 327

Generic Methods 333
Constraints 334
Generics and Late Binding 337
Covariance and Contravariance 337
Summary 339
CHAPTER 9: USING XML WITH VISUAL BASIC 341
An Introduction to XML 342
XML Serialization 343
Source Code Style Attributes 347
System.Xml Document Support 348
XML Stream-Style Parsers 348
Writing an XML Stream 349
Reading an XML Stream 351
Document Object Model (DOM) 359
XSL Transformations 363
XSLT Transforming between XML Standards 366
Other Classes and Interfaces in System.Xml.Xsl 368
XML in ASP.NET 369
The XmlDataSource Server Control 369
The XmlDataSource Control’s Namespace Problem 373
The Xml Server Control 373
LINQ to XML 375
LINQ Helper XML Objects 375
XDocument 375
XElement 375
XNamespace 376
XAttribute 378
Visual Basic and XML Literals 379
Using LINQ to Query XML Documents 380
www.it-ebooks.info

CONTENTS
xxi
Querying Static XML Documents 380
Querying Dynamic XML Documents 381
Working with the XML Document 382
Reading from an XML Document 382
Writing to an XML Document 384
Lambda Expressions in Visual Basic 385
Summary 387
CHAPTER 10: ADO.NET AND LINQ 389
ADO.NET Architecture 390
Basic ADO.NET Features 391
Common ADO.NET Tasks 391
Basic ADO.NET Namespaces and Classes 395
ADO.NET Components 396
.NET Data Providers 398
Connection Object 398
Command Object 399
Using Stored Procedures with Command Objects 399
DataReader Object 402
Executing Commands Asynchronously 404
DataAdapter Objects 406
SQL Server .NET Data Provider 409
OLE DB .NET Data Provider 409
The DataSet Component 410
DataTableCollection 410
DataRelationCollection 410
ExtendedProperties 411
Creating and Using DataSet Objects 411
ADO.NET DataTable Objects 413

Advanced ADO.NET Features of the DataSet and DataTable Objects 414
Working with the Common Provider Model 416
Connection Pooling in ADO.NET 418
Transactions and System.Transactions 418
Creating Transactions 419
Creating Resource Managers 421
LINQ to SQL 421
LINQ to SQL and Visual Basic 422
Retrieving Data Using LINQ to SQL: Creating the Console Application 422
Introducing the O/R Designer 423
Creating the Product Object 424
www.it-ebooks.info
CONTENTS
xxii
How Objects Map to LINQ Objects 425
The DataContext Object 426
The Table(TEntity) Object 428
Querying the Database 428
Using Query Expressions 429
Query Expressions in Detail 429
Filtering Using Expressions 429
Performing Joins 430
Grouping Items 431
Stored Procedures 432
Updating the Database 433
Summary 435
CHAPTER 11: DATA ACCESS WITH THE ENTITY FRAMEWORK 437
Object-Relational Mapping 438
Entity Framework Architecture 438
Conceptual Model 439

Storage Model 442
Mapping Model 443
LINQ to Entities 443
The ObjectContext 445
Mapping Objects to Entities 446
Simple Mapping 446
Using a Single Table for Multiple Objects 448
Using Multiple Tables for an Object 450
Generating the Database from a Model 452
Updating the Model 454
Summary 456
CHAPTER 12: WORKING WITH SQL SERVER 457
SQL Server Compact 458
Connecting to a SQL Server Compact Database 459
Synchronizing Data 462
SQL Server’s Built-in XML Features 468
CLR Integration In SQL Server 470
Deciding between T-SQL and Visual Basic 470
Creating User-Defined Types 471
Creating Stored Procedures 483
Exposing Web Services from SQL Server 489
SQL Server 2008 Features 494
www.it-ebooks.info
CONTENTS
xxiii
WCF Data Services 495
REST 495
Atom and JSON 495
Exposing Data Using WCF Data Services 497
WCF Data Services Client Library 501

Summary 505
CHAPTER 13: SERVICES XML/WCF 507
Introduction to Services 508
The Network Angle 508
Application Development 508
Merging the Network and Application Development 508
The Foundations of Web Services 509
The Problems 510
Some Other Players 511
Web Services 511
What Makes a WCF Service 512
The Larger Move to SOA 513
Capabilities of WCF 514
Contracts and Metadata 514
Working with the WS-* Protocols 515
Building a WCF Service 516
Building a WCF Consumer 523
Adding a Service Reference 523
Reviewing the Reference 525
Configuration File Changes 528
Writing the Consumption Code 529
Working with Data Contracts 531
Building a Service with a Data Contract 532
Namespaces 533
Building the Host 533
Building the Consumer 534
Looking at WSDL and the Schema for HelloCustomerService 536
Summary 538
PART III: SMART CLIENT APPLICATIONS
CHAPTER 14: WINDOWS FORMS 541

The System.Windows.Forms Namespace 541
Using Forms 542
Setting a Startup Form 542
www.it-ebooks.info
CONTENTS
xxiv
Showing Forms via Sub Main 543
More about the Application Class 543
Startup Location for a Form 543
Form Borders 544
Always on Top — the TopMost Property 544
Owned Forms 544
Making Forms Transparent and Translucent 545
Visual Inheritance 547
Scrollable Forms 547
MDI Forms 547
An MDI Example in VB 2010 548
Dialog Forms 549
Forms at Runtime 550
Default Instances of Forms 551
Controls 552
Control Tab Order 552
Properties for All Controls 552
Dynamic Sizing and Positioning of Controls 553
FlowLayoutPanel Control 555
TableLayoutPanel Control 556
Panel and GroupBox Container Controls 556
Extender Providers 557
Advanced Capabilities for Data Entry 559
Validating Data Entry 561

Toolbars and the ToolStrip Control 562
Menus 564
Common Dialogs 566
Drag and Drop 568
Summary of Standard Windows.Forms Controls 569
Handling Groups of Related Controls 571
Adding Controls at Runtime 572
Other Handy Programming Tips 573
Summary 573
CHAPTER 15: ADVANCED WINDOWS FORMS 575
Packaging Logic in Visual Controls 575
Custom Controls in Windows Forms 576
Inheriting from an Existing Control 576
Building a Composite Control 576
Writing a Control from Scratch 577
www.it-ebooks.info
CONTENTS
xxv
Inheriting from an Existing Control 577
Process Overview 577
Writing Code for an Inherited Control 578
Other Useful Attributes 581
Defining a Custom Event for the Inherited Control 581
A CheckedListBox Limiting Selected Items 582
The Control and UserControl Base Classes 584
The Control Class 585
The UserControl Class 585
A Composite UserControl 586
Creating a Composite UserControl 586
Resizing the Control 587

Exposing Properties of Contained Controls 587
Stepping through the Example 588
Building a Control from Scratch 591
Painting a Custom Control with GDI+ 591
Attaching an Icon for the Toolbox 595
Embedding Controls in Other Controls 595
Summary 597
CHAPTER 16: USER CONTROLS COMBINING WPF
AND WINDOWS FORMS 599
The Integration Library 600
Hosting WPF Controls in Windows Forms 601
Creating a WPF Control Library 602
The Windows Forms Application 604
Hosting Windows Forms Controls in WPF 610
Integration Limitations 614
Summary 615
CHAPTER 17: WPF DESKTOP APPLICATIONS 617
What, Where, Why, How — WPF Strategy 618
Raster Graphics and Vector Graphics 619
Should Your Next Windows Project Use WPF? 619
Creating a WPF Application 620
Implementing a Custom WPF Application 621
Customizing the User Interface 633
Customizing the Buttons 641
Summary 666
www.it-ebooks.info
CONTENTS
xxvi
CHAPTER 18: EXPRESSION BLEND 3 667
Getting to Know Blend 668

Creating a New Project 668
SketchFlow 674
Your First SketchFlow 674
SketchFlow Player 676
Documenting Your SketchFlow 678
Summary 678
CHAPTER 19: SILVERLIGHT 679
What Is Silverlight? 679
Smooth Streaming 680
Industry Standard Video 680
Digital Rights Management 680
Starting a Silverlight Project 680
Silverlight Application 681
Silverlight Navigation Application 681
Silverlight Class Library 682
Silverlight Solution 682
Web Application 682
Application Library Caching 682
Silverlight Application 683
Controls 685
Layout Management 686
Adding Items to the Silverlight Project 692
Silverlight User Control 693
Silverlight Application Class 693
Silverlight Page 693
Silverlight Child Window 694
Silverlight Template Control 694
Silverlight Resource Dictionary 694
Silverlight Out of the Browser 694
Summary 695

PART IV: INTERNET APPLICATIONS
CHAPTER 20: SILVERLIGHT AND SERVICES 699
Services and Silverlight 699
ASMX Web Service 700
WCF Service 702
www.it-ebooks.info
CONTENTS
xxvii
ADO.NET Data Service 705
Model-View-ViewModel 714
Separation of Concerns 714
The Model 714
The View 717
The ViewModel 717
Summary 718
CHAPTER 21: WORKING WITH ASP.NET 719
The History of ASP.NET 719
Key Features of ASP.NET 720
Developer Productivity 720
Performance and Scalability 720
Localization 721
Health Monitoring 721
Easy Access to Data 721
Administration and Management 721
Visual Studio Support for ASP.NET 721
Web Site and Web Application Projects 722
ASP.NET Application Folders 722
Web Server Options 723
Building ASP.NET Applications Using Web Forms 723
Pages, Forms, Controls, and Events 724

Data-Driven Applications 735
Data Binding with the SqlDataSource Control 735
Data Binding with the LinqDataSource Control 743
Data Binding with the ObjectDataSource Control 746
Summary 749
CHAPTER 22: ASP.NET ADVANCED FEATURES 751
Master Pages 751
Creating a Master Page 752
Creating the Content Page 755
Providing Default Content in Your Master Page 758
Navigation 758
Using the SiteMapPath Server Control 759
Menu Server Control 760
Working with the ASP.NET Provider Model 761
Creating an Application Services Database 762
Membership and Role Management 766
www.it-ebooks.info
CONTENTS
xxviii
Profile Properties 771
Microsoft Ajax (ASP.NET AJAX) 772
Understanding the Need for Ajax 773
Microsoft Ajax Implementation 773
UpdatePanel Control vs. Client-Side Service Calls 775
Introducing the Sample Project 775
Adding the UpdatePanel Control 778
Using Client-Side Service Calls and Client Templates 780
Summary 785
CHAPTER 23: ASP.NET MVC 787
Model-View-Controller and ASP.NET 788

Building an ASP.NET MVC Application 788
Creating the Project 788
Controllers and Actions 789
Adding the Model 792
Views 793
Routing 796
Scaolding and CRUD Operations 797
Validation 804
Summary 806
CHAPTER 24: SHAREPOINT 2010 DEVELOPMENT 807
Introduction 807
SharePoint Foundation 2010 808
SharePoint Server 2010 808
SharePoint Terminology 808
The SharePoint Development Environment 809
Features and the Solutions Framework 809
Features 809
Solution Framework 817
Visual Studio Tools for SharePoint Development 821
The SharePoint 2010 Object Models 827
Server Object Model 828
Client Object Models 831
Building Web Parts 833
Summary 839
www.it-ebooks.info
CONTENTS
xxix
PART V: LIBRARIES AND SPECIALIZED TOPICS
CHAPTER 25: VISUAL STUDIO TOOLS FOR OFFICE 843
Examining the VSTO Releases 844

Oce Automation versus VSTO 844
PIA-Free Deployment 844
VSTO Project Types 845
Oce Business Application Architecture 846
Working with Both VBA and VSTO 847
Creating a Document Template (Word) 852
Adding Content to the Document 854
Adding a Ribbon and an Actions Pane 856
Activating the Actions Pane 859
Updating a Content Control 862
Creating an Oce Add-In (Excel) 864
Outlook Form Regions 871
Summary 880
CHAPTER 26: WINDOWS WORKFLOW FOUNDATION 881
Workflow in Applications 881
Building Workflows 882
Adding Workflow with Windows Workflow Foundation 882
A Simple Workflow 884
Standard Activities 887
A Less Simple Workflow 889
Building Custom Activities 896
Dynamically Loading Workflows 899
Rehosting the Workflow Designer 900
Summary 903
CHAPTER 27: LOCALIZATION 905
Cultures and Regions 905
Understanding Culture Types 906
Looking at Your Thread 907
Declaring Culture Globally in ASP.NET 908
Adopting Culture Settings in ASP.NET 909

Translating Values and Behaviors 910
Understanding Dierences in Dates 910
Dierences in Numbers and Currencies 913
Understanding Dierences in Sorting 915
www.it-ebooks.info
CONTENTS
xxx
ASP.NET Resource Files 916
Making Use of Local Resources 916
Global Resources 921
Resource Files in Windows Forms 923
Summary 927
CHAPTER 28: COMINTEROP 929
Understanding COM 930
COM and .NET in Practice 931
A Legacy Component 931
The .NET Application 932
Trying It All Out 935
Using TlbImp Directly 935
Late Binding 936
ActiveX Controls 940
The Legacy ActiveX Control 940
A .NET Application, Again 941
Trying It All Out, Again 944
Using .NET Components in the COM World 944
A .NET Component 944
RegAsm 946
TlbExp 947
P/Invoke 947
Windows API Code Pack 948

Summary 948
CHAPTER 29: NETWORK PROGRAMMING 949
Protocols, Addresses, and Ports 949
Addresses and Names 951
Ports: They’re Not Just for Ships 952
Firewalls: Can’t Live with Them, Can’t Live without Them 952
The System.Net Namespace 953
Web Requests (and Responses) 953
Simplifying Common Web Requests with WebClient 958
Sockets 960
Building the Application 960
Creating Conversation Windows 962
Sending Messages 969
Shutting Down the Application 973
www.it-ebooks.info
CONTENTS
xxxi
Using Internet Explorer in Your Applications 976
Windows Forms and HTML — No Problem! 976
Summary 979
CHAPTER 30: APPLICATION SERVICES 981
Using IIS for Application Services 981
Windows Services 982
Characteristics of a Windows Service 982
Interacting with Windows Services 983
Creating a Windows Service 984
The .NET Framework Classes for Windows Services 984
Other Types of Windows Services 987
Creating a Windows Service in Visual Basic 987
Creating a File Watcher Service 988

Creating a Solution for the Windows Service 988
Adding .NET Components to the Service 989
Installing the Service 992
Starting the Service 992
Uninstalling the Service 993
Communicating with the Service 994
The ServiceController Class 994
Integrating a ServiceController into the Example 995
More about ServiceController 996
Custom Commands 997
Passing Strings to a Service 998
Debugging the Service 998
Summary 1000
CHAPTER 31: ASSEMBLIES AND REFLECTION 1001
Assemblies 1002
The Manifest 1002
Assembly Identity 1004
Referenced Assemblies 1006
Assemblies and Deployment 1006
Application-Private Assemblies 1006
Shared Assemblies 1007
Versioning Issues 1008
Application Isolation 1008
www.it-ebooks.info

×