Asmx Webservice Example

Testing the se:webservice macro isn't always that easy, since you need a target webservice to connect to. This example provides a basic asmx webservice with some mathematical functions, which you can configure to run in one of your sites.

First the code of the webservice which should be saved to a file within the App_Code folder of your site's rootpath:

C# CopyCode image Copy Code
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
 
[WebService(Namespace = "http://smartsite.nl/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
    public WebService() { }
 
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public double FahrenheitToCelsius(double FahrenHeit) {
        return ((FahrenHeit - 32) * 5) / 9;
    }
    [WebMethod]
    public double CelsiusToFahrenheit(double Celsius) {
        return ((Celsius * 9) / 5) + 32;
    }    
    [WebMethod]
    public double Add(double n1, double n2) {
        return n1 + n2;
    }
    [WebMethod]
    public double Subtract(double n1, double n2) {
        return n1 - n2;
    }
    [WebMethod]
    public double Multiply(double n1, double n2) {
        return n1 * n2;
    }
    [WebMethod]
    public double Divide(double n1, double n2) {
        return n1 / n2;
    }    
    [WebMethod]
    public string ReturnLargeMessage(string inputMessage, int nr) {
        string result = "";
        for (int i = 0; i < nr; i++) {
            result += inputMessage;
        }
        return result;
    }
}

Assuming this code is saved to the file Webservice.cs (within the App_Code folder) create an asmx file (e.g. TestWebservice.asmx) with the following code:

HTML CopyCode image Copy Code
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>

Now save this file to your site's rootpath and you will have a basic asmx webservice for testing purposes. Just enter the address of the asmx file in a browser window (e.g. http://localhost/TestWebservice.asmx) and you can test it right away.

Calling Asmx Webservice using se:webservice

Once the test webservice is up and running, you can use the se:webservice macro to execute methods on the webservice as is done in the sxml example shown below.

Notice that in most cases, you would need to specify the following parameters: url, methodname, methodnamespace and methodarguments if the method requires arguments. Soapaction is usuallly required as well, but when it's not specified it will default to methodnamespace + methodname (which in general will be the correct value).

Notice as well that the methodnamespace equals the namespace specified in the C# code example on the WebServiceAttribute. The sxml example also uses the se:webservice parameter outputinnertext and set its value to true, since a single result is expected.

Smartsite SXML CopyCode image Copy Code
<se:webservice>
    <se:parameters>
        <se:parameter name="url">http://localhost/TestWebservice.asmx</se:parameter>
        <se:parameter name="methodname">CelsiusToFahrenheit</se:parameter>
        <se:parameter name="methodnamespace">http://smartsite.nl/</se:parameter>
        <se:parameter name="outputinnertext">true</se:parameter>
        <se:parameter name="methodarguments">        
            <se:collection>
                <se:member name="Celsius" type="double">12</se:member>
            </se:collection>        
        </se:parameter>
    </se:parameters>
</se:webservice>