Back to description
Throughout this book, we emphasize that the C# language must be considered in parallel with the .NET Framework, rather than... more
Throughout this book, we emphasize that the C# language must be considered in parallel with the .NET Framework, rather than viewed in isolation. The C# compiler specifically targets .NET, which means that all code written in C# will always run within the .NET Framework. This has two important consequences for the C# language:
1. The architecture and methodologies of C# reflect the underlying methodologies of .NET.
2. In many cases, specific language features of C# actually depend on features of .NET, or of the .NET base classes.
Because of this dependence, it is important to gain some understanding of the architecture and methodology of .NET before you begin C# programming. That is the purpose of this chapter. The following is an outline of what this chapter covers:
This chapter begins by explaining what happens when all code (including C#) that targets .NET is compiled and run.
Once you have this broad overview, you take a more detailed look at the Microsoft Intermediate Language (MSIL or simply IL); the assembly language that all compiled code ends up in on .NET. In particular, you see how IL, in partnership with the Common Type System (CTS) and Common Language Specification (CLS), works to give you interoperability between languages that target .NET. This chapter also discusses where common languages (including Visual Basic and C++) fit into .NET.
Next, you move on to examine some of the other features of .NET, including assemblies, namespaces, and the .NET base classes.
The chapter finishes with a brief look at the kinds of applications you can create as a C# developer.
... less
Now that you understand more about what C# can do, you will want to learn how to use it. This chapter gives you a good start... more
Now that you understand more about what C# can do, you will want to learn how to use it. This chapter gives you a good start in that direction by providing you with a basic knowledge of the fundamentals of C# programming, which is built on in subsequent chapters. The main topics covered are:
Declaring variables
Initialization and scope of variables
Predefined C# data types
Dictating the flow of execution within a C# program using loops and conditional statements
Enumerations
Namespaces
The Main() method
Main()
Basic command-line C# compiler options
Using System.Console to perform console I/O
System.Console
Using comments and documentation features
Preprocessor directives
Guidelines and conventions for good programming in C#
By the end of this chapter, you will know enough C# to write simple programs, though without using inheritance or other object-oriented features, which are covered in later chapters.
So far, you’ve been introduced to some of the building blocks of the C# language, including variables, data types, and program... more
So far, you’ve been introduced to some of the building blocks of the C# language, including variables, data types, and program flow statements, and you have seen a few very short complete programs containing little more than the Main() method. What you haven’t really seen yet is how to put all of these together to form a longer, complete program. The key to this lies in working with classesthe subject of this chapter. In particular, this chapter covers:
The differences between classes and structs
Class members
Passing values by value and by reference
Method overloading
Constructors and static constructors
Read-only fields
Partial classes
Static classes
The Object class, from which all other types are derived
Object
Note that we cover inheritance and features related to inheritance in Chapter 4, “Inheritance.”
Note
This chapter introduces the basic syntax associated with classes. However, we assume that you are already familiar with the underlying principles of using classesfor example, that you know what a constructor or a property is. This chapter is largely confined to applying those principles in C# code.
In this chapter, we introduce and explain those concepts that are not necessarily supported by most object-oriented languages. For example, although object constructors are a widely used concept that you should be familiar with, static constructors are something new to C#, so this chapter explains how static constructors work.
Chapter 3, “Objects and Types,” examined how to use individual classes in C#. The focus in that chapter was how to define... more
Chapter 3, “Objects and Types,” examined how to use individual classes in C#. The focus in that chapter was how to define methods, constructors, properties, and other members of a single class (or a single struct). Although you did learn that all classes are ultimately derived from the class System.Object, you did not see how to create a hierarchy of inherited classes. Inheritance is the subject of this chapter. In this chapter, you will see how C# and the .NET Framework handle inheritance. Topics covered include:
System.Object
Types of inheritance
Implementing inheritance
Access modifiers
Interfaces
If you need to work with multiple objects of the same type, you can use collections and arrays. C# has a special notation... more
If you need to work with multiple objects of the same type, you can use collections and arrays. C# has a special notation to declare and use arrays. Behind the scenes, the Array class comes into play, which offers several methods to sort and filter the elements inside the array.
Array
Using an enumerator, you can iterate through all the elements of an array.
This chapter discusses the following:
Simple arrays
Multidimensional arrays
Jagged arrays
The Array class
Interfaces for arrays
The preceding chapters have covered most of what you need to start writing useful programs using C#. This chapter completes... more
The preceding chapters have covered most of what you need to start writing useful programs using C#. This chapter completes the discussion of the essential language elements and begins to illustrate some powerful aspects of C# that allow you to extend the capabilities of the C# language. Specifically, this chapter discusses the following:
The operators available in C#
The idea of equality when dealing with reference and value types
Data conversion between the primitive data types
Converting value types to reference types using boxing
Converting between reference types by casting
Overloading the standard operators to support operations on the custom types you define
Adding cast operators to the custom types you define to support seamless data typeconversions
Callback functions are an important part of programming in Windows. If you have a background in C or C++ programming, you... more
Callback functions are an important part of programming in Windows. If you have a background in C or C++ programming, you have seen callbacks used in many of the Windows APIs. With the addition of the AddressOf keyword, Visual Basic developers are now able to take advantage of the API that once was off limits. Callback functions are really pointers to a method call. Also known as function pointers, they are a very powerful programming feature. .NET has implemented the concept of a function pointer in the form of delegates. What makes them special is that unlike the C function pointer, the .NET delegate is type-safe. What this means is that a function pointer in C is nothing but a pointer to a memory location. You have no idea what that pointer is really pointing to. Things like parameters and return types are not known. As you see in this chapter, .NET has made delegates a type-safe operation. Later in the chapter, you see how .NET uses delegates as the means of implementing events.
AddressOf
The main topics of this chapter are:
Delegates
Anonymous methods
Lambda expressions
Events
Since the beginning of this book, you have been using strings almost constantly and might not have realized that the stated... more
Since the beginning of this book, you have been using strings almost constantly and might not have realized that the stated mapping that the string keyword in C# actually refers to is the System.String .NET base class. System.String is a very powerful and versatile class, but it is by no means the only string-related class in the .NET armory. This chapter starts by reviewing the features of System.String and then looks at some nifty things you can do with strings using some of the other .NET classesin particular those in the System.Text and System.Text.RegularExpressions namespaces. This chapter covers the following areas:
string
System.String
System.Text
System.Text.RegularExpressions
Building stringsIf you’re performing repeated modifications on a string, for example in order to build up a lengthy string prior to displaying it or passing it to some other method or application, the String class can be very inefficient. For this kind of situation, another class, System.Text.StringBuilder, is more suitable because it has been designed exactly for this situation.
String
System.Text.StringBuilder
Formatting expressionsWe also take a closer look at those formatting expressions that have been used in the Console.WriteLine() method throughout the past few chapters. These formatting expressions are processed using a couple of useful interfaces, IFormatProvider and IFormattable. By implementing these interfaces on your own classes, you can actually define your own formatting sequences so that Console.WriteLine() and similar classes will display the values of your classes in whatever way you specify.
Console.WriteLine()
IFormatProvider
IFormattable
Regular expressions.NET also offers some very sophisticated classes that deal with situations in which you need to identify or extract substrings that satisfy certain fairly sophisticated criteria; for example, finding all occurrences within a string where a character or set of characters is repeated, finding all words that begin with s and contain at least one n, or strings that adhere to employee ID or Social Security number constructions. Although you can write methods to perform this kind of processing using the String class, such methods are cumbersome to write. Instead, you can use some classes from System.Text.RegularExpressions, which are designed specifically to perform this kind of processing.
A new feature of the CLR 2.0 was the introduction of generics. With CLR 1.0, creating a flexible class or method that should... more
A new feature of the CLR 2.0 was the introduction of generics. With CLR 1.0, creating a flexible class or method that should use classes that are not known at compile time must be based on the Object class. With the Object class, there’s no type safety during compile time. Casting is necessary. Also, using the Object class for value types has a performance impact.
CLR 2.0 (.NET 3.5 is based on the CLR 2.0) supports generics. With generics, the Object class is no longer necessary in such scenarios. Generic classes make use of generic types that are replaced with specific types as needed. This allows for type safety: the compiler complains if a specific type is not supported with the generic class.
Generics are a great feature, especially with collection classes. Most of the .NET 1.0 collection classes are based on the Object type. Starting with version 2.0 .NET offers collection classes that are implemented as generics.
Generics are not limited to classes; in this chapter, you also see generics with delegates, interfaces, and methods.
Generics overview
Creating generic classes
Features of generic classes
Generic interfaces
Generic methods
Generic delegates
Other generic framework types
In Chapter 5, “Arrays,” you read information about arrays and the interfaces implemented by the Array... more
In Chapter 5, “Arrays,” you read information about arrays and the interfaces implemented by the Array class. The size of arrays is fixed. If the number of elements is dynamic, you should use a collection class.
List<T>
ArrayList
This chapter shows you how to work with groups of objects. It takes a close look at these topics:
Collection interfaces and types
Lists
Queues
Stacks
Linked lists
Sorted lists
Dictionaries
Lookups
HashSets
Bit arrays
Performance
LINQ (Language Integrated Query) is the most important new feature of C# 3.0 and .NET 3.5. LINQ integrates query syntax inside... more
LINQ (Language Integrated Query) is the most important new feature of C# 3.0 and .NET 3.5. LINQ integrates query syntax inside the C# programming language and makes it possible to access different data sources with the same syntax. LINQ makes this possible by offering an abstraction layer.
This chapter gives you the core foundation of LINQ and the language extensions for C# 3.0 that make the new features possible. The topics of this chapter are:
Traditional queries across objects using List<T>
Extension methods
LINQ query
Standard query operators
Expression trees
LINQ providers
This chapter gives you the core foundation of LINQ. For using LINQ across the database you should read Chapter 27, “LINQ to SQL.” To query XML data read Chapter 29, “LINQ to XML,” after reading this chapter.
This chapter presents various aspects of memory management and memory access. Although the runtime takes much of the responsibility... more
This chapter presents various aspects of memory management and memory access. Although the runtime takes much of the responsibility for memory management away from the programmer, it is useful to understand how memory management works and important to know how to work with unmanaged resources efficiently.
A good understanding of memory management and knowledge of the pointer capabilities provided by C# will better enable you to integrate C# code with legacy code and perform efficient memory manipulation in performance-critical systems.
Specifically, this chapter discusses:
How the runtime allocates space on the stack and the heap
How garbage collection works
How to use destructors and the System.IDisposable interface to ensure unmanaged resources are released correctly
System.IDisposable
The syntax for using pointers in C#
How to use pointers to implement high-performance stack-based arrays
Reflection is a generic term that describes the ability to inspect and manipulate program elements at runtime. For example... more
Reflection is a generic term that describes the ability to inspect and manipulate program elements at runtime. For example, reflection allows you to:
Enumerate the members of a type
Instantiate a new object
Execute the members of an object
Find out information about a type
Find out information about an assembly
Inspect the custom attributes applied to a type
Create and compile a new assembly
This list represents a great deal of functionality and encompasses some of the most powerful and complex capabilities provided by the .NET Framework class library. Although this chapter does not have the space to cover all the capabilities of reflection, it focuses on those elements that you are likely to use most frequently.
This chapter is about:
Custom attributes, a mechanism that allows you to associate custom metadata with program elements. This metadata is created at compile time and embedded in an assembly.
Inspecting the metadata at runtime using some of the capabilities of reflection.
Some of the fundamental classes that enable reflection, including the System.Type and System.Reflection.Assembly classes, which provide the access points for much of what you can do with reflection.
System.Type
System.Reflection.Assembly
To demonstrate custom attributes and reflection, you develop an example based on a company that regularly ships upgrades of its software and wants to have details of these upgrades documented automatically. In the example, you define custom attributes that indicate the date when program elements were last modified, and what changes were made. You then use reflection to develop an application that looks for these attributes in an assembly, and can automatically display all the details about what upgrades have been made to the software since a given date.
Another example in this chapter considers an application that reads from or writes to a database and uses custom attributes as a way of marking which classes and properties correspond to which database tables and columns. By reading these attributes from the assembly at runtime, the program is able to automatically retrieve or write data to the appropriate location in the database, without requiring specific logic for each table or column.
Errors happen, and they are not always caused by the person who coded the application. Sometimes your application will generate... more
Errors happen, and they are not always caused by the person who coded the application. Sometimes your application will generate an error because of an action that was initiated by the end user of your application or it might be simply due to the environmental context in which your code is running. In any case, you should anticipate errors occurring in your applications and code accordingly.
The .NET Framework has enhanced the ways in which you deal with errors. C#’s mechanism for handling error conditions allows you to provide custom handling for each type of error condition as well as to separate code that identifies errors from the code that handles them.
The main topics covered in this chapter include:
Looking at the exception classes
Using try – catch – finally to capture exceptions
try
catch
finally
Creating user-defined exceptions
By the end of the chapter, you will have a good grasp on advanced exception handling in your C# applications.
No matter how good your coding is, your programs should have the ability to handle any possible errors that may occur. For example, in the middle of some complex processing your code may discover that it doesn’t have permission to read a file, or, while it is sending network requests, the network may go down. In such exceptional situations, it is not enough for a method to simply return an appropriate error codethere might be 15 or 20 nested method calls, so what you really want the program to do is jump back up through all those 15 or 20 calls in order to exit the task completely and take the appropriate counteractions. The C# language has very good facilities to handle this kind of situation, through the mechanism known as exception handling.
Error-handling facilities in Visual Basic 6 are very restricted and essentially limited to the On Error GoTo statement. If you are coming from a Visual Basic 6 background, you will find that C# exceptions open a completely new world of error handling in your programs. Java and C++ developers, however, will be familiar with the principle of exceptions because these languages handle errors in a similar way to C#. Developers using C++ are sometimes wary of exceptions because of possible C++ performance implications, but this is not the case in C#. Using exceptions in C# code in general does not adversely affect performance. Visual Basic developers will find that working with exceptions in C# is very similar to using exceptions in Visual Basic (except for the syntax differences).
On
Error
GoTo
At this point, you should be familiar with the C# language and almost ready to move on to the applied sections of the book... more
At this point, you should be familiar with the C# language and almost ready to move on to the applied sections of the book, which cover how to use C# to program a variety of applications. Before doing that, however, you need to examine how you can use Visual Studio and some of the features provided by the .NET environment to get the best from your programs.
This chapter explains what programming in the .NET environment means in practice. It covers Visual Studio, the main development environment in which you will write, compile, debug, and optimize your C# programs, and provides guidelines for writing good applications. Visual Studio is the main IDE used for everything from writing Web Forms and Windows Forms to XML Web services, and more. For more details on Windows Forms and how to write user interface code, see Chapter 31, “Windows Forms.” This chapter takes a strong look at the following:
Using Visual Studio 2008
Refactoring with Visual Studio
Visual Studio 2008’s multi-targeting capabilities
Working with the new technologies WPF, WCF, WF, and more.
This chapter also explores what it takes to build applications that are targeted at the .NET Framework 3.0 or 3.5. The types of applications provided ever since the .NET Framework 3.0 class library include the Windows Presentation Foundation (WPF), the Windows Communication Foundation (WCF), and the Windows Workflow Foundation (WF). Working with Visual Studio 2008 will provide you the ability to work with these new application types directly.
The development process does not end when the source code is compiled and testing is complete. At that stage, the job of... more
The development process does not end when the source code is compiled and testing is complete. At that stage, the job of getting the application into the user’s hands begins. Whether it’s an ASP.NET application, a smart client application, or an application built using the Compact Framework, the software must be deployed to a target environment. The .NET Framework has made deployment much easier than it was in the past. The pains of registering COM components and writing new hives to the registry are all gone.
This chapter looks at the options that are available for application deployment, both from an ASP.NET perspective and from the smart client perspective. The following topics are discussed:
Deployment requirements
Simple deployment scenarios
Windows Installer–based projects
ClickOnce
An assembly is the .NET term for a deployment and configuration unit. This chapter discusses exactly what assemblies are,... more
An assembly is the .NET term for a deployment and configuration unit. This chapter discusses exactly what assemblies are, how they can be applied, and why they are such a useful feature. In particular, this chapter covers the following topics:
Overview
Creating assemblies
Application domains
Shared assemblies
Versioning
The chapter begins with an overview of assemblies.
Chapter 14 covered errors and exception handling. Besides handling exceptional code, it might be really interesting to get... more
Chapter 14 covered errors and exception handling. Besides handling exceptional code, it might be really interesting to get some live information about your running application to find the reason for some issues that application might have during production, or to monitor resources needed to early adapt to higher user loads. This is where the namespace System.Diagnostics comes into play.
System.Diagnostics
The application doesn’t throw exceptions, but sometimes it doesn’t behave as expected. The application might be running well on most systems but might have a problem on a few. On the live system, you change the log behavior by changing a configuration value and get detailed live information about what’s going on in the application. This can be done with tracing.
If there are problems with applications, the system administrator needs to be informed. With the Event Viewer, the system administrator both interactively monitors problems with applications and gets informed about specific events that happen by adding subscriptions. The event-logging mechanism allows you to write information about the application.
To analyze resources needed from applications, monitor applications with specified time intervals, and plan for a different application distribution or extending of system resources, the system administrator uses the performance monitor. You can write live data of your application using performance counts.
This chapter explains these three facilities and demonstrates how you can use them from your applications:
Tracing
Event logging
Performance monitoring
There are several reasons for using threading. Suppose that you are making a network call from an application that might... more
There are several reasons for using threading. Suppose that you are making a network call from an application that might take some time. You don’t want to stall the user interface and just let the user wait until the response is returned from the server. The user could do some other actions in the meantime or even cancel the request that was sent to the server. Using threads can help.
For all activities that require a waitfor example, because of a file, database, or network access a new thread can be started to fulfill other tasks at the same time. Even if you have only processing-intensive tasks to do, threading can help. Multiple threads of a single process can run on different CPUs, or, nowadays, on different cores of a multiple-core CPU, at the same time.
You must be aware of some issues when running multiple threads, however. Because they can run during the same time, you can easily get into problems if the threads access the same data. You must implement synchronization mechanisms.
This chapter provides the foundation you will need when programming applications with multiple threads, including:
An overview of threading
Lightweight threading using delegates
Thread class
Thread pools
Threading issues
Synchronization techniques
Timers
COM apartments
Event-based asynchronous pattern
Security has several key aspects to consider. One is the user of the application. Is it really the user, or someone posing... more
Security has several key aspects to consider. One is the user of the application. Is it really the user, or someone posing as the user, who is accessing the application? How can this user be trusted? As you will see in this chapter, the user first needs to be authenticated, and then authorization occurs to verify if the user is allowed to use the requested resources.
What about data that is stored or sent across the network? Is it possible that someone accesses this data, for example, by using a network sniffer? Encryption of data is important here.
Yet another aspect is the application itself. How can you trust the application? What is the origin or evidence from the application? This is extremely important, for example, in a Web hosting scenario. A Web hosting provider does not allow its customers to access all resources from the system. Depending on the evidence of the assembly, different permissions for the application apply.
This chapter explores the features available in .NET to help you manage security, including how .NET protects you from malicious code, how to administer security policies, and how to access the security subsystem programmatically. The topics of this chapter are:
Authentication and authorization
Cryptography
Access control to resources
Code access security
Managing security policies
NASA’s Mars Climate Orbiter was lost on September 23, 1999, at a cost of $125 million because one engineering team used metric... more
NASA’s Mars Climate Orbiter was lost on September 23, 1999, at a cost of $125 million because one engineering team used metric units, while another one used inches for a key spacecraft operation. When writing applications for international distribution, different cultures and regions must be kept in mind.
Different cultures have diverging calendars and use different number and date formats. Also, sorting strings may lead to various results because the order of A–Z is defined differently based on the culture. To make applications fit for global markets, you have to globalize and localize them.
Globalization is about internationalizing applications: preparing applications for international markets. With globalization, the application supports number and date formats that vary depending on the culture, different calendars, and so on. Localization is about translating applications for specific cultures. For translations of strings, you can use resources.
.NET supports globalization and localization of Windows and Web applications. To globalize an application, you can use classes from the namespace System.Globalization; to localize an application, you can use resources that are supported by the namespace System.Resources.
System.Globalization
System.Resources
This chapter covers the globalization and localization of .NET applications; more specifically, it discusses the following:
Using classes that represent cultures and regions
Globalization of applications
Localization of applications
All or nothingthis is the main characteristic of a transaction. When writing a few records, either all are written, or everything... more
All or nothingthis is the main characteristic of a transaction. When writing a few records, either all are written, or everything will be undone. If there is even one failure when writing one record, all the other things that are done within the transaction will be rolled back.
Transactions are commonly used with databases, but with classes from the namespace System.Transactions, you can also perform transactions on volatile or in-memory based objects such as a list of objects. With a list that supports transactions, if an object is added or removed and the transaction fails, the list action is automatically undone. Writing to a memory-based list can be done in the same transaction as writing to a database.
System.Transactions
In Windows Vista, the file system and registry also get transactional support. Writing a file and making changes within the registry supports transactions.
In this chapter, the following topics are covered:
Overview of transaction phases and ACID properties
Traditional transactions
Committable transactions
Transaction promotions
Dependent transactions
Ambient transactions
Transaction isolation level
Custom resource managers
Transactions with Windows Vista and Windows Server 2008
Windows Services are programs that can be started automatically at boot time without the need for anyone to log on to the... more
Windows Services are programs that can be started automatically at boot time without the need for anyone to log on to the machine.
In this chapter, you learn:
The architecture of Windows Services, including the functionality of a service program, a service control program, and a service configuration program
How to implement a Windows Service with the classes found in the System.ServiceProcess namespace
System.ServiceProcess
Installation programs to configure the Windows Service in the registry
How to write a program to control the Windows Service using the ServiceController class
ServiceController
How to troubleshoot Windows Service programs
How to react to power events from the operating system
The first section explains the architecture of Windows Services. You can download the code for this chapter from the Wrox Web site at www.wrox.com.
www.wrox.com
If you have Windows programs written prior to .NET, you probably don’t have the time and resources to rewrite everything... more
If you have Windows programs written prior to .NET, you probably don’t have the time and resources to rewrite everything for .NET. Sometimes rewriting code is useful to do some refactoring, rethinking the application architecture. A rewrite can also help with productivity in the long-term when adding new features is easier to do with the new technology. However, there should not be a reason to rewrite old code just because a new technology is available. You might have thousands of lines of existing, running code, which would require too much effort to rewrite just to move it into the managed environment.
The same applies to Microsoft. With the namespace System.DirectoryServices, Microsoft hasn’t rewritten the COM objects accessing the hierarchical data store; the classes inside this namespace are wrappers accessing the ADSI COM objects instead. The same thing happens with System.Data.OleDb, where the OLE DB providers that are used by classes from this namespace do have quite complex COM interfaces.
System.DirectoryServices
System.Data.OleDb
The same issue may apply for your own solutions. If you have existing COM objects that should be used from .NET applications, or the other way around if you want to write .NET components that should be used in old COM clients, this chapter will be a starter for using COM interoperability.
If you don’t have existing COM components you want to integrate with your application, or old COM clients that should use some .NET components, you can skip this chapter.
COM and .NET technologies
Using COM objects from within .NET applications
Using .NET components from within COM clients
Platform invoke for invoking native methods
Like all other chapters, you can download the sample code for this chapter from the Wrox Web site at www.wrox.com.
This chapter examines how to perform tasks involving reading from and writing to files and the system registry in C... more
This chapter examines how to perform tasks involving reading from and writing to files and the system registry in C#. In particular, it covers the following:
Exploring the directory structure, finding out what files and folders are present, and checking their properties
Moving, copying, and deleting files and folders
Reading and writing text in files
Reading and writing keys in the registry
Reading and writing to isolated storage
Microsoft has provided very intuitive object models covering these areas, and in this chapter, you learn how to use .NET base classes to perform the listed tasks. In the case of file system operations, the relevant classes are almost all found in the System.IO namespace, whereas registry operations are dealt with by classes in the Microsoft.Win32 namespace.
System.IO
Microsoft.Win32
The .NET base classes also include a number of classes and interfaces in the System.Runtime.Serialization namespace concerned with serializationthat is, the process of converting data (for example, the contents of a document) into a stream of bytes for storage. This chapter does not focus on these classes; it focuses on the classes that give you direct access to files.
System.Runtime.Serialization
Note that security is particularly important when modifying files or registry entries. The whole area of security is covered separately in Chapter 20, “Security.” In this chapter, however, we assume that you have sufficient access rights to run all of the examples that modify files or registry entries, which should be the case if you are running from an account with administrator privileges.
This chapter discusses how to access data from your C# programs using ADO.NET. The following details are covered:... more
This chapter discusses how to access data from your C# programs using ADO.NET. The following details are covered:
Connecting to the databaseYou learn how to use the SqlConnection and OleDbConnection classes to connect to and disconnect from the database.
SqlConnection
OleDbConnection
Executing commandsADO.NET has command objects that can execute SQL commands or issue a call to a stored procedure with optional return values. You learn the various command object options and see how commands can be used for each of the options presented by the Sql and OleDB classes.
Sql
OleDB
Stored proceduresYou learn how to call stored procedures with command objects and how the results of those stored procedures can be integrated into the data cached on the client.
The ADO.NET object modelThis is significantly different from the objects available with ADO. The DataSet, DataTable, DataRow, and DataColumn classes are discussed as well as the relationships between tables and constraints that are part of DataSet. The class hierarchy has changed significantly with version 2 of the .NET Framework, and some of these changes are also described.
DataSet
DataTable
DataRow
DataColumn
Using XML and XML schemasYou examine the XML framework on which ADO.NET is built.
Microsoft has also added support for Language Integrated Query (LINQ) in C# for the 3.0 release. Although this topic largely supersedes the information in this chapter, it is included here for completeness. See Chapters 28, “Manipulating XML,” 29, “LINQ to XML,” and 31, “Windows Forms,” for some details on new data access capabilities in .NET.
As is the case with the other chapters, you can download the code for the examples used in this chapter from the Wrox Web site at www.wrox.com. The chapter begins with a brief tour of ADO.NET.
Probably the biggest and most exciting addition to the .NET Framework 3.5 is the addition of the .NET Language Integrated... more
Probably the biggest and most exciting addition to the .NET Framework 3.5 is the addition of the .NET Language Integrated Query Framework (LINQ) into C# 2008. Basically, what LINQ provides is a lightweight façade over programmatic data integration. This is such a big deal because data is king.
Pretty much every application deals with data in some manner, whether that data comes from memory (in-memory data), databases, XML files, text files, or something else. Many developers find it very difficult to move from the strongly typed object-oriented world of C# to the data tier where objects are second-class citizens. The transition from the one world to the next was a kludge at best and was full of error-prone actions.
In C#, programming with objects means a wonderful strongly typed ability to work with code. You can navigate very easily through the namespaces, work with a debugger in the Visual Studio IDE, and more. However, when you have to access data, you will notice that things are dramatically different.
You end up in a world that is not strongly typed, where debugging is a pain or even non-existent, and you end up spending most of the time sending strings to the database as commands. As a developer, you also have to be aware of the underlying data and how it is structured or how all the data points relate.
Microsoft has provided LINQ as a lightweight façade that provides a strongly typed interface to the underlying data stores. LINQ provides the means for developers to stay within the coding environment they are used to and access the underlying data as objects that work with the IDE, IntelliSense, and even debugging.
With LINQ, the queries that you create now become first-class citizens within the .NET Framework alongside everything else you are used to. When you work with queries for the data store you are working with, you will quickly realize that they now work and behave as if they are types in the system. This means that you can now use any .NET-compliant language and query the underlying data store as you never have before.
Chapter 11, “Language Integrated Query,” provides an introduction to LINQ.
Looking at the figure, you can see that there are different types of LINQ capabilities depending on the underlying data that you are going to be working with in your application. From the list, you will find the following LINQ technologies:
LINQ to Objects
LINQ to DataSets
LINQ to SQL
LINQ to Entities
LINQ to XML
As a developer, you are given class libraries that provide objects that, using LINQ, can be queried as any other data store can. Objects are really nothing more than data that is stored in memory. In fact, your objects themselves might be querying data. This is where LINQ to Objects comes into play.
LINQ to SQL (the focus of this chapter), LINQ to Entities, and LINQ to DataSets provide the means to query relational data. Using LINQ, you can query directly against your database and even against the stored procedures that your database exposes. The last item from the diagram is the ability to query against your XML using LINQ to XML (this topic is covered in Chapter 29). The big thing that makes LINQ exciting is that it matters very little what you are querying against, because your queries will be quite similar.
This chapter looks at the following:
Working with LINQ to SQL along with Visual Studio 2008
Looking at how LINQ to SQL objects map to database entities
Building LINQ to SQL operations without the O/R Designer
Using the O/R Designer with custom objects
Querying the SQL Server database using LINQ
Stored procedures and LINQ to SQL
XML plays a significant role in the .NET Framework. Not only does the .NET Framework allow you to use XML in your application... more
XML plays a significant role in the .NET Framework. Not only does the .NET Framework allow you to use XML in your application, but the .NET Framework itself uses XML for configuration files and source code documentation, as do SOAP, Web services, and ADO.NET, to name just a few.
To accommodate this extensive use of XML, the .NET Framework includes the System.Xml namespace. This namespace is loaded with classes that can be used for the processing of XML, and many of these classes are discussed in this chapter.
System.Xml
This chapter discusses how to use the XmlDocument class, which is the implementation of the Document Object Model (DOM), as well as what .NET offers as a replacement for SAX (the XmlReader and XmlWriter classes). It also discusses the class implementations of XPath and XSLT and demonstrates how XML and ADO.NET work together, as well as how easy it is to transform one to the other. You also learn how you can serialize your objects to XML and create an object from (or deserialize) an XML document using classes in the System.Xml.Serialization namespace. More to the point, you learn how you can incorporate XML into your C# applications.
XmlDocument
XmlReader
XmlWriter
System.Xml.Serialization
You should note that the XML namespace allows you to get similar results in a number of different ways. It is impossible to include all these variations in one chapter, so while exploring one possible way of doing things we’ll try our best to mention alternative routes that will yield the same or similar results.
Because it's beyond the scope of this book to teach you XML from scratch, we assume that you are already somewhat familiar with XML technology. For example, you should be familiar with elements, attributes, and nodes, and you should also know what we mean when we refer to a well-formed document. You should also be familiar with SAX and DOM. If you want to find out more about XML, Wrox’s Beginning XML (Wiley Publishing, Inc., ISBN 0-7645-7077-3) is a great place to start.
This chapter covers the following:
XML standards
XmlReader and XmlWriter
XPathDocument
XmlNavigator
The discussion begins with a brief overview of the current status of XML standards.
As stated in Chapter 27, “LINQ to SQL,” probably the biggest and most exciting addition to the .NET Framework 3.5 is the... more
As stated in Chapter 27, “LINQ to SQL,” probably the biggest and most exciting addition to the .NET Framework 3.5 is the addition of the .NET Language Integrated Query framework (LINQ) into C# 2008. LINQ comes in many flavors depending on the final data store that you are working with in querying your data. Chapter 27 took a look at using LINQ to SQL to query SQL Server databases; this chapter takes a quick look at using LINQ to query your XML data sources instead.
You read about the following in this chapter:
What LINQ to XML brings to the table
The new objects available in the System.Xml.Linq namespace
System.Xml.Linq
How to query your XML documents using LINQ
Moving around your XML documents using LINQ
Using LINQ to SQL and LINQ to XML together
Extensible Markup Language (XML) is now in widespread use. Many applications on the Internet or residing on individual computers use some form of XML to run or manage the processes of an application. Earlier books about XML commented that XML was to be the “next big thing.” Now, it is “the big thing.” In fact, there really isn’t anything bigger.
Microsoft has been working for years to make using XML in the .NET world as easy as possible. You can’t help but notice the additional capability and the enhancements to XML usage introduced in each new version of the .NET Framework. In fact, Bill Gates highlighted Microsoft’s faith in XML in his keynote address at the Microsoft Professional Developers Conference in 2005 in Los Angeles. He stated that XML is being pushed deeper and deeper into the Windows core each year. If you look around the .NET Framework, you will probably agree.
For this reason, this chapter focuses on using LINQ to XML to query your XML documents. Figure 29-1 shows LINQ’s place in querying XML data.
Much of what you learned in the chapter on using LINQ to SQL can be applied here when working with LINQ to XML.
SQL Server 2005 was the first version of this database product to host the .NET runtime. In fact, it was the first new version... more
SQL Server 2005 was the first version of this database product to host the .NET runtime. In fact, it was the first new version of Microsoft’s SQL Server product in nearly six years. It allows running .NET assemblies in the SQL Server process. Furthermore, it enables you to create stored procedures, functions, and data types with .NET programming languages such as C# and Visual Basic.
In this chapter, you learn about the following:
Hosting the .NET runtime with SQL Server
Classes from the namespace System.Data.SqlServer
System.Data.SqlServer
Creating user-defined types
Creating user-defined aggregates
Stored procedures
User-defined functions
Triggers
XML data types
This chapter requires SQL Server 2005 or a later version of this database product.
SQL Server has many features that are not directly associated with the CLR, such as many T-SQL improvements, but they are not covered in this book. To get more information about these features you can read Wrox’s SQL Server 2005 Express Edition Starter Kit (Wiley Publishing, Inc., ISBN 0-7645-8923-7).
The samples in this chapter make use of a ProCSharp database that you can download with the code samples, and the AdventureWorks database. The AdventureWorks database is a sample database from Microsoft that you can install as an optional component with SQL Server.
Web-based applications have become very popular over the past several years. The ability to have all of your application... more
Web-based applications have become very popular over the past several years. The ability to have all of your application logic reside on a centralized server is very appealing from an administrator’s viewpoint. Deploying client-based software can be very difficult, especially COM-based client software. The downside of Web-based applications is that they cannot provide that rich user experience. The .NET Framework has given developers the ability to create rich, smart client applications and eliminate the deployment problems and “DLL Hell” that existed before. Whether Windows Forms or Windows Presentation Foundation (see Chapter 34, "Windows Presentation Foundation") is chosen, client applications are no longer difficult to develop or deploy.
Windows Forms had quite an impact on Windows development. Now when an application is in the initial design phase, the decision between building a Web-based application or a client application has become a little more difficult. Windows client applications can be developed quickly and efficiently, and they can provide users with the rich experience that they expect.
Windows Forms will seem somewhat familiar if you are a Visual Basic developer. You create new forms (also known as windows or dialogs) in much the same way that you drag and drop controls from a toolbox onto the Form Designer. However, if your background is in the classic C style of Windows programming where you create the message pump and monitor messages, or if you’re an MFC programmer, you will find that you’re able to get to the lower-level internals if you need to. You can override the wndproc and catch those messages, but you might be surprised that you really won’t need to very often.
This chapter looks at the following aspects of Windows Forms:
The Form class
Form
The class hierarchy of Windows Forms
The controls and components that are part of the System.Windows.Forms namespace
System.Windows.Forms
Menus and toolbars
Creating controls
Creating user controls
This chapter builds on the content of Chapter 26, “Data Access,” which covered various ways of selecting and changing data... more
This chapter builds on the content of Chapter 26, “Data Access,” which covered various ways of selecting and changing data, by showing you how to present data to the user by binding to various Windows controls. More specifically, this chapter discusses:
Displaying data using the DataGridView control
DataGridView
The .NET data-binding capabilities and how they work
How to use the Server Explorer to create a connection and generate a DataSet class (all without writing a line of code)
How to use hit testing and reflection on rows in the DataGrid
DataGrid
You can download the source code for the examples in this chapter from the Wrox Web site at www.wrox.com.
This is the third of the eight chapters that deal with user interaction and the .NET Framework. Chapter 31, “Windows Forms... more
This is the third of the eight chapters that deal with user interaction and the .NET Framework. Chapter 31, “Windows Forms,” focused on how to display a dialog box or SDI or MDI window, and how to place various controls such as buttons, text boxes, and list boxes. Chapter 32, “Data Binding,” looked at how to work with data in Windows Forms using a number of the Windows Forms controls that work with the disparate data sources that you might encounter.
Although these standard controls are powerful and, by themselves, quite adequate for the complete user interface for many applications, some situations require more flexibility. For example, you might want to draw text in a given font in a precise position in a window, or display images without using a picture box control, or draw simple shapes or other graphics. None of this can be done with the controls discussed in Chapter 31. To display that kind of output, the application must instruct the operating system what to display and where in its window to display it.
Therefore, this chapter shows you how to draw a variety of items including:
Principles of drawing
Lines and simple shapes
BMP images and other image files
Text
Dealing with printing
In the process, you will need to use a variety of helper objects, including pens (to define the characteristics of lines), brushes (to define how areas are filled in), and fonts (to define the shape of the characters of text). This chapter also goes into some detail on how devices interpret and display different colors.
The chapter starts, however, by discussing a technology called GDI+. GDI+ consists of the set of .NET base classes that are available to control custom drawing on the screen. These classes arrange for the appropriate instructions to be sent to graphics device drivers to ensure the correct output is placed on the screen (or printed to a hard copy).
Windows Presentation Foundation (WPF) is one of the major extensions of .NET Framework 3.0. WPF is a new library to create... more
Windows Presentation Foundation (WPF) is one of the major extensions of .NET Framework 3.0. WPF is a new library to create the UI for smart client applications. While the Windows Forms controls are native Windows controls that use Window handles that are based on screen pixels, WPF is based on DirectX. The application does not use Window handles. It is easy to resize the UI, and it supports sound and video.
The main topics of this chapter are, as follows:
An overview of WPF
Shapes as the base drawing elements
Scaling, rotating, and skewing with transformations
Different kind of brushes to fill elements
WPF controls and their features
How to define a layout with WPF panels
The WPF event-handling mechanism
Styles, templates, and resources
In the previous chapter you read about some of the core functionality of WPF. In this chapter programming with WPF continues... more
In the previous chapter you read about some of the core functionality of WPF. In this chapter programming with WPF continues. Here you read about some important aspects for creating complete applications such as data binding and command handling, and you also get an introduction to animations and 3-D programming.
Data binding
Commands
Animations
3-D
Windows Forms integration
Add-ins allow you to add functionality to an application at a later time. You can create a hosting application that gains... more
Add-ins allow you to add functionality to an application at a later time. You can create a hosting application that gains more and more functionality over timefunctionality that might be written by your developer team but also different vendors can extend your application by creating add-ins.
Today, add-ins are used with many different applications, such as Internet Explorer and Visual Studio. Internet Explorer is a hosting application that offers an add-in framework that is used by many companies to offer extensions when viewing Web pages. The Shockwave Flash Object allows you to view Web pages with Flash content. The Google toolbar offers specific Google features that can be accessed quickly from Internet Explorer. Visual Studio also has an add-in model that allows you to extend Visual Studio with different levels of extensions.
For your custom applications it has always been possible to create an add-in model to dynamically load and use functionality from assemblies. With an add-in model many issues need to be thought about. How can new assemblies be detected? How can versioning issues be resolved? Can the add-in change the stability of the hosting application?
The .NET Framework 3.5 offers a framework for hosting and creating add-ins with the assembly System.AddIn. This framework is also known by the name Managed AddIn Framework (MAF).
Add-ins are also known by different terms such as "add-on" or "plug-in."
Topics covered in this chapter are
System.AddIn architecture
Creating a simple add-in
If you are new to the world of C# and .NET, you might wonder why a chapter on ASP.NET has been included in this book. It’s... more
If you are new to the world of C# and .NET, you might wonder why a chapter on ASP.NET has been included in this book. It’s a whole new language, right? Well, not really. In fact, as you will see, you can use C# to create ASP.NET pages.
ASP.NET is part of the .NET Framework and is a technology that allows for the dynamic creation of documents on a Web server when they are requested via HTTP. This mostly means HTML and XHTML documents, although it is equally possible to create XML documents, CSS files, images, PDF documents, or anything else that supports MIME types.
In some ways, ASP.NET is similar to many other technologiessuch as PHP, ASP, or ColdFusion. There is, however, one key difference: ASP.NET, as its name suggests, has been designed to be fully integrated with the .NET Framework, part of which includes support for C#.
Perhaps you are familiar with Active Server Pages (ASP) technology, which enables you to create dynamic content. If you are, you will probably know that programming in this technology used scripting languages such as VBScript or JScript. The result was not always perfect, at least not for those of us used to “proper,” compiled programming languages, and it certainly resulted in a loss of performance.
One major difference related to the use of more advanced programming languages is the provision of a complete server-side object model for use at runtime. ASP.NET provides access to all of the controls on a page as objects, in a rich environment. On the server side, you also have access to other .NET classes, allowing for the integration of many useful services. Controls used on a page expose a lot of functionality; in fact, you can do almost as much as with Windows Forms classes, which provide plenty of flexibility. For this reason, ASP.NET pages that generate HTML content are often called Web Forms.
This chapter takes a more detailed look at ASP.NET, including how it works, what you can do with it, and how C# fits in. The following is a brief outline of what is covered:
An introduction to ASP.NET
How to create ASP.NET Web Forms with server controls
How to bind data to ASP.NET controls with ADO.NET
Application configuration
Sometimes the tools available for Web development, however powerful, don’t quite match up with your requirements for a specific... more
Sometimes the tools available for Web development, however powerful, don’t quite match up with your requirements for a specific project. Perhaps a given control doesn’t quite work as you would like it to, or perhaps one section of code, intended for reuse on several pages, is too complex in the hands of multiple developers. In such cases, there is a strong argument for building your own controls. Such controls can, at their simplest, wrap multiple existing controls together, perhaps with additional properties specifying layout. They can also be completely unlike any existing control. Using a control you have built yourself can be as simple as using any other control in ASP.NET (if you have written it well), which can certainly ease Web site coding.
In the first part of this chapter, you examine the options available to control developers, and assemble some simple user controls of your own. You also look at the basics of more advanced control construction, although you won’t see these in any great depth; whole books are devoted to the subject.
Next, you look at master pages, a technique new to ASP.NET 2.0 that enables you to provide templates for your Web sites. Using master pages, you can implement complex layouts on Web pages throughout a Web site with a great deal of code reuse. You also see how you can use the navigation Web server controls in combination with a master page to provide consistent navigation across a Web site.
Site navigation can be made user-specific, such that only certain users (those that are registered with the site, or site administrators, say) can access certain sections. You also look at site security and how to log in to Web sitessomething that is made extremely easy via the login Web server controls.
After that, you look at some more advanced styling techniques, namely, providing and choosing themes for Web sites, which separate the presentation of your Web pages from their functionality. You can supply alternative cascading style sheets for your sites, as well as different skins for Web server controls.
Finally, you will see how to use Web Parts to enable your users to dynamically personalize Web pages by positioning and customizing controls on a page.
To summarize, in this chapter you look at:
User and custom controls
Master pages
Site navigation
Security
Themes
Web Parts
Throughout this chapter, you will refer to one large example application that includes all the techniques that you have seen in this and the previous chapter. This application, PCSDemoSite, is available in the downloadable code for this chapter. It is a little too large to include all the code here, but you don’t need to have it running in front of you to learn about the techniques it illustrates. The relevant sections of code are examined as and when necessary, and the additional code (mostly dummy content or simple code you have already seen) is left for you to examine at your convenience.
PCSDemoSite
Web application programming is subject to continuous change and improvement. In the previous two chapters, you learned how... more
Web application programming is subject to continuous change and improvement. In the previous two chapters, you learned how to use ASP.NET to create fully functional Web applications, and you may think that you have seen all the tools that you need to create your own Web applications. However, if you spend much time on the Internet, you may have noticed that more recent Web sites are significantly better, in terms of usability, than older Web sites. Many of today’s best Web sites provide rich user interfaces that feel almost as responsive as Windows applications. They achieve this by using client-side processing, primarily through JavaScript code, and increasingly through a technology known as Ajax.
This change of direction is possible because the browsers that clients use to browse Web sites, and the computers that clients use to run browsers, have become more powerful. The current generation of Web browsers, such as Internet Explorer 7 and Firefox, also support a wide variety of standards. These standards, which include JavaScript, enable Web applications to provide functionality far in advance of what was previously possible using plain HTML. You have already seen some of this in previous chaptersfor example the use of cascading style sheets (CSS) to style Web applications.
Ajaxas you will discover shortlyis not a new technology. Rather, it is a combination of standards that makes it possible to realize the rich potential functionality of current Web browsers.
Perhaps the key defining feature of Ajax-enabled Web applications is the ability for the Web browser to communicate with the Web server in out-of-band operations; this is known as asynchronous, or partial-page, postbacks. In practice, this means that the user can interact with server-side functionality and data without needing a full-page refresh. For example, when a link is followed to move to the second page of data in a table, Ajax makes it possible to refresh just the table’s content rather than the entire Web page. This means that there is less traffic required across the Internet, which leads to a more responsive Web application. You will see this example in practice later in this chapter, as well as many more examples that illustrate the power of Ajax in Web applications.
You will be using Microsoft’s implementation of Ajax in the code in this chapter, known as ASP.NET AJAX. This implementation takes the Ajax model and applies it to the ASP.NET framework. ASP.NET AJAX provides a number of server controls and client-side techniques that are specifically targeted at ASP.NET developers and that enable you to add Ajax functionality to your Web applications with surprisingly little effort.
This chapter is organized as follows:
First, you learn more about Ajax and the technologies that make Ajax possible.
Next, you learn about ASP.NET AJAX and its component parts, as well as the functionality that ASP.NET AJAX offers.
Last, you see how to use ASP.NET AJAX in your Web applications, by using both server-side and client-side code. This coverage forms the largest part of this chapter.
Visual Studio Tools for Office (VSTO) is a technology that enables you to customize and extend Microsoft Office applications... more
Visual Studio Tools for Office (VSTO) is a technology that enables you to customize and extend Microsoft Office applications and documents by using the .NET Framework. It also includes tools that you can use to make this customization easier in Visual Studiofor example, a visual designer for office ribbon controls.
VSTO is the latest in a long line of products that Microsoft has released to allow the customization of Office applications. The object model that you use to access Office applications has evolved over time. If you have used it in the past, then parts of it will be familiar to you. If you have programmed VBA add-ins for Office applications, then you will be well prepared for the techniques discussed in this chapter (and, as you will see, VSTO is capable of interoperability with VBA). However, the classes that VSTO makes available so that you can interact with Office through the Office Primary Interop Assemblies (PIAs) have been extended beyond the Office object model. For example, the VSTO classes include .NET data binding functionality.
Up until Visual Studio 2008, VSTO was a separate download that you could obtain if you wanted to develop Office solutions. With Visual Studio 2008, VSTO is integrated with the VS IDE. This version of VSTO, which is also known as VSTO 3, includes full support for Office 2007 and has many new features. This includes the ability to interact with Word content controls, the visual ribbon designer mentioned previously, VBA integration, and more.
This chapter does not assume any prior knowledge of VSTO or its predecessors. In this chapter, you learn the following:
What types of projects you can create with VSTO and what capabilities you can include in these projects
Fundamental techniques that apply to all types of VSTO solutions
How to build VSTO solutions with a custom UI, VBA interoperability, and ClickOnce deployment
Chapters 37 through 39 discuss how you can use C# to write powerful, efficient, and dynamic Web pages using ASP.NET. For... more
Chapters 37 through 39 discuss how you can use C# to write powerful, efficient, and dynamic Web pages using ASP.NET. For the most part, the clients accessing ASP.NET pages will be users running Internet Explorer or other Web browsers such as Opera or Firefox. However, you might want to add Web-browsing features to your own application, or you might need your applications to programmatically obtain information from a Web site. In this latter case, it is usually better for the site to implement a Web service. However, when you are accessing public Internet sites, you might not have any control over how the site is implemented.
This chapter covers facilities provided through the .NET base classes for using various network protocols, particularly HTTP and TCP, to access networks and the Internet as a client. In particular, this chapter covers:
Downloading files from the World Wide Web
Using the Web Browser control in a Windows Forms application
Manipulating IP addresses and performing DNS lookups
Socket programming with TCP, UDP, and socket classes
This chapter covers some of the lower-level means of getting at these protocols through the .NET Framework. You will also find other means of communicating via these items using technologies, such as the Windows Communication Foundation (WCF), which is covered in the next chapter.
The two namespaces of most interest for networking are System.Net and System.Net.Sockets. The System.Net namespace is generally concerned with higher-level operations, for example, downloading and uploading files, and making Web requests using HTTP and other protocols, whereas System.Net.Sockets contains classes to perform lower-level operations. You will find these classes useful when you want to work directly with sockets or protocols, such as TCP/IP. The methods in these classes closely mimic the Windows socket (Winsock) API functions derived from the Berkeley sockets interface. You will also find that some of the objects that this chapter works with are found in the System.IO namespace.
System.Net
System.Net.Sockets
This chapter takes a fairly practical approach, mixing examples with a discussion of the relevant theory and networking concepts as appropriate. This chapter is not a guide to computer networking but an introduction to using the .NET Framework for network communication.
You will learn how to use the WebBrowser control in a Windows Forms environment. You will also learn how the WebBrowser control can make some specific Internet access tasks easier to accomplish.
WebBrowser
However, the chapter starts with the simplest case, sending a request to a server and storing the information sent back in the response. (As with other chapters, you can download the sample code for this chapter from the Wrox Web site at www.wrox.com.)
Previous to .NET 3.0, several communication technologies were required in a single enterprise solution. For platform-independent... more
Previous to .NET 3.0, several communication technologies were required in a single enterprise solution. For platform-independent communication, ASP.NET Web services were used. For more advanced Web services, technologies such as reliability, platform-independent security, and atomic transactions, Web Services Enhancements added a complexity layer to ASP.NET Web services. If the communication needed to be faster, and both the client and service were .NET applications, .NET Remoting was the technology of choice. .NET Enterprise Services with its automatic transaction support, by default, was using the DCOM protocol that was even faster than .NET Remoting. DCOM was also the only protocol to allow passing transactions. All of these technologies have different programming models that require many skills from the developer.
.NET Framework 3.0 introduced a new communication technology that includes all the features from the predecessors and combines them into one programming model: Windows Communication Foundation (WCF).
In particular, this chapter discusses the following topics:
WCF overview
A simple service and client
Contracts
Service implementation
Binding
Hosting
Clients
Duplex communication