将 IDictionary 对象传递给 Web 服务
在我的 asmx 文件中,我将
[WebMethod]
[ScriptMethod]
public void Method(IDictionary<string, CustomClass> objectOfCustomClass)
{
//do stuff
}
自定义类定义为:
public class CustomClass
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
一切顺利,并且 Web 服务正在从 Jquery ajax 方法成功使用。最近我决定测试并尝试从直接 URL 访问它,例如 http://localhost/Services.asmx/Method
我收到此消息
“无法序列化接口 System.Collections.Generic.IDictionary”
这似乎关闭。是什么原因造成的?这是否正常?我很困惑 - 这似乎是一种干净的做事方式,但根据微软的说法,不应该这样做,但它确实有效,只是在直接访问网络服务时不起作用。什么给?我还在一些 MS 网站上读到,您无法将 IDictionary 作为参数传递给 Web 服务,但它工作正常......那么它可以完成吗?有人可以一劳永逸地澄清这一点吗?
In my asmx file, I have
[WebMethod]
[ScriptMethod]
public void Method(IDictionary<string, CustomClass> objectOfCustomClass)
{
//do stuff
}
custom class is defined as:
public class CustomClass
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
All goes well and the web service is being successfully consumed from Jquery ajax method. Recently I decided to test and try and access it from the direct URL such as http://localhost/Services.asmx/Method
and I get this message
"Cannot serialize interface System.Collections.Generic.IDictionary"
This seems off. What's causing it and is it normal? I am confused - it seems like the clean way of doing things but according to Microsoft shouldn't be done, yet it works, only not when accessing the web service directly. What gives? I also read on some MS sites that you can't pass IDictionary to the web service as a parameter yet it works fine...SO can it be done or not? Can someone clarify this once and for all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,是的,这是正常的。您看到的是经典 ASP.NET Web 服务堆栈中 XML 序列化(XmlSerializer)的限制 - 它拒绝与实现 IDictionary 的任何内容一起工作。
通过 [ScriptMethod] 向 JavaScript 公开的服务使用不同的序列化器(JavaScriptSerializer),它没有此限制。
当您从 JavaScript 调用服务时,您将调用 JSON 端点(使用 [ScriptMethod] 声明)。然而,当您从浏览器进行测试时,您会到达传统的 XML 端点(用 [WebMethod] 声明)。
有一些解决方法,请参阅此问题。但是,如果您只需要支持 AJAX 客户端,则可以简单地删除 XML 端点([WebMethod] 属性)并避免该问题。
附带说明一下,WCF 中改进的序列化程序支持字典的序列化。
Unfortunately, yes, this is normal. What you see is a limitation of XML serialization (the XmlSerializer) in the classic ASP.NET Web Service stack - it refuses to work with anything that implements IDictionary.
The service exposed to JavaScript via [ScriptMethod] uses a different serializer (the JavaScriptSerializer), which does not have this limitation.
When you call the service from JavaScript, you invoke the JSON endpoint (declared with [ScriptMethod]). When you test from the browser, however, you reach the traditional XML enpoint (declared with [WebMethod]).
There are some workarounds, see e.g. this question. However, if you only need to support AJAX clients, you may simply remove the XML endpoint (the [WebMethod] attribute) and avoid the issue.
As a side note, the improved serializer in WCF supports serialization of dictionaries.
ASMX Web 服务使用 XmlSerializer 序列化和反序列化对象,并且没有一种好的方法可以在正确的 XML 架构中表示字典。不幸的是,我认为除了将 IDictionary 转换为可以由 XmlSerializer 序列化的另一种类型的对象之外,没有什么好的方法可以解决这个问题。
摘自本文“.NET Framework 中的 XML 序列化”(http://msdn .microsoft.com/en-us/library/ms950721.aspx):
ASMX Webservices serialize and deserialize objects using an XmlSerializer, and there isn't a good way to represent a Dictionary in a proper XML schema. Unfortunately, I don't think there is a good way around this besides converting your IDictionary to another type of object that can be serialzed by an XmlSerializer.
Taken from this article "XML Serialization in the .NET Framework" (http://msdn.microsoft.com/en-us/library/ms950721.aspx):
我发现了同样的问题,只是将 IDictionary 类型更改为动态。
其他一切都一样。
I found the same issue, just changed the IDictionary type to dynamic.
Everything else works the same.