May 18 2007

How to make a web service call from javascript

Category: Tips and TricksJoeGeeky @ 22:10

The other day I was having a coversation with a couple of my colleague about making web service calls via JavaScript.  Within the .NET Web Service Ecosystem (e.g. ASMX web services) there are two popular technologies which are being used to facilitate this process

  • Microsoft AJAX Extensions
    • In order to make AJAX calls using this, the target Web Service must be decorated with the ‘[ScriptService]’ attribute which can be a problem if you do not own the web service.
  • JavaScript SOAP Client
    • This is an open source project that is delivered in the form of a .js file.  The good news here is that you can see how it all works and have complete control over what you put in your projects

Here are a couple of links to get started with either… 

Microsoft AJAX Extensions:
http://ajax.asp.net/docs/
(Look at the ‘Web Services’ Section)

Open Source JavaScript SOAP Client:
http://www.guru4.net/articoli/javascript-soap-client/en/

Working DEMOs
http://www.guru4.net/articoli/javascript-soap-client/demo/en.aspx

As a side note, either of these can be handy if you are developing service oriented Vista Gadgets...  Enjoy...

Tags:

May 16 2007

How to invoke a web service without a Web Reference

Category: Tips and TricksJoeGeeky @ 04:08

At some point in your career you will need or want to call a Web Service from either a class library, a server control, HTTPModule, etc... If your lucky, you will soon realize that making a "Web Reference" in these types of projects is; at the very least; problemattic.  So what if you could reflect a Web Service and then invoke it as you would with other object types, assemblies, and so forth...  Well...  You can.  Below you will find a link to a commented class that you can use to make late-bound calls to web services given there WSDL Path, Service Name, and Service Type Name (e.g. class name).  Once you are familiar with its inner workings it is pretty easy to extend to support custom SOAP Headers, Type Reflection, etc...

samplewsinvoker.htm (32.88 kb)

Once you have the invoker, making a web service call is pretty simple.

Dim wsInvoker As New WebServiceReflectedMethodInvoker(
    "http://localhost/service/service.asmx?WSDL", "service", "service")
Dim arguments As New OrderedDictionary
Dim user As Object
arguments.Add("username", Username)
arguments.Add("userIsOnline", True)
'Execute the WS 
user = wsInvoker.InvokeWebServiceMethod("GetUser", arguments)

Here is yet another side note...  There is a performance hit when you make this type of call as compared to a "Web Reference".  However, one benefit is that you do not have to keep a lot of extraneous proxy classes under source control for methods and classes you do not plan on using.  Additionally, you will not run into problems related to needed "Update Web Reference" just to keep your solution up to date.

Tags: ,

Jan 31 2007

Should we use ASMX as a Web Service

Category: Intellectual PursuitsJoeGeeky @ 09:35

I was having a conversation with one of my colleagues and we were having a debate related to what Web Service technologies made sense to use for one of our new projects. After doing some digging I was surprised to find out that ASMX 2.0 is only compliance with SOAP and WSDL Specifications.  The below graph is an extract from a document supplied by Microsoft (click for the full view).  Digging deeper I learned that ASMX had many of the bones required to become compliant with the other standards, assuming you were willing to write the code.  After a little sole searching and some prototyping with other technologies I decided that; while not compliant; Microsoft had one feature that made it all worth while.  It was FAST, EASY, FAST, and EASY...  and given the inherent support within TFS we opted to go with ASMX.  That being said, you should look closely at ASMX compliance levels, WSE, WCF, etc... before making any decisions on what to use.  

Tags: