How to talk to a C# / ASP.Net web service via Ruby / Rails

Maybe not the right answer.. but I took the path of least resistance and succeeded. Yay Ruby!

First I did a dummy Xml Web service as shown in this MSDN walkthrough which does F to C temp conversion. This meant that I can access my legacy/existing C# code without any interop hassles. Yes it is SOAP based (and old-school) but it will have to do for now. You can test out the web service via a dummy stub page that exercises the web service. You can also infer the needed urls from this page.

Next I found this uber-blog post by Ryan Heath and it was all downhill after that.

You write a wrapper Ruby class like this which gets the WSDL and sets itself up. You can then just call your Web methods on it!

   require 'soap/wsdlDriver'

class TempConverterWrapper
attr_accessor :endpoint, :service_name
def initialize(endpoint, service)
@endpoint = endpoint
@service = service

def convert_fahrenheit_to_celsius(temp)
soap = wsdl.create_rpc_driver
response = soap.convert_fahrenheit_to_celsius(:temp_in_fahrenheit => temp)

def wsdl
puts "http://#{@endpoint}/#{@service}.asmx?WSDL""http://#{@endpoint}/#{@service}.asmx?WSDL")

Your main / irb code to exercise this web service. More bang / LOC!

puts "Enter a fahrenheit value to convert to celsius... via a Web Service"
temp_fahrenheit = gets.to_f

wrapper ='localhost:1464', 'TemperatureConverter')
puts "The corresponding celsius value is #{wrapper.convert_fahrenheit_to_celsius(temp_fahrenheit)}"

No comments:

Post a Comment