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 8716491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:05:23+00:00 2026-06-13T06:05:23+00:00

I am getting the following error when trying to return a Questionnaire object to

  • 0

I am getting the following error when trying to return a
Questionnaire object to the client. I added the KnowType[typeof(…)] in the Data Contract as suggested, but it still doesn’t work. Not knowing which type is unknown to the Serializer, I just threw in all classes that are in the EF model. Can someone help? Thanks.

Here is the Service Contract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using QuestionnaireWcfServiceApp.Models;

namespace QuestionnaireWcfService
{
    [ServiceContract]
    public interface IQuestionnaireService
    {
        [OperationContract]
        QuestionnaireContract GetQuestionnaire(string questionnaireName);

        [OperationContract]
        QuestionChain LoadQuestion(int questionnaireID, int? questionID, int? userResponse);
    }

    [DataContract]
    [KnownType(typeof(Questionnaire))]
    [KnownType(typeof(Question))]
    [KnownType(typeof(Choice))]
    [KnownType(typeof(Decision))]
    [KnownType(typeof(QuestionFlow))]
    public class QuestionChain
    {
        [DataMember]
        public Question Question { get; set; }

        [DataMember]
        public int? Decision {get;set;}
    }

    [DataContract]
    [KnownType(typeof(Questionnaire))]
    [KnownType(typeof(Question))]
    [KnownType(typeof(Choice))]
    [KnownType(typeof(Decision))]
    [KnownType(typeof(QuestionFlow))]
    public class QuestionnaireContract
    {
        [DataMember]
        public Questionnaire Questionnaire { get; set; }
    }
}

Here is the Service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using QuestionnaireWcfServiceApp.Models;

namespace QuestionnaireWcfService
{
    public class QuestionnaireService : IQuestionnaireService
    {
        QuestionnaireWcfServiceApp.Models.QuestionnaireEntities db = new QuestionnaireEntities();

        public QuestionnaireContract GetQuestionnaire(string questionnaireName)
        {
            QuestionnaireContract questionnaireContract = new QuestionnaireContract();
            if (!string.IsNullOrEmpty(questionnaireName))
            {
                Questionnaire thisQuestionnaire = (from q in db.Questionnaires where q.Name.Equals(questionnaireName) select q).FirstOrDefault();
                if (thisQuestionnaire == null)
                    throw new ArgumentNullException("Questionnaire ID is not found.");
                else
                {
                    questionnaireContract.Questionnaire = thisQuestionnaire;
                    return questionnaireContract;
                }
            }
            else
                throw new ArgumentException("Questionnaire name is not specified.");
        }

        public QuestionChain LoadQuestion(int questionnaireID, int? currentQuestionID, int? userResponse)
        {
            QuestionChain qc = new QuestionChain();
            QuestionFlow thisFlow = null;
            Question nextQuestion = null;           
            Questionnaire thisQuestionnaire = (from q in db.Questionnaires where q.QuestionnaireId == questionnaireID select q).FirstOrDefault();

            if (thisQuestionnaire == null)
                throw new ArgumentNullException("Questionnaire ID is not found");   //InvalidOperationException;

            if (currentQuestionID.HasValue)
            {
                //QuestionID should never be changed after setup.  Change the QuestionText around the QuestionID
                Question thisQuestion = thisQuestionnaire.Questions.Where(q => q.PKey.Equals(currentQuestionID)).FirstOrDefault();
                if (thisQuestion == null)
                    throw new ArgumentNullException("Question ID is not found");
                else
                {
                    if (userResponse.HasValue)
                    {
                        thisFlow = thisQuestion.QuestionFlows.First(f => f.QuestionId.Equals(currentQuestionID) && f.ChoiceId.Equals(userResponse));
                        if (thisFlow.Question1 != null)
                        {
                            nextQuestion = thisFlow.Question1;
                            qc.Question = nextQuestion;
                        }
                        else
                        {
                            qc.Question = null;
                            qc.Decision = thisFlow.Decision.Value;
                        }
                    }
                    else
                    {
                        //can't happen. when reaches here, a userResponse must not be null
                    }
                }
            }           
            else
            {
                //default to question 1
                nextQuestion = thisQuestionnaire.Questions.First(q => q.QuestionId.Equals(1));
                if (nextQuestion == null)
                    throw new ArgumentNullException("Question ID");
                else
                    qc.Question = nextQuestion;
            }
            return qc;
        }
    }
}

This is the exception in the Windows Application Log.

Exception: System.ServiceModel.CommunicationException: There was an error while trying to serialize parameter http://tempuri.org/:GetQuestionnaireResult. The InnerException message was 'Type System.Data.Entity.DynamicProxies.Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342' 
with data contract name 'Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342:
http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' 
is not expected. Consider using a DataContractResolver or 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.'.  Please see InnerException for more details. ---> System.Runtime.Serialization.SerializationException: Type 'System.Data.Entity.DynamicProxies.Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342' with data contract name 'Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or 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.
           at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
           at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
           at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
           at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
           at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
           at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
           --- End of inner exception stack trace ---
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
           at System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest)
           at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
           at System.ServiceModel.Channels.BodyWriterMessage.OnBodyToString(XmlDictionaryWriter writer)
           at System.ServiceModel.Channels.Message.ToString(XmlDictionaryWriter writer)
           at System.ServiceModel.Diagnostics.MessageLogTraceRecord.WriteTo(XmlWriter writer)
           at System.ServiceModel.Diagnostics.MessageLogger.LogInternal(MessageLogTraceRecord record)
           at System.ServiceModel.Diagnostics.MessageLogger.LogMessageImpl(Message& message, XmlReader reader, MessageLoggingSource source)
           at System.ServiceModel.Diagnostics.MessageLogger.LogMessage(Message& message, XmlReader reader, MessageLoggingSource source)
         Process Name: WebDev.WebServer40
         Process ID: 11620

        Event Xml:
        <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
          <System>
            <Provider Name="System.ServiceModel 4.0.0.0" />
            <EventID Qualifiers="49154">5</EventID>
            <Level>2</Level>
            <Task>7</Task>
            <Keywords>0x80000000000000</Keywords>
            <TimeCreated SystemTime="2012-10-18T07:32:11.000000000Z" />
            <EventRecordID>36499</EventRecordID>
            <Channel>Application</Channel>
            <Computer>Jon-PC</Computer>
            <Security UserID="S-1-5-21-334737869-2079735299-2176000493-1000" />
          </System>
          <EventData>
            <Data>System.ServiceModel.CommunicationException: There was an error while trying to serialize parameter http://tempuri.org/:GetQuestionnaireResult. The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342' with data contract name 'Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or 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.'.  Please see InnerException for more details. ---&gt; System.Runtime.Serialization.SerializationException: Type 'System.Data.Entity.DynamicProxies.Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342' with data contract name 'Questionnaire_EF00247BEFB9F733C947A4C3E57FD12709E91510AC0DA534D137ED75FCCAC342:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or 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.
           at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
           at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
           at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
           at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
           at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
           at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
           --- End of inner exception stack trace ---
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
           at System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest)
           at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
           at System.ServiceModel.Channels.BodyWriterMessage.OnBodyToString(XmlDictionaryWriter writer)
           at System.ServiceModel.Channels.Message.ToString(XmlDictionaryWriter writer)
           at System.ServiceModel.Diagnostics.MessageLogTraceRecord.WriteTo(XmlWriter writer)
           at System.ServiceModel.Diagnostics.MessageLogger.LogInternal(MessageLogTraceRecord record)
           at System.ServiceModel.Diagnostics.MessageLogger.LogMessageImpl(Message&amp; message, XmlReader reader, MessageLoggingSource source)
           at System.ServiceModel.Diagnostics.MessageLogger.LogMessage(Message&amp; message, XmlReader reader, MessageLoggingSource source)</Data>
            <Data>WebDev.WebServer40</Data>
            <Data>11620</Data>
          </EventData>
        </Event>
  • 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-06-13T06:05:29+00:00Added an answer on June 13, 2026 at 6:05 am

    All types that are used with WCF must be either a data contract or marked as [Serializable]. This includes any objects that are in a WCF data contract already. If you have the ability, you must go and either add the [Serializable] tag to the classes, or add the WCF specific tags to them. This no kidding means that even though you add a Type to the KnownTypes, it doesn’t mean that WCF will know that it is serializable.

    If neither of these options are available, I would suggest creating “proxy” objects that can contain the values you want to pass and have those convert to and from your target objects. Sounds crazy but that is the way it goes…

    ADDED EXAMPLE:

    WCF utilizes a serializer containined in the System.Runtime.Serialization namespace to serialize and deserialize data into different formats (XML, SOAP, JSON, Binary). In order for this to work, the objects that are going to be serialized must be some type of serializable type (marked with an attribute). The built in WCF attributes for data objects looks like this:

    //This is marked with the DataContract attribute, which is WCF specific
    [DataContract]
    public class Foo
    {
        //The DataMember attribute is also WCF specific and specifies what data
        // is included in the serialization.  Any properties (or variables) must be
        // accessable, as in not read only.
        [DataMember]
        public string Property1{get;set;}
    
        //This variable will be serialized as well, even though it is private.  This
        // works great if you have a readonly property but still need to pass the data
        [DataMember]
        private int Id = 0;
    
    
        //This does not have a DataMember attribute and will not be serialized
        private string var1;
    }
    

    WCF can also utilize classes (such as the DataTable and DataSet) that are marked as Serializable.

    //This is marked with the Serializable attribute.  All public and private
    // fields are automatically serialized (unless there is a containing object
    // that is not serializable then you get a SerializationException
    [Serializable]
    public class Bar
    {
        //gets serialized
        public string Property1{get;set;}
    
        //gets serialized
        private string var1;
    }
    

    The idea of creating “proxy” objects is the same that is what is generated by Visual Studio when you add a service reference, only reversed. Say you have class “Foo2” that is not serializable, and has certain properties.

    public class Foo2
    {
        public string Property1{get;set;}
    
        public string Property2{get;set;}
    
        public int Property3{get;set;}
    }
    

    You can make a proxy class (in nearly any way you want) that will allow you to pass the proerties back and forth to and from your service.

    [DataContract]
    public class Foo2Proxy
    {
        [DataMember]
        public string Property1{get;set;}
    
        [DataMember]
        public string Property2{get;set;}
    
        [DataMember]
        public int Property3{get;set;}
    
        public Foo2Proxy()
        {
        }
    
        public Foo2Proxy(Foo2 foo)
        {
            this.Property1 = foo.Property1;
            this.Property2 = foo.Property2;
            this.Property3 = foo.Property3;
        }
    
        public static Foo2 Create(Foo2Proxy fProxy)
        {
            var foo = new Foo2();
            foo.Property1 = fProxy.Property1;
            foo.Property2 = fProxy.Property2;
            foo.Property3 = fProxy.Property3;
            return foo;
        }
    }
    

    I’m sure that there are probably 100,000 different ways and opinions on how to do this, but this is just an example of a possibility that worked for me at the time. If you take a look at this article on CodeProject you can see what I was shooting for here.

    In your particular case, you may need to create “proxy” objects (or wrapper objects whatever you want to call it) for your Question and Decision types that are created by EF, since it would seem that they are not inherently serializable, unless you can get into the code that is generated for those objects and add the Attributes as described above. One additional consideration, is if they are derived from another class (or abstract class) that base class MUST ALSO be marked as either serializable or as a DataContract!

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

Sidebar

Related Questions

I am getting the following error when trying to update an object using nhibernate.
I am getting the following error when trying to return a ViewResult from a
I am getting the following error when trying to use the WCF Test Client
When compiling, I'm getting the following error when trying to return coordinates to the
I am trying to use df.apply() function in pandas but getting the following error.
I'm getting the following error when trying to return the value using the operator*()
Getting the following error when trying to start a session: Warning: session_start() [function.session-start]: Node
I am getting the following error when trying to save a file to disk:
I am getting the following error when trying to check in my code: $>
I'm getting the following error when trying to delete objects in my database while

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.