C#WCF传递字典< string,object>范围 [英] C# WCF Passing Dictionary<string, object> parameter

查看:90
本文介绍了C#WCF传递字典< string,object>范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Dictionary发送到WCF服务.但是,如果objcte类型是Array或List,它将向我显示错误尝试序列化参数时出现错误……."

I am try send Dictionary to WCF service. However if the objcte type is Array or List, it will show me the error " There was an error while trying to serialize parameter......."

尝试序列化参数时发生错误 http://tempuri.org/:str . InnerException消息为类型 带有数据协定名称的"System.String []" 'ArrayOfstring: http://schemas.microsoft.com/2003/10/Serialization/Arrays ' 不是预期的.将任何静态未知的类型添加到以下列表中 已知类型-例如,通过使用KnownTypeAttribute属性 或将它们添加到传递给的已知类型列表中 DataContractSerializer.".有关更多详细信息,请参见InnerException.

There was an error while trying to serialize parameter http://tempuri.org/:str. The InnerException message was 'Type 'System.String[]' with data contract name 'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

这是我的WCF服务:

[OperationContract]
int PDFImport(string server, 
              string repo, 
              string username, 
              string password, 
              string templateName, 
              string entryName, 
              byte[] image, 
              Dictionary<string, Object> value, 
              string entryPath = @"\1. Incoming", 
              string volume = "DEFAULT");

也尝试添加一些ServiceKnowType属性,但仍然无法正常工作

Also try to adding some ServiceKnowType Attributes, but still not working

[ServiceContract]
[ServiceKnownType(typeof(Dictionary<string, string>))]
[ServiceKnownType(typeof(Dictionary<string, object>))]
[ServiceKnownType(typeof(Dictionary<string, List<string>>))]
[ServiceKnownType(typeof(List<Dictionary<string, List<string>>>))]

请帮帮我.预先感谢.

推荐答案

作为该词典中的对象传递的是什么?

What are you passing as an object in that dictionary?

WCF需要知道您要传递的类型,以便它可以反序列化该对象,换句话说,WCF是强类型的.

WCF needs to know what type are you passing so that it can deserialise that object in other words WCF is strongly typed.

如果您尝试使用此方法

[OperationContract]
 int Method(object o);

并从客户端传递DataContract类型:

and from client pass a DataContract type:

 [DataContract]
    public class MyDataClass
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public DateTime DateOfBirth { get; set; }   
    }

由于Method(object o)已捕获WCF 不能反序列化的所有已定义类型,因此WCF运行时仍将引发CommunicationException.

the WCF runtime will still throw CommunicationException because Method(object o) has catch all type defined that WCF can not deserialise.

如果您要像这样用MyDataClass替换对象

If you would replace object o with MyDataClass like so

  [OperationContract]
     int Method(MyDataClass myClassObject);

并传入MyDataClass对象,它应该可以正常工作.

and pass in MyDataClass object, it should work as expected.

现在相同的规则适用于Dictionary<string, Object>.试图定义像上面所示的MyDataClass这样的DataContract类型,而不是像这样的对象 Dictionary<string, MyDataClass>,以便WCF可以反序列化它,并通过电线发送到服务方法.

Now same rule applies to the Dictionary<string, Object>. Tye to define a DataContract type like MyDataClass as shown above instead of object like Dictionary<string, MyDataClass> so that WCF can deserialise it, send across the wire to the service method.

如果要保持通用性,请使用手动序列化方法Stream,XML或byte [].

If you want to keep it generic then use manual serialisation methods Stream, XML or byte[].

希望这会有所帮助!

这篇关于C#WCF传递字典&lt; string,object&gt;范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆