none
The formatter threw an exception while trying to deserialize the message RRS feed

  • Question

  • Hi guys, getting a message which is confusing me somewhat, not because of the content but because I'm actually getting the message.

    System.ServiceModel.FaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:action. The InnerException message was 'Error in line 1 position 8761. Element 'http://schemas.datacontract.org/2004/07/Ascertus.MatterLifecycle.DAL.Domain:ContainerAttribute' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/Ascertus.MatterLifecycle.DAL.Domain:LookupAttribute'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'LookupAttribute' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

    I have on my server machine a LookupAttribute which which has a base abstract class of ContainerAttribute (which in turn has a base abstract class of BaseEntity (not marked as a DataContract). I confused about the error as I have the [ServiceKnownType(typeof(LookupAttribute))] attribute against the contract interface for the method being called and I also have [KnownType(typeof(LookupAttribute))] on the contract implementation. I can also see the type in the client application Object Browser, which suggests to me, the client application does know about the type.

    Any ideas what I could be doing wrong?

    Cheers

    Stu


    Tuesday, October 21, 2014 4:27 PM

Answers

  • Thanks, I just used the standard add service reference.

    I don't think this error is actually the error at all...... like I said, it has been deserialising ok in parts of the method as it lets me step through the LookupAttributes in the collection using .OfType<LookupAttribute>

    I noticed I got it again though much further on in my code when I had a stub NotImplementedException get hit.

    I'm going to close the problem now anyway as currently its not affecting the code running under normal circumstances. Just a bit odd!

    Cheers for the help

    Wednesday, October 22, 2014 4:28 PM

All replies

  • Hi legal developer,

    Could you please try to post the main code in here, then I want to have a test in my side.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 22, 2014 5:11 AM
    Moderator
  • Hi Amy,

    Thanks, but that's going to be a lot of code!

    EF Models (you'll have to make up the other models as they aren't important)

    namespace Ascertus.MatterLifecycle.DAL.Domain
    {
        [DataContract(IsReference = true)]
        public abstract class ContainerAttribute : BaseEntity
        {
            /// <summary>
            ///     Unique Reference
            /// </summary>
            [DataMember]
            public int ContainerAttributeId { get; set; }
    
            /// <summary>
            ///     FieldId as stated in the Field Dictionry
            /// </summary>
            [DataMember]
            [Required]
            public int FieldDictionaryId { get; set; }
    
            [DataMember]
            [ForeignKey("FieldDictionaryId")]
            public FieldDictionary FieldDictionary { get; set; }
    
            /// <summary>
            ///     ContainerId the field value is applied to
            /// </summary>
            [DataMember]
            [Required]
            public int ContainerId { get; set; }
    
            [DataMember]
            [ForeignKey("ContainerId")]
            public Container Container { get; set; }
    
            [DataMember]
            public int SourceAttributeDefinitionId { get; set; }
    
            [DataMember]
            [ForeignKey("SourceAttributeDefinitionId")]
            public SourceAttributeDefinition SourceAttributeDefinition { get; set; }
    
            [NotMapped]
            public WizardAttributeType ContainerAttributeType { get; set; }
    
            public abstract bool HasValue();
    
            public int WizardPageDisplay(int templateId)
            {
                //TODO: Unit Test
                if (!SourceAttributeDefinition.AppliesToTemplate(templateId) ||
                    SourceAttributeDefinition.TemplateAttributeDefinitions == null ||
                    !SourceAttributeDefinition.TemplateAttributeDefinitions.Any())
                    return SourceAttributeDefinition.WizardPageId;
    
                TemplateAttributeDefinition template =
                    SourceAttributeDefinition.TemplateAttributeDefinitions.SingleOrDefault(t => t.TemplateId == templateId);
    
                if (template != null && template.WizardPageId.HasValue)
                    return template.WizardPageId.Value;
    
                return SourceAttributeDefinition.WizardPageId;
            }
        }
    }
    
    namespace Ascertus.MatterLifecycle.DAL.Domain
    {
        [DataContract(IsReference = true)]
        public class LookupAttribute : ContainerAttribute
        {
            public LookupAttribute()
            {
                ContainerAttributeType = WizardAttributeType.Lookup;
            }
    
            [DataMember]
            public int? AttributeLookupValueId { get; set; }
    
            [DataMember]
            [ForeignKey("AttributeLookupValueId")]
            public AttributeLookupValue AttributeLookupValue { get; set; }
    
            public override bool HasValue()
            {
                return (AttributeLookupValueId.HasValue && AttributeLookupValueId > 0) ||
                    (AttributeLookupValue != null  && !string.IsNullOrEmpty(AttributeLookupValue.Description));
            }
        }
    }
    
    [DataContract(IsReference = true)]
        public abstract class ValueAttribute : ContainerAttribute
        {
            [DataMember]
            [MaxLength(1024)]
            public string Value { get; set; }
    
            public override bool HasValue()
            {
                return !string.IsNullOrEmpty(Value);
            }
        }
    
        /// <summary>
        ///     Master container table
        /// </summary>
        [DataContract(IsReference = true)]
        public class Container : BaseEntity
        {
            /// <summary>
            ///     Unique database reference
            /// </summary>
            [DataMember]
            public int ContainerId { get; set; }
    
    
            [DataMember]
            public List<ContainerAttribute> ContainerAttributes { get; set; }
    }
    
    namespace Ascertus.MatterLifecycle.DAL.Domain
    {
        [DataContract(IsReference = true)]
        [Table("ActionContainerDetails")]
        public class ActionContainerDetail : ActionItemDetail
        {
            /// <summary>
            ///     Id from the Linked table in question
            /// </summary>
            [DataMember]
            [Required]
            public int ContainerId { get; set; }
    
            [DataMember]
            [ForeignKey("ContainerId")]
            public Container Container { get; set; }
        }
    }
    
    namespace Ascertus.MatterLifecycle.DAL.Domain
    {
        /// <summary>
        ///     Links the ActionQueue to the items of the correct type
        /// </summary>
        [DataContract(IsReference = true)]
        public abstract class ActionItemDetail : BaseEntity
        {
            /// <summary>
            ///     UniqueId
            /// </summary>
            [DataMember]
            public int ActionItemDetailId { get; set; }
    
            /// <summary>
            /// </summary>
            [DataMember]
            [Required]
            public int LifecycleActionId { get; set; }
    
            /// <summary>
            ///     Action navigation property
            /// </summary>
            [DataMember]
            [ForeignKey("LifecycleActionId")]
            public LifecycleAction Action { get; set; }
    
            /// <summary>
            ///     Current state of the container
            /// </summary>
            [DataMember]
            public ProcessState State { get; set; }
    
            [DataMember]
            [MaxLength(1024)]
            public string Comments { get; set; }
    
            public int? AutomatedRetryCount { get; set; }
    
            [DataMember]
            [MaxLength(5)]
            public string FaultCode { get; set; }
        }
    }
    
    namespace Ascertus.MatterLifecycle.DAL.Domain
    {
        /// <summary>
        ///     Action to be performed against a container object
        /// </summary>
        [DataContract(IsReference = true)]
        public class LifecycleAction : BaseEntity
        {
            [DataMember]
            public int LifecycleActionId { get; set; }
    
            [DataMember]
            public DateTime? ActivityCompletionDate { get; set; }
    
            [DataMember]
            public int? DependancyId { get; set; }
    
            [DataMember]
            [ForeignKey("DependancyId")]
            public LifecycleAction Dependancy { get; set; }
    
            [DataMember]
            public InstructionPriority Priority { get; set; }
    
            [DataMember]
            public List<ActionItemDetail> ActionItems { get; set; }
    
            [DataMember]
            [Required]
            public int ObjectTypeId { get; set; }
    
            [DataMember]
            [Required]
            public ActionCategory ActionCategory { get; set; }
    
            [DataMember]
            [Required]
            public ActivityType ActivityType { get; set; }
    
            [DataMember]
            [Required]
            public ProcessState State { get; set; }
    
            [DataMember]
            public int? ActorId { get; set; }
    
            [DataMember]
            [ForeignKey("ActorId")]
            public Actor Actor { get; set; }
    
            [DataMember]
            [MaxLength(2048)]
            public string Comments { get; set; }
    
            [DataMember]
            public int SourceId { get; set; }
    
            [DataMember]
            [ForeignKey("SourceId")]        
            public Source Source { get; set; }
        }
    }

    Contract

    namespace Ascertus.MatterLifecycle.Service
    {
        [ServiceContract]
        public interface IMatterLifecycleService
        {
            [OperationContract]
            [ServiceKnownType(typeof (ActionContainerDetail))]
            [ServiceKnownType(typeof (ActionMetadataDetail))]
            [ServiceKnownType(typeof(LookupAttribute))]
            [ServiceKnownType(typeof(ValueAttribute))]
            [ServiceKnownType(typeof(NumericAttribute))]
            [ServiceKnownType(typeof(DateAttribute))]
            [ServiceKnownType(typeof(BooleanAttribute))]
            [ServiceKnownType(typeof(TextAttribute))]
            LifecycleAction RequestLifecycleAction(int serviceActorId);
    }
    }

    [KnownType(typeof (Container))]
        [KnownType(typeof(LookupAttribute))]
        [KnownType(typeof(ValueAttribute))]
        [KnownType(typeof(BooleanAttribute))]
        [KnownType(typeof(TextAttribute))]
        [KnownType(typeof(DateAttribute))]
        [KnownType(typeof(NumericAttribute))]
        [KnownType(typeof(SourceLookupAttributeDefinition))]
        [KnownType(typeof(SourceValueAttributeDefinition))]
        public class MatterLifecycleService : IMatterLifecycleService
        {
            public LifecycleAction RequestLifecycleAction(int actorServiceId)
            {
                ActionRequestFacade request = null;
    
                LifecycleAction requestedAction = null;
                try
                {
                    _logger.AddInfo(String.Format("RequestLifecycleAction({0}) called", actorServiceId));
    
                    request = UnityInstance.Container.Resolve<ActionRequestFacade>();
    
                    requestedAction = request.RequestAction(actorServiceId);
    
                    if (requestedAction != null)
                        _logger.AddDebug(String.Format("RequestLifecycleAction({0}) returning action {1}", actorServiceId,
                            requestedAction.LifecycleActionId));
    
                    return requestedAction;
                }
                catch (Exception ex)
                {
                    _logger.AddError(string.Format("Error occurred Requesting Container Action - {0}", ex.Message), ex);
    
                    if (request != null && requestedAction != null)
                    {
                        _logger.AddDebug("Action Rollback in progress");
                        request.Rollback();
                    }
    
                    return null;
                }
            }
    }
    You can then just return a single item from the database
            public LifecycleAction RequestAction(int actorServiceId)
            {
                var actorInfo = new ActorInformation(actorServiceId);
    
                //Check actor can process result or has it exceded concurrent usage
                if (!actorInfo.ActorCanProcessRequest())
                {
                    _logger.AddDebug(String.Format("{0} concurrent request limit reached, no action returning",
                        actorInfo.ActorService.ServiceActor.ActorIdentifier));
                    return null;
                }
    
                _requestManager = new LifecycleActionRequestManager(actorInfo);
                try
                {
                    LifecycleAction action = _requestManager.RequestNextAction();
    
                    return action;
                }
                catch (Exception ex)
                {
                    _logger.AddError("Error occurred Requesting Next Action - Rollback in progress", ex);
    
                    _requestManager.Rollback();
                    throw;
                }
            }  

    I've tried to include most dependencies but delete as appropriate as well as the main code, I've left out the retrieval code as you can probably just set it up to return dummy objects rather than the EF Domain objects.

    Cheers

    Stu

    Wednesday, October 22, 2014 7:55 AM
  • I've actually done some more digging and it makes this error even more confusing. I deal with the Lookup Attributes in my client service prior to getting this error!

    So the application definitely knows about the type, wondering if this error is a bit of a red herring.

    Wednesday, October 22, 2014 8:26 AM
  • Hi Stu,

    just a quick hint that might help:

    • The ServiceKnownTypeAttribute is for decorating ServiceContracts and/or OperationContracts.
    • KnownTypeAttribute is for decorating classes within your DataContract. So decorating your service implementation with KnowTypeAttributes would only have an effect, if an instance of this implementation would actually be serialized or deserialized at some point. But I am not sure if this causes your problem.

    Since your problem seems to be happening on your client side (deserialize) I am wondering how you created your client proxy? At runtime? Using svcutil? Are you sharing your DataContract between client and service?

    Wednesday, October 22, 2014 3:41 PM
  • Thanks, I just used the standard add service reference.

    I don't think this error is actually the error at all...... like I said, it has been deserialising ok in parts of the method as it lets me step through the LookupAttributes in the collection using .OfType<LookupAttribute>

    I noticed I got it again though much further on in my code when I had a stub NotImplementedException get hit.

    I'm going to close the problem now anyway as currently its not affecting the code running under normal circumstances. Just a bit odd!

    Cheers for the help

    Wednesday, October 22, 2014 4:28 PM