What's the point of a DataContract in WCF?

VS.net creates a template when you create a WCF project.

It adds a class to the iService1.cs file:

// Use a data contract as illustrated in the sample below to
// add composite types to service operations.
public class CompositeType
    bool boolValue = true;
    string stringValue = "Hello ";

    public bool BoolValue
        get { return boolValue; }
        set { boolValue = value; }

    public string StringValue
        get { return stringValue; }
        set { stringValue = value; }

Since a WCF service can return any user defined class, why use a DataContract and CompositeType class?

I can return something like:

MyUserCollection GetUsers();

What am I missing?


The DataContract is just a formal definition of a type that can be understood on both sides of the service boundary.

If you return, as in your example, a "MyUserCollection" object, the consumers of your service will need to reference the innards of your service/system, which is a violation of the SOA tenet of explicit boundaries. By using a DataContract, you are publishing the structure of your return types in a loosely-coupled way.

Need Your Help

runAllManagedModulesForAllRequests = “true” killing windows authentication in IIS7

asp.net .net iis-7 web-config windows-authentication

We have windows authentication set up on our staging server for our clients to help prevent google from indexing our staging URLs. We recently found out that one of our sites is seemingly ignoring...

Custom support for __attribute__((format))

c++ c gcc printf clang

Both GCC and Clang have a support to make compile-time checks on variable argument functions like printf. These compilers accept syntax like:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.