我的数据合同有什么问题?

发布于 2024-11-24 19:36:48 字数 2138 浏览 2 评论 0原文

我正在编写我的第一个 WCF 服务。我试图了解数据合同是如何工作的。我已阅读 MSDN 文章,其中描述了应如何标记自定义类型创建数据合同,但我无法让我的示例工作。

我有一个简单的 DTO 对象,我已将其放置在共享库中,因为我希望客户端和服务了解此类型(对吗?)它看起来像这样:

  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; }
        }
    }
}

这是 ServiceContract:

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

我创建了一个小控制台应用程序来尝试将此 MockCheckInDTO 发送到我的服务,但它永远不会到达那里。我似乎遇到了序列化 MockCheckInDTO 对象的问题。有人可以帮我吗?

这是我看到的例外:

System.Runtime.Serialization.SerializationException:键入“org.healthwise.gatewayinterfaces.mocks.MockCheckInDTO”,数据协定名称为“MockCheckInDTO:http://schemas.datacontract.org/2004/07/org.healthwise.gatewayinterfaces”。模拟'不是预期的。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装迷糊 2024-12-01 19:36:48

尝试从私有字段中删除 [DataMember] ,这样它就只在公共属性上。如果您仍然遇到问题,那么最好让自己了解 DataContract 的情况,而不是将 DC 放在共享库中,而是从服务元数据自动创建它。然后看一下它是否是您所期望的。如果没有,当您尝试序列化/反序列化对象时,您至少会知道出了什么问题。

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.

回眸一遍 2024-12-01 19:36:48

首先,奇怪的是您将相同的数据序列化两次:私有字段和公共属性。正如 Tim S. 所说,最好删除一个。
我尝试直接使用 DataContractSerializer 重现您的问题,但失败了。


            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);
            }

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);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文