Unity - SOAP Interaction

From NoskeWiki
Jump to navigation Jump to search

About

NOTE: This page is a daughter page of: Unity


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:
  • 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.



Articles

Forum Threads


Links


Acknowledgements: Stig Olavsen from RandomRnD for his great article and an e-mail to help me out.