MULTIMEDIA

WCF Services : Delegates and Data Contracts

9/23/2010 3:01:15 PM

All delegate definitions are compiled into serializable classes, so in theory your data contract types can contain delegates as member variables:

[DataContract]
class MyDataContract
{
[DataMember]
public EventHandler MyEvent;
}

Or even as events (note the use of the field qualifier):

[DataContract]
class MyDataContract
{
[field:DataMember]
public event EventHandler MyEvent;
}

In practice, however, when the data contract refers to a custom delegate, the imported data contract will contain an invalid delegate definition. While you could manually fix that definition, the bigger problem is that when you serialize an object that has a delegate member variable, the internal invocation list of the delegates is serialized, too. In most cases, this is not the desired effect with services and clients, because the exact structure of the list is local to the client or the service and should not be shared across the service boundary. In addition, there are no guarantees that the target objects in the internal list are serializable or are valid data contracts. Consequently, sometimes the serialization will work, and sometimes it will fail.

The simplest way to avoid this pitfall is not to apply the DataMember attribute on delegates. If the data contract is a serializable type, you need to explicitly exclude the delegate from the data contract:

[Serializable]
public class MyDataContract
{
[NonSerialized]
public EventHandler MyEvent;
}
Other  
 
Top 10
Windows Server 2003 : Building a Nameserver (part 3) - Configuring a Secondary Nameserver, Upgrading a Secondary Nameserver to Primary, Controlling the Zone Transfer Process
Windows Server 2003 : Building a Nameserver (part 2) - Creating and Editing CNAME Records, Creating and Editing MX Records, Generating a Reverse Lookup Zone, Creating and Editing PTR Records
Windows Server 2003 : Building a Nameserver (part 1) - Enabling Incremental Transfers, Entering A Records into a Zone, Entering and Editing SOA Records
Windows Server 2003 : Domain Name System - Zones Versus Domains, Resource Records
The Connected BlackBerry : Connection Paths (part 2) - Mobile Data Service Connection, Wi-Fi Connection
The Connected BlackBerry : Connection Paths (part 1) - Wireless Application Protocol Connection, Direct TCP Internet Connection, BlackBerry Internet Service Connection
Simulating SOA : Interpreting the Results of a Simulation
Simulating SOA : BPEL Simulation Examples
Oracle Database 11g : Database Fundamentals - Work with Object and System Privileges, Introduce Yourself to the Grid
Oracle Database 11g : Database Fundamentals - Become Familiar with Other Important Items in Oracle Database 11g
Most View
HP Envy 3D Ivy Bridge Version
Windows Phone 7 Development : Handling Data Connection Issues with Rx.NET
Visual Studio 2010 : Managing Extensions with the Extension Manager, Managing Add-Ins with the Add-In Manager
Advice Centre by Photography Experts (Part 5) - Getting to grips with bracketing
Windows 7 : Exploring and Searching Your Computer - Exploring Your Documents
SQL Server System and Database Administration : System Databases
Unifying: Greatest Challenge
Logitech S715i iPhone - iPod Dock
IIS 7.0 : Managing Administration Extensions
Smart TV : The Next Evolution…
Using MySQL Enterprise (part 2) - Monitoring
SQL Server 2008 : Managing Query Performance - Forcing a Specific Execution Plan
The Revolution Of Visual Resolution (Part 6) - AOC d2357Ph, Samsung S27A950D, ViewSonic V3D245
Tokina AT-X 17-35mm f4 Pro FX (Part 1)
Windows Server 2008 : DHCP/WINS/Domain Controllers - Exploring Advanced DHCP Concepts
System Center Configuration Manager 2007 : Planning for Internet-Based Clients
Standarlize Desktop Computer (Part 2)
LINQ to Objects : How to Get the Index Position of the Results, How to Remove Duplicate Results
Windows Server 2003 : Planning for Disaster
Windows Server 2008 and Windows Vista : Getting Around in the GPMC (part 2) - Selecting Domain Controllers for Administration of GPOs