Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 667187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:54:02+00:00 2026-05-13T23:54:02+00:00

I have a class that I need to be able to serialize to a

  • 0

I have a class that I need to be able to serialize to a SQLServer session variable and be available over a WCF Service. I have declared it as follows

namespace MyNM
{
[Serializable] 
[DataContract(Name = "Foo", Namespace = "http://www.mydomain.co.uk")]

public class Foo : IEntity, ISafeCopy<Foo>
{
    [DataMember(Order = 0)] 
    public virtual Guid Id { get; set; }

    [DataMember(Order = 1)] 
    public virtual string a { get; set; }

    DataMember(Order = 2)]
    public virtual Bar c { get; set; }

    /* ISafeCopy implementation */
}


[Serializable]
[DataContract(Name = "Bar ", Namespace = "http://www.mydomain.co.uk")]
public class Bar : IEntity, ISafeCopy<Bar>
{
    #region Implementation of IEntity

    DataMember(Order = 0)]
    public virtual Guid Id { get; set; }
    [DataMember(Order = 1)]
    public virtual Baz y { get; set; }

    #endregion

    /* ISafeCopy implementation*/
}


[Serializable]
[DataContract]
public enum Baz
{
    [EnumMember(Value = "one")]
    one,
    [EnumMember(Value = "two")]
    two,
    [EnumMember(Value = "three")]
    three
}

But when I try and call this service, I get the following error in the trace log.

“System.Runtime.Serialization.InvalidDataContractException:
Type
‘BarProxybcb100e8617f40ceaa832fe4bb94533c’
cannot be ISerializable and have
DataContractAttribute attribute.”

If I take out the Serializable attribute, the WCF service works, but when the object can’t be serialized to session. If I remove the DataContract attribute from class Bar, the WCF service fails saying

Type
‘BarProxy3bb05a31167f4ba492909ec941a54533’
with data contract name
‘BarProxy3bb05a31167f4ba492909ec941a54533:http://schemas.datacontract.org/2004/07/‘
is not expected. Add any types not
known statically to the list of known
types – for example, by using the
KnownTypeAttribute attribute or by
adding them to the list of known types
passed to DataContractSerializer

I’ve tried adding a KnownType attribute to the foo class

[KnownType(typeof(Bar))]

But I still get the same error.

Can anyone help me out with this?

Many thanks

Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:54:03+00:00Added an answer on May 13, 2026 at 11:54 pm

    This question on MSDN might help: DataContract versus Serializable.

    The accepted answer from that thread:

    1. [DataContract] and [Serializable] can be used together.

    2. DataContractSerializer understands both of them. If the type is marked with both of them, it will take the projection of
      [DataContract]

    3. Here are the docs on data transfer and serialization in WCF which provide a lot of detail on the serializers and known type: Data Transfer and Serialization

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that I need to binary serialize. The class contains one
I have a simple Java class that I need to serialize to be stored
I have a class that defines a CallRate type. I need to add the
I need your expertise once again. I have a java class that searches a
I have a not-so-small class under development (that it changes often) and I need
I have a class that I wish to expose as a remote service using
I have certain classes that have common feature - class factory that I need
I have class method that returns a list of employees that I can iterate
I have a class that I want to use to store properties for another
I have a class that map objects to objects, but unlike dictionary it maps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.