Search code examples
wcfdatacontractserializerdatacontract

What is wrong with my DataContract?


I am writing my first WCF service. I am trying to understand how Datacontracts work. I have read the MSDN Article that describes how custom types should be marked up to create a data contract but I cannot get my example to work.

I have a simple DTO object that I have placed in a shared library because I want the client and the service to know about this type (right?) it looks like this:

  using System.Collections.Generic;
using System.Runtime.Serialization;

namespace org.healthwise.gatewayinterfaces.mocks
{
    [DataContract]
    public class MockCheckInDTO : ICheckInDTO
    {
        [DataMember]
        private string _testPackageFilePath = "testpackages\\973eb455-6acc-486b-a1dd-2cf527872b1e.zip";
        [DataMember]
        private IDictionary<string, string> _testMetaData;

        public MockCheckInDTO()
        {
            _testMetaData = MakeTestMetaDataDictionary();
        }

        private IDictionary<string, string> MakeTestMetaDataDictionary()
        {
            IDictionary<string, string> testMetaData = new Dictionary<string, string>();
            testMetaData.Add("Version", "9.0.1");
            testMetaData.Add("Product Family", "Learning Modules");
            return testMetaData;
        }

        [DataMember]
        public string PackageFileLocation
        {
            get { return _testPackageFilePath; }
            set { _testPackageFilePath = value; }
        }

        [DataMember]
        public IDictionary<string, string> PackageMetaData
        {
            get { return _testMetaData; }
            set { _testMetaData = value; }
        }
    }
}

This is the ServiceContract:

[ServiceContract]
    public interface IIndexCheckIn
    {
        [OperationContract]
        void AddToIndex(MockCheckInDTO mockCheckInDto);
    }

I have created a little console application to attempt to send this MockCheckInDTO over to my service but it never gets there. It seems that I am having and issue serializing the MockCheckInDTO object. Can someone help me out?

This is the exception I am seeing:

System.Runtime.Serialization.SerializationException: Type 'org.healthwise.gatewayinterfaces.mocks.MockCheckInDTO' with data contract name 'MockCheckInDTO:http://schemas.datacontract.org/2004/07/org.healthwise.gatewayinterfaces.mocks' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known type


Solution

  • Try removing [DataMember] from the private fields, so it's just on the public properties. If you're still having trouble, it might be good for educating yourself on what's going on with your DataContract to, instead of having the DC in a shared library, have it automatically created from the service metadata. Then take a look at it and see if it's what you expect. If not, you'll at least have an idea of what's going wrong when you try to serialize/deserialize the object.