Web service – the consumer (client)

As from the web service server from the previous post you can now link to that web service. You will need to do a couple of things to be able to get the client to “talk” to the web service.

To start with on the server part of the tutorial you will need to create the dll (dynamically linked library) for the FirstWebService of the server, this is because this is what the client will use to “talk” to the server. To create this dll file you will need to compile up the FirstWebService, pull information from the server about the service and then just compile into a library

I am using mono, so if you are using .net within Windows then there is a similar command (may be just wsdl instead of wsdl2)

wsdl2 http://localhost/csharp/web_service.asmx?WSDL
 
--- output from the above command
Web Services Description Language Utility
Mono Framework v2.0.50727.1433
Writing file 'FirstWebService.cs'

as you can see it has created a file called FirstWebService.cs, a csharp source file of the WSDL (Web Services Description Language). To compile this into a FirstWebService.dll within the mono environment you just need to

gmcs /t:library FirstWebService.cs -r:System.Web.Services

the /t:library means to create a .dll file, if you do not pass in the “-r:System.Web.Services” it will complain with the below error.

FirstWebService.cs(21,51): error CS0234: The type or namespace name `Services' does not exist in the namespace `System.Web'. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings

if you place the FirstWebService.dll within a bin directory within the directory where you are hosting the client from (you may need to create the bin directory for the dll)

Now it is the consumer (the client)

Since we have a FirstWebService.dll within the bin, this means that we try to compile up the client (on-the-fly) it knows how to create the class of FirstWebService. So to call the function of “Add” on the web service, we just need to create a new class of type FirstWebService and then call that function (and the rest is done behind the scenes)

    FirstWebService myFirstWebService = new FirstWebService();
    myFirstWebService.Add(4,5);

that is about it, of course it is nicer to have a web page to post some values to the server from the client, so here is the client source code, if you save this as web_service_consumer.aspx (aspx is a web page extension) and then just goto that web page hosted on the apache environment.

<%@ Page Language="C#" %>
<script runat="server">
// on the asp:Button onclick call this method
void runWebService_Click(Object sender, EventArgs e)
{
    FirstWebService myFirstWebService = new FirstWebService();
    // call the add method from the webservice
    // pass in the 2 values from the page and convert to integer values
    resultLabel.Text = myFirstWebService.Add(
		  Int32.Parse(number1.Text),
                  Int32.Parse(number2.Text)).ToString();
}
</script>
<html>
<head>
<title>ASP Web service consumer</title>
</head>
<body>
<form runat="server">
      First Number to Add : <asp:TextBox id="number1" runat="server">0</asp:TextBox>
<br/>
      Second Number To Add :
      <asp:TextBox id="number2" runat="server">0</asp:TextBox>
<br/>
      THE WEB SERVICE RESULTS!!!
<br/>
      Adding result : <asp:Label id="resultLabel" runat="server">Result</asp:Label>
<br/>
      <asp:Button id="runService" onclick="runWebService_Click" runat="server" Text="Run the Service"></asp:Button>
</form>
</body>
</html>

when you goto that page and see a error like

The type or namespace name `FirstWebService' could not be found. Are you missing a using directive or an assembly reference?
 
Source Error:
 
Line 4: void runWebService_Click(Object sender, EventArgs e)
Line 5: {
Line 6:     FirstWebService myFirstWebService = new FirstWebService();

that is because you have not created the bin directory and placed the FirstWebService.dll into that directory as describe at the top of this page.

Web service

An web service is basically like a SOAP server in that it “talks” from the client-server in XML with the client requesting the function on the server and obtaining a result.

I am using mono to compile and run the web service since I am running apache on Linux (kubuntu) (here is how I setup the mono apache module within k/ubuntu).

To start with, you need to create a web service instance and what language you are going to be using (since using mono then c#) and also the class that you want to “expose” to the web service itself.

<%@ WebService language="C#" class="FirstWebService" %>

after that you then need to add the attribute to the class to tell the compiler that the class is going to be a webservice and what the base directory is going to be (I created a new directory within the apache hosting directory), since we are writing a WebService we need to inherit from a System.Web.Services.WebService

[WebService(Namespace="http://localhost/csharp/")]
public class FirstWebService : WebService

and then just within the class structure you only need to tell the function with a attribute heading of [WebMethod] that it is going to be “exposed” to the web service, if you do not put that in, it will be “exposed” to the web service and thus the client cannot access that method.

    [WebMethod]
    public int Add(int a, int b)

and that is about it, the mono (and of course .net base framework) will create the rest of the WSDL and additional parts for a WebService.

Here is the full web service code in full, save this as web_service.asmx (the asmx means a web service extension)

<%@ WebService language="C#" class="FirstWebService" %>
 
using System;
using System.Web.Services;
 
// expose as the web service
[WebService(Namespace="http://localhost/csharp/")]
public class FirstWebService : WebService
{
    // expose as a web method
    [WebMethod]
    public int Add(int a, int b)
    {
        return TheAddingMethod(a,b);
    }
 
    // this one will not be exposed since it does not have the [WebMethod] attribute
    public int TheAddingMethod(int a, int b)
    {
	// but since it is part of the class you can still call class methods etc.
	return a+b;
    }
}

and when you goto the web URL for the webservice you should see something similar to this

The FirstWebService URL
The FirstWebService URL

if you click on the left menu “add” and then “test form” to test the webservice, it will bring up a window similar to the below, I have done a full test with adding 4 + 5 = 9

Testing the first web service
Testing the first web service