ASP.NET Ajax weirdness

I just ran into a strange issue when trying to serialize one of my objects over a webservice that’s called using Ajax, the weirdness isn’t what you need to resolve the issue but instead how it manifests itself in such a convoluted way and goes like this:

public class Foo
{
    public Foo(Bar bar)
    {
    }

    // some properties here...
}

Say I have the above class and then the following in my webservice:

[WebMethod]
[GenerateScriptType(typeof(Foo))]
public Foo RetrieveFoo(int id)
{
// perform retrival here...
}

Now if I have some javascript on the client that tries to make a call to this webservice it will fail because the javascript proxy to the webservice just doesn’t get created so it ends up being null, this is not good cos’ if you have been making various changes you have no way of knowing why the proxy class isn’t being generated so you have to spend time tracking down what could be causing the issue.

Turns out it’s because my Foo class needs to have a public empty constructor but it took a while to track this down, couldn’t a better way of alerting the issue to the developer been accomplished.

Advertisements