views:

40

answers:

1

Hello

Recently a colleague had to call a .NET 2.0 web service from script. We noticed that we'd have to put the [ScriptService] attribute, either via AJAX extensions or upgrading to 3.5.

That's all I've been able to find out - no-one explains what it's doing under the hood!

Can anyone enlighten me?

Thanks Duncan

+1  A: 

Strictly speaking, you don't need to do anything to a web service in order to make it callable from script.

See here: How to call web service using vbscript (synchronous)?

A web service is just a system that is addressible via remote protocols, usually web-based protocols. Often that means HTTP. A simple request can just be performed via an HTTP GET on a particular URL - the segments in the URL path or the query string make up the "parameters" to the web service request.

A client might send an HTTP GET to

http://server/appPath/p1/p2/p3

And the application listening there would be responsible for unpacking that URL, maybe mapping the p1, p2, p3 into query parameters, and then determining how to respond to it.

For more complex requests, the transaction might be HTTP POST, and the format of the payload is either XML, JSON, or something else that your application specifies.

In this case the URL may be like so:

http://server/appPath/resource1

and the POST'd payload, if using JSON, might be:

{"Age":35,"FirstName":"Peyton","LastName":"Manning"} 

if using XML, it might be:

<person>
  <Age>35</Age>
  <FirstName>Peyton</FirstName>
  <LastName>Manning</LastName>
</person>

You can form a request that complies to those constraints in any programming language, including script like Javascript.

If you are using SOAP, then that XML document needs to be wrapped in a SOAP envelope. See Calling WCF service by VBScript for an example (VBScript, but easily translatable to Javascript).

Cheeso
What you say definitely makes sense to me, but the fact remains that if I don't put the [ScriptService] attribute on the web service then it can't be called via AJAX. According to your last link, does this mean that if I do the manual SOAP handling then I'll get away without using that [ScriptService] attribute?
Duncan
[ScriptService] generates a script and inserts it into the browser content. You *can* use that script to invoke the services but it's not a requirement. You can certainly write your own browser-side script to invoke the method, which is what I showed you how to do. more on that: http://visualstudiomagazine.com/articles/2007/08/20/consuming-web-services-with-aspnet-ajax.aspx
Cheeso