Posts Tagged ‘Web Service’

Testing your web service with SOAP UI

Tuesday, December 16th, 2008

It is really fun! I hope you’ll find this interesting. The subject of this post is how to test your web service or some wcf service. You only need WSDL and SoapUI. Foe this demonstration we’ll use this service created in Windows Communication Foundation (WCF). If you start your Service you, you will see something like this in your web browser. In my case URL address is: http://localhost:3363/WebServiceHost/Service.svc

“You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

svcutil.exe http://localhost:3363/WebServiceHost/Service.svc?wsdl”

I need only this wsdl so I’ll click on this link: “http://localhost:3363/WebServiceHost/Service.svc?wsdl”.
That is the first step, remember location of your wsdl. Then start SoaupUI applcation, and under projects click “New WSDL project”.

Enter your project name, and enter URL of your WSDL and press OK.
Here is preview:

Now we will test method “FunctionAdd”. Click on Request 1 under the FunctionAdd and populate xml request.
Here is XML request:


And here is response



That is all. I find this tool “SoapUI” very interesting at all.

ASP.NET web service

Tuesday, December 9th, 2008

Creating web service in ASP.NET using Visual studio 2005 is very easy. Follow this steps:
Open Visual Studio and click New Web Site, and select ASP.NET web service.

After a second your project is created. You will find file named: “Service.cs” in your App_Code folder.
There is already default web methid implemented “Hello World”. Here is how it looks like:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
}

we will change this method “HelloWorld” with an other method “IntigerAddition”. So copy/paste this code into Service.cs file.

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public Int32 IntigerAddition(int a, int b)
    {
        return a + b;
    }
}

It is always useful to try to build your code. If everything is fine, let’s see how it works.
Right click on Service.asmx file in solution explorer and select “View in browser”. The same is if you try to click on “F5″. If there is no web.config file, you’ll be asked to put it into solution.
when web page is opened, you will see listed all services you have. For now it is only “Integer Addition” and if you click on it, you will see SOAP 1.1 and SOAP 1.2 request and response. Also you can test your web service method there if you want.


Now, get us back to solution explorer and create one web page to test this web service.
In Solution Explorer right click on solution and select Add new web site. This is image preview how to do this:

ASP.NET Web service - ASMX

ASP.NET Web service - ASMX




I’ll call this new web site project: “DemoClientForWebService”.
In your new web site project, ther is a file Default.aspx. Open it and add 2 text boxes, one Button and one more text box. Like this:

Now, we need to add web reference in our web site from web service project. Right click on web site project and select Add Web Reference.
Right click on web project and select “Add web reference”.



Then you need to specify web reference. Select “Web service in this solution”.


Then select your reference and click on button “Add reference”.



Now let’s use this reference and connect our application to web service.
Here is the code for Default.aspx page. This is code behind, file: Deafult.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using localhost; //this is how we called our web service

public partial class _Default : System.Web.UI.Page
{
    localhost.Service myWeb;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myWeb = new Service();
        }
    }
    protected void bntGo_Click(object sender, EventArgs e)
    {
        txtResult.Text = myWeb.IntigerAddition(
                Convert.ToInt32(txtAdd1.Text),
                Convert.ToInt32(txtAdd2.Text)
                ).ToString();
    }
}

And now, let’s do some testing :)
But first be carefull. You must start your web service first.
It must be running when you try to call it from your web apps.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in