Namespace for [DataContract]
Asked Answered
A

6

238

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?

Adena answered 13/9, 2011 at 12:23 Comment(2)
Using VS 2017, compiler error, can't build, no message in errors list or any indication at all. No syntax coloring of DataContract, nothing.Lach
@Lach Right click on your project Add/Reference. In the dialog find System.Runtime.Serialization and make checkbox cheked state. Then Press "Ok"Cud
C
445

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.

Campy answered 13/9, 2011 at 12:25 Comment(6)
....and that assembly isn't referenced by default, so you have to go to References -> Add Reference in Solution Explorer and add an assembly reference...Coniferous
That's it, I didn't have the reference in my solution! I thought that if a reference were needed, it would get me an error on the line using System.Runtime.Serialization;. I guess I was wrong :)Adena
Ugh. Why on Earth doesn't it error on the using statement? That seems to make sense to me.Freyah
I guess that's because another dll is using that namespace (otherwise you would get an error)Alluvial
Thank you! None of Microsoft's example doc mentions this.Survive
If you are using VisualStudio you'll need to exit VS and reopen it before it will recognize newly referenced packages.Advowson
Z
7

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

Zurn answered 13/9, 2011 at 12:26 Comment(0)
Y
7

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.
Yarak answered 25/11, 2015 at 12:29 Comment(0)
G
5

[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.

Gynaeco answered 13/12, 2012 at 16:2 Comment(0)
W
1

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

Whit answered 15/4, 2019 at 20:42 Comment(0)
G
0

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.

Gook answered 4/3, 2014 at 1:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.