【问题标题】:How to serialize a Dictionary<string, List<string>> using DataContractSerializer?如何使用 DataContractSerializer 序列化 Dictionary<string, List<string>>?
【发布时间】:2011-08-06 12:40:11
【问题描述】:

当我尝试序列化这个类时,我得到了一个 CommunicationException。 我是否需要使用任何特定的 CollectionDataContract 标记才能使其正常工作?

[DataContract()]
public class MyClass
{
  [DataMember()]
  public Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
}

【问题讨论】:

    标签: .net dictionary datacontractserializer


    【解决方案1】:

    这应该可以正常工作(请参见下面的示例)。您得到的完整例外是什么?

    public class StackOverflow_6966835
    {
        [DataContract()]
        public class MyClass
        {
            [DataMember()]
            public Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
        }
        [ServiceContract]
        public interface ITest
        {
            [OperationContract]
            MyClass GetMyClass();
        }
        public class Service : ITest
        {
            public MyClass GetMyClass()
            {
                return new MyClass
                {
                    dictionary = new Dictionary<string, List<string>>
                    {
                        { "one", "uno un eins".Split().ToList() },
                        { "two", "dos deux zwei".Split().ToList() },
                        { "three", "tres trois drei".Split().ToList() }
                    }
                };
            }
        }
        public static void Test()
        {
            Console.WriteLine("Stand-alone serialization");
            MemoryStream ms = new MemoryStream();
            MyClass c = new Service().GetMyClass();
            DataContractSerializer dcs = new DataContractSerializer(typeof(MyClass));
            dcs.WriteObject(ms, c);
            Console.WriteLine("Serialized: {0}", Encoding.UTF8.GetString(ms.ToArray()));
    
            Console.WriteLine();
            Console.WriteLine("Now using in a service");
    
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
            host.Open();
            Console.WriteLine("Host opened");
    
            ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
            ITest proxy = factory.CreateChannel();
    
            Console.WriteLine(proxy.GetMyClass());
    
            ((IClientChannel)proxy).Close();
            factory.Close();
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

    【讨论】:

    • 错误类似于“读取管道时出错:无法识别的异常 109 (0x6d)”(翻译成英文)。顺便说一句,我正在使用 NetNamedPipeBinding。
    • 服务器可能因为某种原因关闭了频道。尝试在客户端和服务器上都启用跟踪,跟踪应该包含原因。
    • 我发现了问题。正如您所说,它应该可以正常工作,并且我的代码示例本身确实没有任何问题。但是,该示例显然被简化了,问题出在实际代码中。 (我无意中尝试通过未标记 [DataContract] 或 KnownType 的线路发送继承的类型)。感谢您保证它应该有效!
    猜你喜欢
    • 2017-01-15
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多