How to call ASP.NET Web Service in unity3d – ASMX Service

In this tutorial, I will explain how to call ASP.NET Web service in unity3d engine. First let’s create a simple ASP.NET Web service (.asmx) then we will call it in unity3d.

IDE/Engine used for this project: Visual studio 2015 community edition, unity 5.4.1f1 Personal Edition

Create a WCF service

  • Open Visual Studio as an administrator and create a new project in visual studio. Select Web template and select ASP.NET Web Application. Let’s write project name as “SampleWebService” and then click on OK.How to call ASP.NET Web Service in unity3d - Create Project 1
  • Select Empty Project in next window and click on OK.
    How to call ASP.NET Web Service in unity3d - Create Project 2
  •  Right click on the “SampleWebService” project in solution explorer and add – New Item. A dialog box will open, select Web Service (ASMX). Change the name of the service to TestWebService.
    How to call ASP.NET Web Service in unity3d - add service
  • Modify the auto generated TestWebService.asmx file
  • Now run the application (press F5). Make sure visual studio is running as an administrator otherwise you will get “403.14 – forbidden” web error. A web service page will open in the browser. Click on the Service Description link.
    How to call ASP.NET Web Service in unity3d - Service Page
  • After clicking the ServiceDiscription link, WSDL document of web service will open. This document link will be used to create a class that will be used in the client side to call this service.
    How to call ASP.NET Web Service in unity3d - WSDL Page

Call ASP.NET Web Service in unity3d

  • First, we will create the class mentioned in the previous section. To create this class, we will use wsdl.exe tool. To use this tool, go to you unity installed directory and follow the path “../Editor/Data/Mono/lib/mono/2.0”. In this directory, you will find wsdl.exe file.
  • Open command window as an administrator, navigate directory path as mentioned in previous step and run the following command –
       wsdl -out:SampleService.cs http://localhost:58707/TestWebService.asmx?WSDL
    Make sure that wsdl document URL is correct in the command line. This command will generate “SampleService.cs” file in the same directory where wsdl tool is present.
  • Change the Mono compatibility setting in Unity. Go to your Player Settings (File -> Build Settings -> Player Setting), expand “other Settings” and set “API Compatibility Level” to .NET 2.0.
  • Create a new folder in Assets directory under project hierarchy named as “Plugins” in unity editor. Copy “System.Web.dll” and “System.Web.Services.dll” from “../Editor/Data/Mono/lib/mono/2.0” path and paste it in the Plugins folder.
    How to call ASP.NET Web Service in unity3d - Plugin folder
  • Create “Scripts” folder in Assets directory under project hierarchy. Copy “SampleService.cs” in Scripts directory.
  • Create a new Script – CallWebService in the scripts folder.
    How to call ASP.NET Web Service in unity3d - Scripts Folder
  • Modify the CallWebService script.
  • Attach the script to Main Camera and play. You will see the web service response in console window.
    How to call ASP.NET Web Service in unity3d - OutputI hope this tutorial helped you to understand how to call ASP.NET Web Service in unity3d. Please post your comments for queries and feedback. Thanks for reading.
Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

8 thoughts on “How to call ASP.NET Web Service in unity3d – ASMX Service

  • February 20, 2017 at 3:35 pm
    Permalink

    Thanks a lot Gyanendu, It works fine! good post

    Reply
    • September 11, 2018 at 3:36 pm
      Permalink

      hy mikel, i posted an one error below, check whats going wrong in my code?????

      Reply
  • March 2, 2017 at 10:48 am
    Permalink

    Thanks! works great! can anybody give me a link to work with login-passwd authentication?

    Reply
  • September 28, 2017 at 11:49 pm
    Permalink

    Perfect! Thank you very much!

    Reply
  • October 28, 2017 at 6:17 am
    Permalink

    whats the differense between wcf aproach you posted earlier and web service?

    Reply
  • September 7, 2018 at 4:45 pm
    Permalink

    i did exactly that and it did generated with a lots of mistakes. I mean the build was fine, but unity compiler doesn’t see any namespaces
    Assets/Plugins/ARWebService.cs(14,18): error CS0234: The type or namespace name Services' does not exist in the namespace System.Web’. Are you missing `System.Web.Services’ assembly reference?

    Reply
  • September 11, 2018 at 3:35 pm
    Permalink

    C:\Program Files\Unity\Editor\Data\Mono\lib>cd mono

    C:\Program Files\Unity\Editor\Data\Mono\lib\mono>cd 2.0

    C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0> wsdl -out:SampleService.cs http://localhost:58707/TestWebService.asmx?WSDL
    Web Services Description Language Utility
    Mono Framework v2.0.50727.8935
    Error: There was an error downloading ‘http://localhost:58707/TestWebService.asmx?WSDL’.
    Stack:
    at System.Web.Services.Discovery.DiscoveryClientProtocol.Download(String& url, String& contentType)
    at System.Web.Services.Discovery.DiscoveryClientProtocol.DiscoverAny(String url)
    at Mono.WebServices.Driver.Run(String[] args)

    C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0> wsdl -out:SampleService.cs http://localhost:56217/TestWebService.asmx?WSDL
    Web Services Description Language Utility
    Mono Framework v2.0.50727.8935
    Error: There was an error downloading ‘http://localhost:56217/TestWebService.asmx?WSDL’.
    Stack:
    at System.Web.Services.Discovery.DiscoveryClientProtocol.Download(String& url, String& contentType)
    at System.Web.Services.Discovery.DiscoveryClientProtocol.DiscoverAny(String url)
    at Mono.WebServices.Driver.Run(String[] args)

    C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0>

    Reply
  • January 8, 2019 at 1:45 am
    Permalink

    Unfortunately, it does not work on IL2CPP backend.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *