The Web Gambit

Thoughts on Web Development

Calling a .NET Web Service From Rails (original)

A lot of people have come to my blog by googling an older article I wrote describing how to call a .NET web service from Rails. I didn’t migrate any of my older posts over as part of my move to WordPress.com, but I’ll repost this article for those who want it. My only disclaimer is that the Rails platform has changed quite a bit and if you find any mistakes with this code in the current Rails codebase please leave a comment and I will update it appropriately. Thanks!
The Rails platform implements ActionWebService, a set of libraries used for pushing out Web services from your rails application. This seems to work relatively well, but most would agree that AWS leaves a bit to be desired in the area of consuming web services. This is where the Ruby SOAP::WSDLDriverFactory library comes into play.
In this quick example, I will use a stock quote service located here.
The nice thing about .NET webservices (which usually have the .asmx extension) is that you can call the webservice and see all the parameters from the exposed description to test things out.
So, here is the controller code I am using which calls this web service:


require 'soap/wsdlDriver'

def update_stock_info
    @security = Security.find(@params['id'])  
    factory = SOAP::WSDLDriverFactory.new("http://ws.invesbot.com/stockquotes.asmx?WSDL")
    soap = factory.create_rpc_driver
    soapResponse = soap.GetQuote(:symbol => @security.ticker)  
    price = soapResponse.getQuoteResult.stockQuote.price
    @security.description = soapResponse.getQuoteResult.stockQuote.business
    @security.current_price = strip_html(price)   
    @security.save
    soap.reset_stream
end

In this example, I am creating an object called @security which holds the price and description of a particular stock, passing its ticker symbol to a web service, and then retrieving and saving the description and current price of the stock back to my original record.
Here is what the code does:

  • Pulls up a particular Security record by referencing its ID.
  • Creates a web service factory using the WSDL file of the web service.
  • Opens an RPC driver to that web service factory.
  • Calls a particular web service method (in this case GetQuote) from the driver and passes the current Security object’s ticker to the ‘symbol’ parameter of the web service.
  • Retrieves the value of the stock’s description from the Soap object’s business field and store it in my Security object.
  • Repeats the previous step for the current price, and strips off the HTML markup before storing it as a decimal data type.
  • Saves the Security record.
  • Resets the soap stream so it can be called again.

The part I hard the most trouble with was the “marshalling” of the SOAP object. I had assumed the SOAP response would come back as XML and require some manipulation, but in fact it came back as an easy-to-use soap object based on the XML data structure. I recommend using breakpointer or IRB and use the .methods property on the soap object to traverse its structure and find the values you are looking for.
Please leave any comments on this tutorial below.

Advertisements

6 responses to “Calling a .NET Web Service From Rails (original)

  1. Mike Draper January 31, 2007 at 1:01 pm

    Do you know of any examples of this that consume a web service via https:// (SSL)?
    It’s kicking my butt!

  2. Conrad May 30, 2007 at 4:34 pm

    Hi, would it be possible to format the code in the above so that it's easier to read.
    Thanks,
    -Conrad

  3. Karthik Hariharan May 30, 2007 at 5:23 pm

    whoops! Sorry about that, changing platforms must have messed up my code formatting.  It’s fixed now.

  4. Conrad May 31, 2007 at 1:19 am

    Hi, can you share all the code for your example?
    Thanks,
    -Conrad

  5. Karthik Hariharan May 31, 2007 at 11:12 am

    Conrad,
    I actually don’t have the rest of the code for this app, but this single method should be enough for you to get the basics of calling a .NET WS from Rails.

  6. Luke October 24, 2007 at 7:46 am

    Very useful, thanks for this.

%d bloggers like this: