【问题标题】:KeyValuePair in WCF ServiceWCF 服务中的 KeyValuePair
【发布时间】:2016-11-10 09:55:06
【问题描述】:

我有一个 WCF 服务,其方法 UpdateProperty 采用 PropertyValue 作为参数:

void UpdateProperty(PropertyValue propertyValue)

[DataContract]
public class PropertyValue
{
    [DataMember]
    public string Property { get; private set; }

    [DataMember]
    public object Value { get; private set; }
}

我正在尝试使用 KeyValuePair 作为值来调用该方法,但出现此错误:

"格式化程序在尝试反序列化 消息:尝试反序列化参数时出错 http://tempuri.org/:propertyValue。 InnerException 消息是 '第 1 行位置 544 错误。元素 'http://schemas.datacontract.org/2004/07/MyProject.DataContracts:Value' 包含映射到名称的类型的数据 'http://schemas.datacontract.org/2004/07/System.Collections.Generic:KeyValuePairOfanyTypeanyType'。 反序列化器不知道映射到此名称的任何类型。 如果您正在使用,请考虑使用 DataContractResolver DataContractSerializer 或添加对应的类型 'KeyValuePairOfanyTypeanyType' 到已知类型列表 - 对于 例如,通过使用 KnownTypeAttribute 属性或将其添加到 传递给序列化程序的已知类型列表。'。请参见 InnerException 了解更多详情。”

我尝试在课堂上添加 KnownType,但仍然收到错误:

[KnownType(typeof(KeyValuePair<object, object>))]

知道为什么吗?我在其他方法(作为参数)中使用其他 KeyValuePair 没有问题...

在我的 SOAP 正文中,我有:

    <d4p1:Value xmlns:d7p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" i:type="d7p1:KeyValuePairOfanyTypeanyType">
      <d7p1:key>test</d7p1:key>
      <d7p1:value>test</d7p1:value>
    </d4p1:Value>

【问题讨论】:

  • 您通过网络进行序列化的KeyValuePair&lt;,&gt; 的实际类型是什么?真的是KeyValuePair&lt;object, object&gt; 还是KeyValuePair&lt;string, string&gt;
  • 我都试过了,但我的用例真正需要的是 KeyValuePair
  • 他似乎不知道您在属性Value 中使用的类型。用[KnownType( typeof(MyType) )] 标记你的班级PropertyValueMyType 是你在Value 属性中给他的类型。
  • 1) 你能说明你是如何将[KnownType(typeof(KeyValuePair&lt;object, object&gt;))] 属性放在PropertyValue 上的吗?您是否修改了客户端和服务器端的合同以包含这种已知类型?
  • 我只是将它添加到服务器上的类中。然后我更新了客户端上的服务参考。在生成的代理中,我有新的 KnownType 但它仍然不起作用: [System.Runtime.Serialization.KnownTypeAttribute(typeof(System.Collections.Generic.KeyValuePair))] public partial class PropertyValue : object , System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

标签: c# wcf serialization


【解决方案1】:

我想我知道你想要什么。试试这个对象作为你的输入:

    [Serializable]
public class WebServiceInputGetDataParams : ISerializable
{
    public Dictionary<string, object> Entries
    {
        get { return entries; }
    }


    private Dictionary<string, object> entries;
    public WebServiceInputGetDataParams()
    {
        entries = new Dictionary<string, object>();
    }
    protected WebServiceInputGetDataParams(SerializationInfo info, StreamingContext context)
    {
        entries = new Dictionary<string, object>();
        foreach (var entry in info)
        {
            entries.Add(entry.Name, entry.Value);
        }
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (var entry in entries)
        {
            info.AddValue(entry.Key, entry.Value);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2011-04-27
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多