I know, I know, REST is cool and SOAP sucks. Rails is awesome and .NET blows. But the reality is I have to do a little bit of both.
Things at work are moving more and more toward Rails, but there’s still a large investment in a few .NET systems that must be maintained (until the decision is made to rewrite them in Rails). With that means there are a few SOAP services that I still need to work with. It turns out, this isn’t so bad.
Originally I thought I’d bring back AWS and deal with it that way. But from my experience, AWS has more support for generating services rather than consuming services. While AWS isn’t a terrible approach, there’s an easier way this can be handled: SOAP::WSDLDriverFactory. This is a straight-up Ruby solution that comes shipped with the standard library. It’s straight-forward to use and requires no XML parsing (which sort of surprised me).
As an example, one of the Rails applications I’m currently working on needed to have the ability perform certain actions under that users’ .NET account. Mainly, since the applications are completely separate (read: separate DB, separate users table), I needed to get the user id from the .NET system via their credentials in the Rails system. While this isn’t the actual implementation, you can see how easy this is to do.
require 'soap/wsdlDriver' class SomeDotNetWrapper attr_accesssor :endpoint, :service def initialize(endpoint=nil, service=nil) @endpoint = endpoint @service = service end def get_user_id_from_credentials(username, password) soap = wsdl.create_rpc_driver response = soap.GetUserID(:username => username, :password => password) soap.reset_stream response.getUserIDResult end private def wsdl SOAP::WSDLDriverFactory.new("http://#{@endpoint}/services/#{@service}.asmx?WSDL" end end
By creating a “factory” with the services’ WSDL, you can easily set things up for remote procedure calls (i.e. GetUserID(username, password)). What’s even nicer is that you can parse the response by chaining methods together. In this case, it put me right inside the response, where I only needed to call one level of nested XML (response.getUserIDResult). But if this were nested deeper, I’d just keep calling the methods that map to the XML nodes until I got to what I wanted. And of course, I could then write any Ruby code to do what I needed to do, but the point is it’s automatically method-like access, which is nice (think of Builder).
So, once you write your “wrapper” class, you’re ready to go.
class SomeDotNetWrapperController < ApplicationController def store_other_id raise InvalidCredentials if params[:username].blank? || params[:password].blank? service = SomeDotNetWrapper.new('http://example.com', 'authentication') other_id = service.get_user_id_from_credentials(params[:username], params[:password]) current_user.update_attribute(:other_id, other_id) unless other_id.to_i == 0 rescue InvalidCredentials # ... end end
I’m sure there are a lot of different approaches, but this seemed the easiest to me. I had somewhat of a difficult time finding a solid solution online for easily consuming .NET SOAP services, so I decided to resort back to the standard library, as generally every language has support for this sort of thing. Anyway, maybe someone else with the same needs will find this useful.





