将 IDictionary 对象传递给 Web 服务

发布于 2024-10-16 06:16:21 字数 818 浏览 6 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满意归宿 2024-10-23 06:16:21

不幸的是,是的,这是正常的。您看到的是经典 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.

○愚か者の日 2024-10-23 06:16:21

ASMX Web 服务使用 XmlSerializer 序列化和反序列化对象,并且没有一种好的方法可以在正确的 XML 架构中表示字典。不幸的是,我认为除了将 IDictionary 转换为可以由 XmlSerializer 序列化的另一种类型的对象之外,没有什么好的方法可以解决这个问题。

摘自本文“.NET Framework 中的 XML 序列化”(http://msdn .microsoft.com/en-us/library/ms950721.aspx):

问:为什么我无法序列化哈希表?

A: XmlSerializer 无法处理
实现 IDictionary 的类
界面。这部分是由于
日程限制,部分原因是
事实上,哈希表不
有 XSD 类型的对应项
系统。唯一的解决办法是
实现一个自定义哈希表
不实现 IDictionary
界面。

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):

Q: Why can't I serialize hashtables?

A: The XmlSerializer cannot process
classes implementing the IDictionary
interface. This was partly due to
schedule constraints and partly due to
the fact that a hashtable does not
have a counterpart in the XSD type
system. The only solution is to
implement a custom hashtable that does
not implement the IDictionary
interface.

放手` 2024-10-23 06:16:21

我发现了同样的问题,只是将 IDictionary 类型更改为动态。

其他一切都一样。

I found the same issue, just changed the IDictionary type to dynamic.

Everything else works the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文