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

  • Home
  • SEARCH
  • 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 7502637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:56:40+00:00 2026-05-29T20:56:40+00:00

I have a simple data contract: [DataContract] public class MyData { [DataMember] public string

  • 0

I have a simple data contract:

[DataContract]
public class MyData
{
   [DataMember]
   public string AwesomeData { get; set; }
}

And service contract:

[ServiceContract]
public interface IMyDataService
{
   [OperationContract]
   MyData GetAwesomeData();
}

In the server-side assembly, I create a derived class for doing processing:

public class MyDataWithInnerds: MyData
{
   public MyDataWithInnerds(object intializationStuff)
   {
      AwesomeData = Hypermaxulate(initializationStuff);
   }
}

and the service implementation:

public class MyDataService: IMyDataService
{
  public MyData GetAwesomeData()
  {
     return new MyDataWithInnerds(HupnerRayvakManager.GetInitializationStuff());
  }
}

MyDataWithInnerds is just the functional implementation of the purely data contract MyData.

Is there anyway without decorating MyData and referencing MyDataWithInnerds to tell the serializer to serialize MyDataWithInnerds as MyData? The assembly with MyData is also given to clients. I don’t want the server-side only MyDataWithInnerds to be referenced.

I don’t need to de-serialize it to MyDataWithInnerds. I’m guessing I’m going to need to clone the MyDataWithInnerds instance to a MyData instance so it doesn’t have any type information associated with MyDataWithInnerds, but it would be really efficient to not have to do that and just let the serializer know “I know this is a MyDataWithInnerds, but you can just treat it like the base MyData”

Thanks,
Mike

EDIT

I was able to add the following to my config file on the server:

  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="MyClientLib.MyData, MyClientLib">
          <knownType type="MyServerLib.MyDataWithInnerds, MyServerLib"/>
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>

And this makes the serialization work properly.

It just leaves two side questions:

  1. In my JSON serialization, I end up with a “__type” member that contains the derived class type. Can I remove that?
  2. Is there a declarative way to do, on the server objects (not MyData), what I have done in configuration (adding KnownType’s)?

Thanks2,
Mike

  • 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-29T20:56:42+00:00Added an answer on May 29, 2026 at 8:56 pm

    In the end, I created a copy constructor for my base class and instead of returning the derived class (as the base class), I return new base(derived).

    [DataContract]
    public class MyData
    {
       [DataMember]
       public string AwesomeData { get; set; }
    
       public MyData(MyData obj)
       {
           AwesomeData = obj.AwesomeData;
       }
    }
    

    The service implementation then becomes:

    public class MyDataService: IMyDataService
    {
      public MyData GetAwesomeData()
      {
         MyDataWithInnerds data = 
             new MyDataWithInnerds(HupnerRayvakManager.GetInitializationStuff());
         return new MyData(data);
      }
    }
    

    In the simplistic example it looks like overkill, but in the actual application, it works out decently — without having extra configuration and not emitting extraneous serialized data.

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

Sidebar

Related Questions

I have a simple object: [DataContract] public class MyClass { [DataMember(Name = MyClassNo)] public
I have a simple class I'm serializing. [DataContract(Name = Test, Namespace = )] public
Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
I have some simple POCO object: public class ProductCategoryDTO { public string Name {
I have only simple data types in method signature of service (such as int,
I have a simple WCF Data Services service and I want to expose a
If I have a simple piece of data to store (an integer or string
I have a simple object that get's geocoding data from the Google Maps API
I am new to WCF and have a simple question... My DataContract class returns
I have a simple class, let's say Team and I expose a WCF service

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.