Click here to Skip to main content
Click here to Skip to main content
Go to top

Usage of multiple Web Services in a single web application

, 25 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Creating a simple Text Editor web application using multiple web services.

Introduction    

The goal of this article is to explore .NET web services. Web services are standardized way of integrating Web-based applications. The program is developed with Microsoft Visual Studio 2010 using C# as the programming language. 

Task: Creating a simple Text Editor web application using multiple web services. 

Background 

This article is written to make you more familiar with the functions of .NET and the usage of multiple Web Services in a single web application. Before attempting to read this article, you should have a basic knowledge of C# as the programming language and use of Microsoft Visual Studio 2010 to create web services. Please note that .NET Framework 4 is used in the sample code. You also need to have Windows 7 installed on your computer. 

Task: A simple Text Editor Web application 

Create a simple Text Editor Web application using Web Form with the following functions (Please note that you have to create a web service for each of these functions): 

  • Word Count: counts all the words in the main text.
  • Word Occurrence Count: counts the number of a specific word occurrence in the main text.
  • Replace Text: replaces the two specified words in the main text.

You should have a drop down list containing the required operations that upon selection of each operation, the relevant text boxes should appear (Please note that you are required to display the identified messages in each case):

  • For the Word Count, only the main text box should appear
  • For the Word Occurrence Count, another text box in addition to the main text box should appear
  • For the Replace Text, two other text boxes in addition to the main text box should appear

You are also required to check all the validations:

  • None of the text boxes should be empty when the relevant operation happens
  • When the Replace Text happens, if the word that has to be replaced did not exist, an error message should be displayed (you can use the Word Count web service for this purpose).

Using the code  

Create a Website named as Editor in VS2010.

Add 3 web services named as textReplace.asmx, WordCounter.asmx, and WordOccurenceCounter.asmx in the website.

The code below is written in textReplace.asmx web service to replace the words in the text editor.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services; 
namespace Editor
{ 
    /// <summary>
    /// Summary description for textReplace
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script,
    // using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class textReplace : System.Web.Services.WebService
    { 
        [WebMethod]
        public string ReplaceWord(string Data, string Match, string Replace)
        {
            int count = 0;
            int i = 0;
            while ((i = Data.ToLower().IndexOf(Match, i)) != -1)
            {
                i += Match.Length;
                count++;                
            }
            Data = Data.ToLower().Replace(Match, Replace);
            return count + "_" + Data; 
        } 
    }
}

The following code below in WordCounter.asmx is written to count the words in the text editor.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Editor 
{ 
    /// <summary>
    /// Summary description for WordCounter
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script,
    // using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WordCounter : System.Web.Services.WebService
    { 
        [WebMethod]
        public int WordCount(string Data)
        {
            string[] DataArray = Data.Split(' ');
            int length = 0;
            for (int i = 0; i < DataArray.Length; i++)
            {
                if (!(DataArray[i].Equals("")))
                    length++;
            }
            return length;
        } 
    } 
}

The code below is written in WordOccurenceCounter to find the occurrence of a word in a text written in the text editor.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Editor
{
    /// <summary>
    /// Summary description for WordOccurenceCounter
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script,
    // using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WordOccurenceCounter : System.Web.Services.WebService
    {
        [WebMethod]
        public int WordOccurence(string Data, string Match)
        {
            int count = 0;
            int i = 0;
            while ((i = Data.ToLower().IndexOf(Match, i)) != -1)
            {
                i += Match.Length;
                count++;
            } 
            return count;
        } 
    } 
}

After creating the 3 web services, compile the project.

To use these web services in the current project, you need to add their reference in the project. For this right click the project in Solution Explorer, add Service reference. Then add the reference of the above services.

In the Default.aspx page, I have created a method ReplaceOccurance() in which I am using the reference of web service 'textReplace' .

The is shown below:

private void ReplaceOccurence()
{
    lblSearchWord.Visible = true; lblReplaceWord.Visible = true;
    txtSearchWord.Visible = true; txtReplaceWord.Visible = true;
    textReplace trep = new textReplace();
    string ReplaceText = trep.ReplaceWord(txtEdit.Text, txtSearchWord.Text, txtReplaceWord.Text);
    string NoOfOccurence = ReplaceText.Substring(0, ReplaceText.IndexOf("_"));
    if (NoOfOccurence.Equals("0"))
    {
        lblReplaceWordCount.Text = txtSearchWord.Text + " not found in the text.";
    }
    else
    {
        ReplaceText = ReplaceText.Substring(ReplaceText.IndexOf("_") + 1, 
             ReplaceText.Length - (ReplaceText.IndexOf("_") + 1));
        if (NoOfOccurence.Equals("1"))
            lblReplaceWordCount.Text = NoOfOccurence + " word replaced";
        else
            lblReplaceWordCount.Text = NoOfOccurence + " words replaced";
        txtEdit.Text = ReplaceText;
    } 
    lblSearchWordCount.Visible = false; lblSearchWordCount.Text = string.Empty;
    lblSearchWord.Style.Add("display", "block");
    txtSearchWord.Style.Add("display", "block");
    lblReplaceWord.Style.Add("display", "block");
    txtReplaceWord.Style.Add("display", "block");
}

Similarly I have created 2 other methods namely CountOccurence() and CountWords() which are using the reference of web services WordOccurenceCounter and  WordCounter.

Methods are shown below:

private void CountOccurence()
{
    WordOccurenceCounter woc = new WordOccurenceCounter();
    int Count = woc.WordOccurence(txtEdit.Text, txtSearchWord.Text);
    if (Count == 0)
    {
        lblSearchWordCount.Text = txtSearchWord.Text + " not found in the text.";
        lblSearchWordCount.Visible = true;
        lblSearchWordCount.ForeColor = System.Drawing.Color.Red;
    }
    else if (Count > 0) 
    {
        lblSearchWordCount.Text = Convert.ToString(Count) + " matches found";
        lblSearchWordCount.Visible = true;
        lblSearchWordCount.ForeColor = System.Drawing.Color.Black;
    }
    lblSearchWord.Style.Add("display", "block");
    txtSearchWord.Style.Add("display", "block");
}

private void CountWords()
{ 
    WordCounter wc = new WordCounter();
    int Count = wc.WordCount(txtEdit.Text);
    lblWordCount.Visible = true;
    lblWordCount.Text = "Total Word in text: " + Count.ToString();
}

Run the program to see the efficient working of web services.

Points of Interest 

This article gives you a basic knowledge of creating web services in a web application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sonali Agarwal
Web Developer Essential Solve LLC
India India
I am a Web developer, Project Manager. I am:
- Flexible, responsive, creative and enthusiastic.
- Ability to manage multiple initiatives with deadlines
- Willingness to pick up and develop new skills.
- Ability to balance a number of conflicting priorities and make decisions.
- Results oriented - focused on productive and high-yield activities.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 PinmemberSChristmas27-Sep-12 1:46 
GeneralRe: My vote of 4 PinmemberSonali Agarwal27-Sep-12 23:08 
GeneralRe: My vote of 4 PinmemberSChristmas30-Sep-12 23:22 
GeneralRe: My vote of 4 PinmemberSonali Agarwal1-Oct-12 5:38 
GeneralRe: My vote of 4 PinmemberSChristmas1-Oct-12 6:06 
GeneralMy vote of 5 [modified] Pinmemberaspnet_regiis25-Sep-12 8:47 
GeneralRe: My vote of 5 PinmemberSonali Agarwal27-Sep-12 23:05 
GeneralMy vote of 1 Pinmemberaspnet_regiis25-Sep-12 5:05 
GeneralRe: My vote of 1 PinmvpSandeep Mewara25-Sep-12 6:49 
GeneralRe: My vote of 1 Pinmemberaspnet_regiis25-Sep-12 8:47 
GeneralRe: My vote of 1 PinmvpSandeep Mewara25-Sep-12 8:55 
GeneralRe: My vote of 1 PinmemberSonali Agarwal27-Sep-12 23:07 
GeneralRe: My vote of 1 PinmemberSonali Agarwal27-Sep-12 23:06 
GeneralRe: My vote of 1 PinmemberSonali Agarwal27-Sep-12 23:05 
SuggestionNot an article Pinmemberaspnet_regiis25-Sep-12 5:05 
GeneralRe: Not an article PinmemberSonali Agarwal25-Sep-12 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 25 Sep 2012
Article Copyright 2012 by Sonali Agarwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid