Thursday, April 7, 2011

Web Service Minutiae

Sometimes, as part of a web project, I'll make a web service that will be called by a front end .aspx page elsewhere in the project.  Inevitably, I will forget to do two things:

One, I'll forget to uncomment the script service line above the web method, even though Visual Studio puts a comment above it that says the following:

'To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 '<System.Web.Script.Services.ScriptService()> _
And two, I'll forget to set the InlineScript property of the service reference to true.  

Then I'll trouble shoot for an hour before I remember, oh yeah, I have to do those two things.  Hopefully by writing this on my blog, I'll remember.  Anyway, those to two tidbits are things that I forgot, but I did know at one time.  This week I learned some new things regarding web services.  I'm sure many developers out there will say, "well duh...I can't believe you didn't know that!  Dunce!"  In response to such barbs, I will quote Clark Gable in Gone With the Wind, "I apologize again for all my short comings."  Moving on...

Here are the two, new, things I learn about web services.  One, you can change the format of the SOAP data that gets passed back and forth from the verbose (but useful) XML format to the more concise Json format.   Did you know that?  I sure didn't.  You just change the property in the web method:

<WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function DoSomething() ...

Please note that the above code snippet assumes the import of the System.Web.Script.Services namespace.  I hate it when demo code doesn't include what namespaces are involved.

The second thing I learned is that there is a size limit on how big the Json object can be.  I was all happy until I quit getting results back because I knew there were lots of the results that were coming back and that was the problem.  You can set it manually in the web.config, like this:

<jsonSerialization maxJsonLength="Big ol' number"/>

...but I wouldn't recommend that.  Why?  Because the whole point of Json is to have light weight markup text so you can pass data around web services with less bandwidth.  If your Json is so big that the default size of the object isn't big enough to handle it, then maybe there's just too much data in there and it needs to be broken up some other way.  But hey, do what you want.  Pass multi gig Json objects around, set the IIS timeout to 120 minutes and put all your asp.net applications in their own app pools.  See what happens.

G'night.