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();

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(
<title>ASP Web service consumer</title>
<form runat="server">
      First Number to Add : <asp:TextBox id="number1" runat="server">0</asp:TextBox>
      Second Number To Add :
      <asp:TextBox id="number2" runat="server">0</asp:TextBox>
      Adding result : <asp:Label id="resultLabel" runat="server">Result</asp:Label>
      <asp:Button id="runService" onclick="runWebService_Click" runat="server" Text="Run the Service"></asp:Button>

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.

Leave a Reply

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