C#WCF传递字典<;字符串,对象>;参数
我正在尝试将字典发送到WCF服务。但是,如果对象类型是数组或列表,它将向我显示错误“尝试序列化参数时出错……” 尝试序列化参数时出错 . InnerException消息为“类型” 具有数据协定名称的“System.String[]” “ArrayOfstring:” 这是预料不到的。将任何静态未知的类型添加到 已知类型-例如,通过使用KnownTypeAttribute属性 或者将它们添加到传递给的已知类型列表中 DataContractSerializer.'。有关更多详细信息,请参阅InnerException 这是我的WCF服务:C#WCF传递字典<;字符串,对象>;参数,c#,wcf,dictionary,C#,Wcf,Dictionary,我正在尝试将字典发送到WCF服务。但是,如果对象类型是数组或列表,它将向我显示错误“尝试序列化参数时出错……” 尝试序列化参数时出错 . InnerException消息为“类型” 具有数据协定名称的“System.String[]” “ArrayOfstring:” 这是预料不到的。将任何静态未知的类型添加到 已知类型-例如,通过使用KnownTypeAttribute属性 或者将它们添加到传递给的已知类型列表中 DataContractSerializer.'。有关更多详细信息,请参阅Inn
[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");
[运营合同]
int PDFImport(字符串服务器,
字符串回购,
字符串用户名,
字符串密码,
字符串templateName,
字符串入口名,
字节[]图像,
字典值,
字符串entryPath=@“\1.传入”,
字符串volume=“DEFAULT”);
还尝试添加一些ServiceKnowType属性,但仍然不起作用
[ServiceContract]
[ServiceKnownType(typeof(Dictionary<string, string>))]
[ServiceKnownType(typeof(Dictionary<string, object>))]
[ServiceKnownType(typeof(Dictionary<string, List<string>>))]
[ServiceKnownType(typeof(List<Dictionary<string, List<string>>>))]
[服务合同]
[ServiceKnownType(类型(字典))]
[ServiceKnownType(类型(字典))]
[ServiceKnownType(类型(字典))]
[ServiceKnownType(类型(列表))]
请帮帮我。提前谢谢。你在那本词典中作为对象传递的是什么 WCF需要知道传递的是什么类型,以便它可以反序列化该对象,换句话说,WCF是强类型的 如果你愿意试试这个方法
[OperationContract]
int Method(object o);
并从客户端传递一个DataContract类型:
[DataContract]
public class MyDataClass
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Id { get; set; }
[DataMember]
public DateTime DateOfBirth { get; set; }
}
WCF运行时仍将抛出CommunicationException,因为方法(对象o)
定义了WCF无法反序列化的捕获所有类型
如果像这样用MyDataClass替换对象o
[OperationContract]
int Method(MyDataClass myClassObject);
并传入MyDataClass对象,它应该按预期工作
现在同样的规则也适用于字典
。Tye定义一个类似MyDataClass的DataContract类型,如上图所示,而不是类似object的类型
字典
以便WCF可以对其进行反序列化,并通过连接发送到服务方法
如果希望保持通用性,则使用手动序列化方法Stream、XML或byte[]
希望这有帮助 不幸的是,WCF不支持泛型。但有一种方法可以创建字典作为输入。我不确定这是你想要的,但我有一个相同的问题,我可以用这种方式解决它
[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);
}
}
}
为什么需要发送类型对象?我想他正在发送System.String[]作为对象。这是我第一次写WCF服务。它用于与一个ECM软件通信。因此,对于字典,字符串表示模板名称,对象表示值(可以是DateTime、int、double、list、array等),这就是我传递对象类型的原因。int、double、float和其他前置类型应该可以正常工作。DateTime是一个结构,应该也可以。列表和数组列表不起作用。谢谢你的回复。我试试看。
public void TestMethod(WebServiceInputGetDataParams input)
{
//access the input through dictiobnary
input.Entries
}