WebServices with Mono

Cosuming a WebService - Introduction

Basic concepts

So except all that marketing terms you have heard, what ARE webservices? How do they communicate across the network? How do they WORK?

The idea

The idea behind WebServices is, that you can call functions in your code, that are executed on a webserver. So you can pass params to the function and the webserver returns a value.

Tu understand, let's take a look at an example:

Cnn cnn1 = new Cnn();
string headline = cnn1.GetHeadline(1);

Console.WriteLine(headline);
This is a hypotitical webservice, by CNN, that lets you get a headline from CNN. It looks like a normal "local" function. You create a new instance of a class, call a method, get a string returned. But infact the method is executed on the cnn server. Maybe the code on the server connects to a database to get the code. Or get is from an RSS feed. Or extract it from the Homepage. You don't know. That's the point of webservices.

Technical Background: SOAP

This sounds great. But How is it possible? How can C# access the function across the network? Ands most importantly, the class could have been created in Java or even PHP. How should it be possible to call PHP/Java functions from C#? The truth is:
When you call GetHeadLine(1), Mono will generate an Xml file. This *could* look like this:
<webservice url="http://cnn.com/webservice">
        <function name="GetHeadLine" >
                <int name="number" value="1" />
        </function>
</webservice>
In the real world it looks very different and much more complicated, but you get the point. The function and the value passed is turned into XML. This will then be sent to the WebServer. The WebServer will read the XML and call a function - in C#, Java or PHP. Then it will return a value in XML:
<webservice url="http://cnn.com/webservice">
        <function name="GetHeadLine" >
                <int name="return" value="This is some new from CNN" />
        </function>
</webservice>
Mono will read the XML and return the value.

Internal communication

This makes clear how Mono communicates with the Server and that the Server calls the functions and returns a value. It does not make clear, how Mono can be aware of the functions. How does Mono know that the class Cnn is a WebService. How does it know about what types can be passed and what types can be returned? How does this pass the compiler? How does Mono know, it should send a SOAP message?Here is the second truth: Mono has available a local copy of the functions written in C#. However the functions do not contain the REAL code, but some functions that tell Mono do call the WebServer.
// Very simplified and pretty wrong :)

public class Cnn {
    
    public Cnn() {
        Url = "http://cnn.com/webservice";
    }
    
    public int GetHeadLine(int first) {
        result = Mono.GetFunctionOnServer("GetHeadLine", "number=" + first)
        return result;
    }
    
}

I hope you get the point: This is not the real code. But you have to understand, that this "local" code is the class/ function called. This "local" function does then care about how the generate the XML and send it to the server.
Model:

Your code calls a local function "GetHeadlines"
|
--> The local function represents the functions of the remote function.
    It contains some code, that creates some XML containing the Data, sends it to the Server.
     |
     --> The Server gets the XML message and calls the requested functions, gets the returned value
         and generates a new XML message containing the returned value.
         |
 |--------
 |
 --> The local functions reads the XML,
     extracts the returned value, returns it.     
     |
 |----
<-
Your code gets  
the value returned.      

One question remains

Where does that "local function/class" come from?
Do have to write it yourself? (This would be rather nasty)No, it is autogenerated.Cool, but from what is it autogenerated?When you create your webservice with e.g. Java, it will automatically create a "wsdl"-file. WSDL stands fro "Web Service Description language". It describes - in XML - what classes/functions a WebService has and what values it expects and returns.Mono has a tool "wsdl" that will generate the local class from these descriptions.

Consuming a WebService in 3 steps

1. Get the WSDL file from somewhere. The WebService must have it available for download somewhere.
2. Run "wsdl filename.wsdl". This will generate a new C# file "filename.cs", that contains the local functions.
3. Compile it to a library filename.dll
4. Now you can use the WebService. You have to reference the assembly at compile time.