开发者

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.Serializat开发者_如何学Cion.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


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.


first of all, it is weired that you serialize the same data twice: the private fields and the public properties. As Tim S. said it's better to remove one. I tried to reproduce your problem by using DataContractSerializer directly, but I failed.


            DataContractSerializer serializer = new DataContractSerializer(typeof(MockCheckInDTO));

            var data = new MockCheckInDTO();

            using (var file = File.OpenWrite("dto.xml"))
            using (var xmlWriter = XmlDictionaryWriter.CreateTextWriter(file))
            {

                serializer.WriteObject(xmlWriter, data);
            }

            using (var file = File.OpenRead("dto.xml"))
            using (var xmlReader = XmlDictionaryReader.CreateTextReader(file, XmlDictionaryReaderQuotas.Max))
            {

                var result = serializer.ReadObject(xmlReader);
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜