- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在尝试使用 SilverLight 客户端调用将返回 Dictionary<string, object>
的 ASP.Net WCF 服务.当字典中的值是像 int
这样的简单类型时,它工作得很好。 , string
或 Guid
.
但是,我现在有一个场景,我需要其中一个值是 Dictionary<string, object>
的数组。 !一切都编译得很好,服务的签名没有改变,但服务调用现在失败了。
有什么解决办法吗?我试图用 KnownType
注释我的服务类和方法和 ServiceKnownType
属性,但这没有用。
这是一段代码:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
[ServiceKnownType(typeof(Dictionary<string, object>))]
public Dictionary<string, object> GetObject()
{
return new Dictionary<string, object>()
{
{ "pty1", 1 },
{ "pty2", Guid.NewGuid() },
{ "pty3", "blah" },
{ "pty4", new Dictionary<string, object>[]
{
new Dictionary<string, object>()
{
{ "pty1", 4 },
{ "pty2", Guid.NewGuid() },
{ "pty3", "blah" },
}
,
new Dictionary<string, object>()
{
{ "pty1", 4 },
{ "pty2", Guid.NewGuid() },
{ "pty3", "blahblah" },
}
}
}
};
}
}
感谢您的回答。我已经打开了 WCF 跟踪,而且正如所怀疑的那样,在序列化过程中出现了问题。问题不在于 Dictionary<string, object>
的序列化但是Array
中的一个的 Dictionary<string, object>
.
这里是 WCF 服务记录的异常。
There was an error while trying to serialize parameter :GetObjectResult. The InnerException message was 'Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][]' with data contract name 'ArrayOfArrayOfKeyValueOfstringanyType: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.
我还尝试定义一个新的 DataContract
具有单个数据成员的类,但它会导致完全相同的错误。
这是相关代码,后面是 WCF 日志记录的异常。
[DataContract]
[KnownType(typeof(ObjectHolder))]
public class ObjectHolder
{
[DataMember]
public object Object { get; private set; }
public ObjectHolder(object obj)
{
this.Object = obj;
}
}
There was an error while trying to serialize parameter :GetObjectResult. The InnerException message was 'Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[SilverlightApplication7.Web.ObjectHolder, SilverlightApplication7.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]][]' with data contract name 'ArrayOfArrayOfKeyValueOfstringObjectHolderWAwxSTlb: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.
我又玩过 ServiceKnownType
对于 ObjectHolder
, ObjectHolder[]
甚至ObjectHolder[][]
因为异常提到了“ArrayOfArrayOfKeyValueOfstringObjectHolder
”。
仍然没有解决方案。
最佳答案
首先你应该configure WCF tracing在 app cofig 文件中,可以帮助您了解服务通信背后发生的事情。在这种情况下,您可以轻松获取通信过程中发生的所有错误。
现在,让我们尝试解决您的问题。我几乎相信已知类型的问题。在面向服务的世界中,您应该手动定义所有可以参与服务契约的具体类型,因为 DataContractSerializer 不在序列化对象中提供此类信息。
在这种情况下,这意味着您应该执行以下操作:
[ServiceKnownType(typeof(string))]
[ServiceKnownType(typeof(Guid))]
[ServiceKnownType(typeof(int))] // but I think DataContractSerializer can deal himself with primitives
[ServiceKnownType(typeof(YourClass))] //UserDefined types you should add manually
public Dictionary<string, object> GetObject()
我还建议您不要在服务契约(Contract)中使用对象,因为它很容易出错。考虑一下,稍后您或您的一位同事修改了一行代码:
new Dictionary<string, object>()
{
{ "pty1", 4 },
{ "pty2", Guid.NewGuid() },
{ "pty3", new SomeClass() }, //OOPS!!!
}
在这种情况下,当您的服务尝试返回此 Dictionary 时,您会遇到运行时故障,因为 DataContractSerializer 不期望此契约中的 SomeClass。
解决这个问题的一种方法是创建单独的类型:
[DataContract]
[KnownType(typeof(Guid))]
[KnownType(typeof(SomeClass1))]
[KnownType(typeof(SomeClass2))]
public class MyType
{
private MyType(object obj) {
Object = obj;
}
public static MyType FromSomeClass1(SomeClass1 c1) {
return new MyType(c1);
}
public static MyType FromSomeClass2(SomeClass2 c2) {
return new MyType(c2);
}
public static MyType FromGuid(Guid guid) {
return new MyType(guid);
}
[DataMember]
public object Object { get; private set; }
}
在这种情况下,如果您想添加一些新类型,您应该添加工厂方法和 KnownTypeAttribute(这种方法更冗长,但不易出错)。
但是,如果您的客户端和服务是在 WCF 上编写的,您可以牺牲面向服务的主要原则并使用 NetDataContractSerializer相反 DataContractSerializer .NetDataContractSerializer 旨在补充 DataContractSerializer。您可以使用 NetDataContractSerializer 序列化一个类型,并使用 DataContractSerializer 反序列化。但是 NetDataContractSerializer 在序列化的 XML 中包含 CLR 类型信息,而 DataContractSerializer 则没有。因此,NetDataContractSerializer 可以用于任何 CLR 类型的序列化和反序列化,而无需任何 KnownTypes 内容。
关于c# - WCF 服务返回 dictionary<string, object> 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2160052/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在编码时,我问了自己这个问题: 这样更快吗: if(false) return true; else return false; 比这个? if(false) return true; return
如何在逻辑条件下进行“返回”? 在这样的情况下这会很有用 checkConfig() || return false; var iNeedThis=doSomething() || return fa
这是我的正则表达式 demo 如问题所述: 如果第一个数字是 1 则返回 1 但如果是 145 则返回 145 但如果是 133 则返回 133 样本数据a: K'8134567 K'81345678
在代码高尔夫问答部分查看谜题和答案时,我遇到了 this solution返回 1 的最长和最晦涩的方法 引用答案, int foo(void) { return! 0; } int bar(
我想在下面返回 JSON。 { "name": "jackie" } postman 给我错误。说明 Unexpected 'n' 这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来
只要“is”返回 True,“==”不应该返回 True 吗? In [101]: np.NAN is np.nan is np.NaN Out[101]: True In [102]: np.NAN
我需要获取所有在 6 号或 7 号房间或根本不在任何房间的学生的详细信息。如果他们在其他房间,简单地说,我不希望有那个记录。 我的架构是: students(roll_no, name,class,.
我有一个表单,我将它发送到 php 以通过 ajax 插入到 mysql 数据库中。一切顺利,php 返回 "true" 值,但在 ajax 中它显示 false 消息。 在这里你可以查看php代码:
我在 Kotlin 中遇到了一个非常奇怪的无法解释的值比较问题,以下代码打印 假 data class Foo ( val a: Byte ) fun main() { val NUM
请注意,这并非特定于 Protractor。问题在于 Angular 2 的内置 Testability service Protractor 碰巧使用。 Protractor 调用 Testabil
在调试窗口中,以下表达式均返回 1。 Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & "
我在本地使用 jsonplaceholder ( http://jsonplaceholder.typicode.com/)。我正在通过 extjs rest 代理测试我的 GET 和 POST 调用
这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。 var client = new RestClient("http://sub.example.com/wp-json/wp/v2
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我想我对 C 命令行参数有点生疏。我查看了我的一些旧代码,但无论这个版本是什么,都会出现段错误。 运行方式是 ./foo -n num(其中 num 是用户在命令行中输入的数字) 但不知何故它不起作用
我已经编写了一个类来处理命名管道连接,如果我创建了一个实例,关闭它,然后尝试创建另一个实例,调用 CreateFile() 返回 INVALID_HANDLE_VALUE,并且 GetLastErro
即使 is_writable() 返回 true,我也无法写入文件。当然,该文件存在并且显然是可读的。这是代码: $file = "data"; echo file_get_contents($fil
下面代码中的变量 $response 为 NULL,尽管它应该是 SOAP 请求的值。 (潮汐列表)。当我调用 $client->__getLastResponse() 时,我从 SOAP 服务获得了
我一直在网上的不同论坛上搜索答案,但似乎没有与我的情况相符的... 我正在使用 Windows 7,VS2010。 我有一个使用定时器来调用任务栏刷新功能的应用程序。在该任务栏函数中包含对 LoadI
我是一名优秀的程序员,十分优秀!