7

我有一个ScriptServiceWeb 方法(.NET 3.5),它采用抽象基类型的单个参数:

[WebMethod(EnableSession=true)]
[ScriptMethod()]
public bool Test(Item item) { ... }

和:

namespace Namespace {
public abstract class Item
{
    public int id;
}

public class Group : Item
{
    public Item[] items;
}

public class Instance : Item
{
    public string whatever;
}
}

通常,当方法被调用时,item将是一个Group包含Instance和/或Group对象。我从 jQuery 调用这个服务;我没有使用 Microsoft 客户端框架。调用其他方法工作正常。

问题: 当我进行调用时,甚至在调用我的方法之前就引发了异常。例如,如果我的电话是:

POST /WebService.asmx/Test HTTP/1.1
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*

{"item":{"id":0,"__type":"Namespace.Group","items":[]}}

...我得到一个InvalidOperationException

{"Message":"Operation is not valid due to the current state of the object.","StackTrace":"   at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

如果我删除__typeJSON 对象的成员或将其更改为Namespace.Item(并从中删除abstract修饰符Item),异常就会消失,但生成的反序列化对象显然有点没用。

我错过了什么?

4

2 回答 2

15

啊哈!单击此问题右侧的相关链接,我找到了帮助我解决此问题的答案。

GenerateScriptType属性必须应用于 Web 服务类:

[WebService( ... )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[GenerateScriptType(typeof(Group))]
[GenerateScriptType(typeof(Instance))]
public class WebService : System.Web.Services.WebService
{
    [WebMethod(EnableSession=true)]
    [ScriptMethod()]
    public bool Test(Item item) { ... }
}

没有这些属性,反序列化器就不会知道我的派生类型。

于 2010-09-29T06:20:55.257 回答
0

我可以使用Newtonsofts JsonConvert如下方法解决这个问题。

[System.Web.Services.WebMethod]      
public static void MyAjaxCallingMethod(object inputs)
    {           
    string stingDataFromAjaxCall = JsonConvert.SerializeObject(inputs);
            MyObject myObj = JsonConvert.DeserializeObject<MyObject>(stingDataFromAjaxCall);
            ....}
于 2018-11-23T12:44:03.283 回答