Namespace for [DataContract]

C#WcfDatacontract

C# Problem Overview


I can't find the namespace to use for [DataContract] and [DataMember] elements. According to what I've found, it seems that adding the following should be enough, but in my case it is not.

using System;
using System.Runtime.Serialization;

Here is a snippet of my code:

using System;
using System.Runtime.Serialization;

namespace MyNamespace {

    [DataContract]
    public class Tuple<T1, T2> {
            // A custom implementation of a Tuple
            //...
            //...
        }
}

And the error I get: >The type or namespace name 'DataContract' could not be found (are you missing a using directive or an assembly reference?)

Am I not using the right namespaces?

C# Solutions


Solution 1 - C#

DataContractAttribute Class is in the System.Runtime.Serialization namespace.

You should add a reference to System.Runtime.Serialization.dll. That assembly isn't referenced by default though. To add the reference to your project you have to go to References -> Add Reference in the Solution Explorer and add an assembly reference manually.

Solution 2 - C#

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx

DataContractAttribute is in System.Runtime.Serialization namespace and you should reference System.Runtime.Serialization.dll. It's only available in .Net >= 3

Solution 3 - C#

In visual studio for .Net 4.0 framework,

  1. Try to add new reference to project.

  2. On .Net Tab, Search System.Runtime.Serialization.

  3. Now, you can use using System.Runtime.Serialization. And the error will not be shown.

Solution 4 - C#

[DataContract] and [DataMember] attribute are found in System.ServiceModel namespace which is in System.ServiceModel.dll .

System.ServiceModel uses the System and System.Runtime.Serialization namespaces to serialize the datamembers.

Solution 5 - C#

I solved this problem by adding C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Runtime.Serialization.dll in the reference

Solution 6 - C#

First, I add the references to my Model, then I use them in my code. There are two references you should add:

> using System.ServiceModel;
> using System.Runtime.Serialization;

then, this problem was solved in my program. I hope this answer can help you. Thanks.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOtielView Question on Stackoverflow
Solution 1 - C#CD..View Answer on Stackoverflow
Solution 2 - C#GuillaumeView Answer on Stackoverflow
Solution 3 - C#Md Kauser AhmmedView Answer on Stackoverflow
Solution 4 - C#Jatin KhuranaView Answer on Stackoverflow
Solution 5 - C#RotatingWheelView Answer on Stackoverflow
Solution 6 - C#user3342169View Answer on Stackoverflow