Unity - SOAP Interaction
Recently I wanted to communicate between Unity (the game engine) and SOAP (the xml based http messaging system). During my searches I found several forum threads, which I have listed out below, but only one of these provided instructions I could follow:
Using .NET 2.0 and Mono's wsdl command
The only real solution I could find was an excellent article by Stig Olavsen which you can access here:
In this section I have written my own summary notes on this article, specific to the client side. Stig's solution is to use Mono's wsdl command line tool to generate files and placing System.Web.dll and System.Web.Services.dll into an Assets/Plugin. This should work in stand alone applications, but sadly it won't work in web player builds - the web player disables access to Web Services for security reasons (read here). A summary of what to do on the client side (skipping the server setup part):
- Download and install the latest version of Unity's IDE (here) - I used v3.4
- Download and install the latest stable version of Mono SDK (here) - I used v2.10 ... (Unity contains much of mono, but not the "wsdl" program you need)
- Go to your Terminal (Applications > Utilities) then type in:
- >wsdl http://ccdb-stage-portal.crbs.ucsd.edu:8081/SLASH_Annotation_Service2/SLASH_Annotation_Service?wsdl
- NOTE: Substitute in the location of your wsdl file above and it will generate a c sharp file to the current directory.
- Create a new Unity Project
- Go to your "Player Settings" (Edit > Project Settings > Player) and set the "API compatibility layer" to ".NET 2.0" (not Subset). **
- Copy the .cs file generated by the wsdl command into your projects Assets folder
- Create a new folder "Plugins" folder under the Assets folder.
- Copy these two .dll files: System.Web.dll and System.Web.Services.dll from /Applications/Unity/Unity.app/Contents/Frameworks/Mono/lib/mono/2.0, into your new Assets/Plugins folder.
- TIP: On Mac you will need to right click Unity and select "Show Package Contents" to see the folders in the application
- Back in Unity, check for errors
- WARNING: in my case I found that I had an "IsNullable" error that could only be solved by changing all occurrences of "IsNullable=true)] double " to "IsNullable=true)] double? " - and similar for other data types. The ? says that these data types may be returned as null, and apparently the wsdl should have inserted these for me, but didn't.
- If you have no errors, create a new C# file called "Main.cs" to create an Mono instance of your main SOAP class, and then apply your "Main.cs" to an empty object in your scene.
SIDE NOTE: It was a bit frustrating for me that SOAP and other web services only work for standalone builds, not via the web player. In my case I really wanted a web player, so I was hoping to find a work around for this issue.... hoping if I included enough of the relevant C# classes from Mono (see: Mono_source_code/mcs/class/System.Web.Services/) it might work, but I got nowhere. For Web Player builds, Web Services are disables for security reasons and so WWW is pretty much the only reliable option for transferring information via HTTP. A couple of people in the forums look like they've attempted to write they own classes to interpret SOAP messages, but I couldn't get these working, so instead I opted to just use "pure XML" and will have to write server side pages to write out and methods to parse values into each class separately. I could also interface with the database directly from Unity, but such a two-tier system is supposed to have bad security issues/implications.
- (UnifyCommunity) Webservices In Unity - well written aug 2011 article showing how to use .NET classes to implement a SOAP server and client in Unity. The author (Stig) has also posted this same article to his own site "Random R'n'D" (*****)
- (Mono) Web Services - mono's page which give a quick intro to SOAP and explains how one might write a server in C# using .NET . (***--)
- (O'Reilly) Programming Web Services with SOAP - old book showing example of C# client for SOAP interaction . (*----)
- (UnityForum) Unity and Web Services - 2011 article, but pretty poor info, except for its link to the "Webservices in Unity" article (above).
- (UnityForum) Accessing web service within Unity - 2010 thread talking about using WWW class and adding a HashTable to send HTTP POST requests, but doesn't directly deal directly with SOAP. (**---)
- (UnityForum) Soap & C# - old thread from 2009, although someone did post zip file which may work. (*----)
- (Unity Answers) Problem web service / standalone - new thread 2012 which I've commented on and am hoping will be solved soon.