ASP.NET Web服务返回的参数IndexOutOfRangeException [英] ASP.NET Web Service returns IndexOutOfRangeException with arguments

查看:329
本文介绍了ASP.NET Web服务返回的参数IndexOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Web服务:

I have the following web service:

[ScriptService]
public class Handler : WebService {

    [WebMethod]
    public void method1() {

        string json = "{ \"success\": true }";

        System.Web.HttpContext.Current.Response.Write(json);

    }

    [WebMethod]
    public object method2(Dictionary<string, object> d) {

        Dictionary<string, object> response = new Dictionary<string, object>();

        response.Add("success", true);

        return response;

    }

}

第一种方法接受一个传统的HTML表单提交和响应写一个JSON字符串到页面上。第二种方法接受通过AJAX发布了JSON值,并返回序列化对象。

The first method accepts a traditional html form post and response writes a JSON string to the page. The second method accepts a JSON value posted via AJAX and returns a serialized object.

这两种方法做工精细自己,但是当在同一Web服务放在一起我打电话时,方法1得到这个错误:

Both these methods work fine on their own but when put together in the same web service I get this error when calling method1:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

当我从他们的工作方法2删除的参数。

When I remove the arguments from method2 they work.

任何人都可以说明为什么发生这种情况?

Can anyone suggest why this is happening?

编辑:

问题从参数类型方法2的跨越。如果我将其更改为一个字符串或简单数据类型,它工作正常。由于乔尔表明,它可能是因为字典不能被序列化。这似乎并没有影响到阿贾克斯送我的要求,只有打破了直接形式的帖子这个处理程序。因此,我的解决方法是通过自己把表单处理后在一个单独的文件。不理想,但适合我的应用程序。

The problem spans from the argument type of method2. If I change it to a string or simple data type it works fine. As Joel suggests it's probably because Dictionaries can't be serialized. This doesn't seem to affect my requests sent by ajax and only breaks direct form posts to this handler. Therefore my workaround is to put the form post handlers in a separate file by themselves. Not ideal but works for my application.

推荐答案

字典不是序列。隐藏它的对象身后不为你做些什么。你必须先转换你的字典一个数组或发送出去之前,一些其他的序列化对象。

Dictionaries are not serializable. Hiding it behind an object doesn't do anything for you. You must first convert your dictionary to an array or some other serializable object before sending it out.

<一个href=\"http://stackoverflow.com/questions/1124597/why-isnt-there-an-xml-serializable-dictionary-in-net\">Why是不是有在.NET中的XML序列化的词典吗?
http://weblogs.asp.net/pwelter34/archive/2006 /05/03/444961.aspx
http://www.tanguay.info/web/的index.php PG = codeExamples&放大器;?ID = 333

这篇关于ASP.NET Web服务返回的参数IndexOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆