Code Macro Example - Calling Webservice With Proxyclass

This example uses a different approach to calling a webservice. Instead of using the se:webservice macro, this time we will use the se:code macro. (This may be necessary when a webservice requires a complex datatype as input argument; this is easier to accomplish within code.)

At first, we used Visual Studio to auto-generate a proxy class for the webservice described in Asmx Webservice Example. Code within this proxy-class having to do with asynchronously calling webservice methods has been removed, as well as any comment-lines. Then the code is copied inside a se:code macro.

Now all we need to do is add some more lines of code to be able to use this proxy-class within the se:code macro: define a .NET class deriving from Smartsite.Core.Module and provide an implementation for the Execute() method.

Within the Execute() method, create an instance of the webservice proxy-class and call any method on it. Using e.g. this.Result.Append() you can then display the result of the webservice call.

Notice that you have to merge the using statements of both the proxy-class and the class deriving from Smartsite.Core.Module.

Smartsite SXML CopyCode image Copy Code
<se:code language="C#" error="{this.error.message()}"
 references="c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Web.Services.dll;c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll">

using System.ComponentModel;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using Smartsite.Core;
using Smartsite.Tools;
using System;


public class Class1: Module{
    public override void Execute(){
        WebService1 ws = new WebService1();
        this.Result.Append(ws.HelloWorld());
    }
}


[System.Web.Services.WebServiceBindingAttribute(Name="WebServiceSoap", Namespace="http://smartsite.nl/")]
public partial class WebService1 : System.Web.Services.Protocols.SoapHttpClientProtocol 
{        
    public WebService1() {
        this.Url = "http://localhost/TestWebservice.asmx";
    }        
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/HelloWorld", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string HelloWorld() {
        object[] results = this.Invoke("HelloWorld", new object[0]);
        return ((string)(results[0]));
    }        
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/FahrenheitToCelsius", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public double FahrenheitToCelsius(double FahrenHeit) {
        object[] results = this.Invoke("FahrenheitToCelsius", new object[] {FahrenHeit});
        return ((double)(results[0]));
    }
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/CelsiusToFahrenheit", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public double CelsiusToFahrenheit(double Celsius) {
        object[] results = this.Invoke("CelsiusToFahrenheit", new object[] {Celsius});
        return ((double)(results[0]));
    }        
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/Add", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public double Add(double n1, double n2) {
        object[] results = this.Invoke("Add", new object[] {n1,n2});
        return ((double)(results[0]));
    }        
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/Subtract", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public double Subtract(double n1, double n2) {
        object[] results = this.Invoke("Subtract", new object[] {n1,n2});
        return ((double)(results[0]));
    }        
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/Multiply", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public double Multiply(double n1, double n2) {
        object[] results = this.Invoke("Multiply", new object[] {n1,n2});
        return ((double)(results[0]));
    }        
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/Divide", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public double Divide(double n1, double n2) {
        object[] results = this.Invoke("Divide", new object[] {n1,n2});
        return ((double)(results[0]));
    }        
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://smartsite.nl/ReturnLargeMessage", RequestNamespace="http://smartsite.nl/", ResponseNamespace="http://smartsite.nl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string ReturnLargeMessage(string inputMessage, int nr) {
        object[] results = this.Invoke("ReturnLargeMessage", new object[] {inputMessage,nr});
        return ((string)(results[0]));
    }        
}
</se:code>