ASMX WebService/序列化:如何强制“” __类型”要包含在 JavaScript 中的属性

发布于 2024-11-03 05:20:05 字数 1345 浏览 4 评论 0原文

我正在使用 asmx webservices 并通过它们为我正在开发的 JSON 管理面板序列化/反序列化大量数据。

加载信息时,我调用一个 Web 服务,将 User[] 数组加载到 JavaScript 中。但是,我有大约 25 个 User 子类,它们具有自己独特的属性,实际上是通过此 Web 服务调用加载的。以这种方式加载它们似乎工作正常,但保存时存在一些问题。

save 方法需要保存一个 User[] 数组。在大多数子类上,javascript 中没有 __type 提示,并且反序列化失败。它似乎在具有 __type 属性的类上运行良好。

所以我的问题是这样的。我如何强制 __type 包含在序列化的 javascript 对象中?

这是我的 C# 调用,用于加载对象(简化了一点),以防万一您需要它:

[WebMethod(EnableSession = true)]
public User[] GetUsersInGroup(int UserGroupID)
{
    List<User> UsersInGroup = User.GetUsersInGroup(UserGroupID);
    return UsersInGroup.ToArray();
}

这是保存方法:

[WebMethod(EnableSession = true)]
public void SaveUsers(User[] Users)
{
        foreach (User CurUser in Users)
        {
            CurUser.Save();
        }
}

根据请求,服务标头:

  [ScriptService]
    public class FormFields : System.Web.Services.WebService
    {

和 JSON(简化的) - 注释显示不存在的内容:

{
    "d": [
        {
            "__type": "Tools.User.AccountingUser",  /* This is missing */
            "UserID": 3934,
            "HasQBAccess": true 
        },
        {
            "__type": "Tools.User.PowerUser",
            "UserID": 3937,
            "AccessDB": true,
            "AccessFTP": true 
        } 
    ] 
}

I'm working with asmx webservices and serializing/deserializing a lot of data through them for a JSON admin panel I'm working on.

When information is loaded, i call a webservice that loads a User[] array into javascript. However, I have around 25 subclasses of User with their own unique properties that are actually getting loaded by this webservice call. It seems to work fine to load them this way, but saving has some issues.

The save method takes an array of User[] to save. On most of the subclasses, there is no __type hint in the javascript, and the deserialization fails. It seems to work fine on the classes that do have a __type property.

So my question is this. How can i force the __type to be included in the javascript objects that get serialized?

Here's my C# call to load the objects (simplified a bit) just in case you need it:

[WebMethod(EnableSession = true)]
public User[] GetUsersInGroup(int UserGroupID)
{
    List<User> UsersInGroup = User.GetUsersInGroup(UserGroupID);
    return UsersInGroup.ToArray();
}

And here's the Save Method:

[WebMethod(EnableSession = true)]
public void SaveUsers(User[] Users)
{
        foreach (User CurUser in Users)
        {
            CurUser.Save();
        }
}

And as requested, the Service Header:

  [ScriptService]
    public class FormFields : System.Web.Services.WebService
    {

And the JSON (simplified) - comment shows what isn't there:

{
    "d": [
        {
            "__type": "Tools.User.AccountingUser",  /* This is missing */
            "UserID": 3934,
            "HasQBAccess": true 
        },
        {
            "__type": "Tools.User.PowerUser",
            "UserID": 3937,
            "AccessDB": true,
            "AccessFTP": true 
        } 
    ] 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

海风掠过北极光 2024-11-10 05:20:05

为了获得某种有效的答案,即使它不是一个好的答案。

到目前为止,我能找到的最好答案是向我的网络服务添加一个非常长的丑陋的函数定义。我永远不必调用它,只需让它放在那里即可使 C# 识别类型:

[WebMethod(EnableSession = true)]
public void ThisMethodSucks(AccountingUser u1, PowerUser u2, AdminUser u3, DatabaseUser u4, ADAdministratorUser u5, PSUser u6, NUTUser u7, TechUser u8, ...[all classes here])
{
      //this method never needs to get called, just sits here.
}

In the interest of having some sort of answer that works, even if it isn't a good one.

So far, the best answer I can find is to add a really long ugly function definition to my webservice. I never have to call it, just letting it sit there makes C# recognize the types:

[WebMethod(EnableSession = true)]
public void ThisMethodSucks(AccountingUser u1, PowerUser u2, AdminUser u3, DatabaseUser u4, ADAdministratorUser u5, PSUser u6, NUTUser u7, TechUser u8, ...[all classes here])
{
      //this method never needs to get called, just sits here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文