none
Deserialisation problem when using a derived type - and I'm already using ServiceKnownType RRS feed

  • Question

  • I have the following classes...

      public abstract class Fallible<T> {
      }
    
      public class Success<T> : Fallible<T> {
        public T Value { get; set; }
      }

    ...and a WCF service call like this (simplified code)...

        [OperationContract]
        [ServiceKnownType(typeof(Fallible<Patient>))]
        [ServiceKnownType(typeof(Success<Patient>))]
        public Fallible<Patient> GetPatient(int id) {
          return new Success<Patient> {Value = new Patient {ID = 1, FirstName = "Jim", Surname = "Spriggs"}};
        }

    In a test project, this works fine, and the app that consumes the WCF call (or the WCF test client) can receive the Success<Patient> result.

    Whoever, when I try to use this in a real project, I get an exception when it tries to deserialise the result...

    The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :GetPatientResult. The InnerException message was 'Element ':GetPatientResult' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/PhysioDiary.Entities.FallibleClasses:SuccessOfPatient0yGilFAm'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'SuccessOfPatient0yGilFAm' 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 the serializer.'.  Please see InnerException for more details.

    As you can see from the service call, I use the ServiceKnownType attribute on the service call, which is all I needed to do in the test project.

    I have tried adding the KnownType attribute to the Fallible and Success classes, as well as adding DataContract/DataMember attributes, but it doesn't help. I always get this exception. The Patient class was generated from an Entity Framework model but I don't think it makes any difference, as if I try the same thing with a very simple new class, I get the same exception.

    Anyone any idea what I've done wrong? I have copied and pasted the code from my test project, where it works fine, but it doesn't work here.

    Thanks in advance for any help you can give.




    • Edited by Mr Yossu Sunday, February 12, 2017 8:54 PM
    Sunday, February 12, 2017 8:46 PM

All replies

  • Hi Yossu,

    To check whether it is related with real project, I suggest you create a simple WCF Service, and copy the Fallible<T> Success<T> and Fallible<Patient> GetPatient(int id) related code to this new project.

    If this issue did not happen, I would suggest you share us the complete code of WCF Service contract, and the difference you could think out between real project and test project.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 13, 2017 1:47 AM
  • Hi Edward,

    I already mentioned that it works fine in a test project, and the problem only arose when I tried it in the real one.

    Whilst trying to sort it, I added a test class and method to see if it would work on something more simple. It didn't, but in the process of deleting that bits of that test code, both the test and real service methods started working. I have no idea what changed, but it now seems to be working.

    Thanks anyway. I'll keep an eye on it, and if it gives any more problems, I'll post back.

    Monday, February 13, 2017 2:21 PM
  • Hi Yossu,

    I am glad it works now. And I would suggest you mark your reply as answer to close current thread. If there is any new issue, please feel free to come back, and post new thread. We will keep following.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 14, 2017 3:22 AM