开发者

ASMX JSON WebService, changing property types and having old __type - error

开发者 https://www.devze.com 2023-03-31 19:21 出处:网络
I have a .NET JSON web service [WebService(Namespace = \"http://tempuri.org/\")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

I have a .NET JSON web service

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
[GenerateScriptType(typeof(Item))]
[GenerateScriptType(typeof(Info))]
[GenerateScriptType(typeof(Details))]
publ开发者_JAVA百科ic class API : System.Web.Services.WebService
{
    [WebMethod]
    public Item[] GetItems()
    {
         ...
    }

    [WebMethod]
    public bool SaveItem(Item item)
    {
         ...
    }
}

Definition of classes is at the bottom of the message.

There is a property More of type Info in Item class.

On the client side (HTML5) i'm calling GetItems(), and storing it in local storage, and periodically call SaveItem.

JSON data that i get on the client is something like:

[{"__type":"MyApp.API.Item","More":{"__type":"MyApp.API.Info","ID":1}}]

All fine so far.

The problem occurs when i change property More from type Info to type Details in Item class, i.e:

[Serializable]
public class Item 
{
    public Details More { get; set; } // <----- type changed from Info to Details
}

Since my client has the data cached, when i try to call SaveItem ASP.NET is throwing error:

 Cannot convert object of type 'MyApp.API.Info' to 'MyApp.API.Details'

Question: If there a solution to this (ideally keeping the ASMX web service, and not changing JSON data on the client)?

Here's what the classes are:

[Serializable]
public class Item 
{
    public Info More { get; set; }
}

[Serializable]
public class Details: Info, IDetails
{
    public string Notes { get; set; }
}

[Serializable]
public class Info: IInfo
{
    public int ID { get; set; }
}

public interface IDetails: IInfo
{
    string Notes { get; }
}

public interface IInfo
{
    int ID { get; }
}


If you don't want to see the "_type":"object" you can have your method as object type. all this text would dissapear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号