简体   繁体   English

ASP.NET Web Service使用参数返回IndexOutOfRangeException

[英]ASP.NET Web Service returns IndexOutOfRangeException with arguments

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;

    }

}

The first method accepts a traditional html form post and response writes a JSON string to the page. 第一种方法接受传统的html表单帖子,响​​应将JSON字符串写入页面。 The second method accepts a JSON value posted via AJAX and returns a serialized object. 第二种方法接受通过AJAX发布的JSON值并返回一个序列化对象。

Both these methods work fine on their own but when put together in the same web service I get this error when calling method1: 这两种方法都可以自行运行,但是当它们放在同一个Web服务中时,我在调用method1时会遇到这个错误:

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

When I remove the arguments from method2 they work. 当我从method2中删除参数时,它们可以工作。

Can anyone suggest why this is happening? 任何人都可以建议为什么会这样吗?

Edit: 编辑:

The problem spans from the argument type of method2. 问题跨越了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. 这似乎不会影响我的ajax发送的请求,只会将直接表单帖子分解为此处理程序。 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. 您必须先将字典转换为数组或其他可序列化对象,然后再将其发送出去。

Why isn't there an XML-serializable dictionary in .NET? 为什么.NET中没有XML可序列化的字典? http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx http://www.tanguay.info/web/index.php?pg=codeExamples&id=333 http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx http://www.tanguay.info/web/index.php?pg=codeExamples&id=333

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM