I've got a handful of public static WebMethods that I'm accessing with Javascript (directly), usually through jQuery. That's working, and I'm happy with the results (and with not having to deal with the AJAX framework).
What I'd like to be able to do is to define my own mock-JSON object to pass between JS and ASP (this is a .NET 2.0 web project, CS and VB, and I'm also referencing a 3.5 non-web project for when I need 3.5 services). But I'm having tons of problems:
- I can't specify a [DataContract], it tells me I may be missing an assembly.
- I can't figure out how to use an ISerializable interface, nor do I know if it will solve my problem or not. Specifically, adding things through GetObjectData doesn't seem to do squat when I use a JavaScriptSerializer; I always get "{}".
- Tagging my class with [Serializable] doesn't seem to do anything.
- I don't really know what other options are out there.
Some code:
[Serializable]
public class MyClass : ISerializable
{
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("test", "contents");
}
}
// in my ASP page
MyClass tmp = new MyClass();
JavaScriptSerializer s = new JavaScriptSerializer(); // System.Web.Script.Serialization.JavaScriptSerializer, to be specific.
s.Serialize(tmp) // "{}" whether here, or by returning a MyClass instance from my WebMethod function (as Object, MyClass, etc.)
I'm fairly new to CS/VB and .NET, so forgive me if I'm missing something painfully obvious, and that's why I can't find anything online ^^;
So, my plainly-stated question is: how can I make my own classes serializable in ASP with WebMethods?