Click here to Skip to main content
15,884,986 members
Articles / Web Development / ASP.NET
Tip/Trick

Usage of multiple Web Services in a single web application

Rate me:
Please Sign up or sign in to vote.
4.40/5 (3 votes)
25 Sep 2012CPOL3 min read 34.3K   283   8   16
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.

C#
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.

C#
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.

C#
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:

C#
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:

C#
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)


Written By
Web Developer Essential Solve LLC
India India
I am flexible, responsive, creative and enthusiastic with ability to manage multiple initiatives with deadline. I have willingness to pick up and develop new skills and ability to balance a number of conflicting priorities and make decisions. I am results oriented - focused on productive and high-yield activities.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Sandesh M Patil27-Sep-12 1:46
Sandesh M Patil27-Sep-12 1:46 
GeneralRe: My vote of 4 Pin
Sonali Agarwal27-Sep-12 23:08
Sonali Agarwal27-Sep-12 23:08 
GeneralRe: My vote of 4 Pin
Sandesh M Patil30-Sep-12 23:22
Sandesh M Patil30-Sep-12 23:22 
GeneralRe: My vote of 4 Pin
Sonali Agarwal1-Oct-12 5:38
Sonali Agarwal1-Oct-12 5:38 
GeneralRe: My vote of 4 Pin
Sandesh M Patil1-Oct-12 6:06
Sandesh M Patil1-Oct-12 6:06 
GeneralMy vote of 5 Pin
bbirajdar25-Sep-12 8:47
bbirajdar25-Sep-12 8:47 
GeneralRe: My vote of 5 Pin
Sonali Agarwal27-Sep-12 23:05
Sonali Agarwal27-Sep-12 23:05 
GeneralMy vote of 1 Pin
bbirajdar25-Sep-12 5:05
bbirajdar25-Sep-12 5:05 
GeneralRe: My vote of 1 Pin
Sandeep Mewara25-Sep-12 6:49
mveSandeep Mewara25-Sep-12 6:49 
GeneralRe: My vote of 1 Pin
bbirajdar25-Sep-12 8:47
bbirajdar25-Sep-12 8:47 
GeneralRe: My vote of 1 Pin
Sandeep Mewara25-Sep-12 8:55
mveSandeep Mewara25-Sep-12 8:55 
GeneralRe: My vote of 1 Pin
Sonali Agarwal27-Sep-12 23:07
Sonali Agarwal27-Sep-12 23:07 
GeneralRe: My vote of 1 Pin
Sonali Agarwal27-Sep-12 23:06
Sonali Agarwal27-Sep-12 23:06 
Thanks
GeneralRe: My vote of 1 Pin
Sonali Agarwal27-Sep-12 23:05
Sonali Agarwal27-Sep-12 23:05 
SuggestionNot an article Pin
bbirajdar25-Sep-12 5:05
bbirajdar25-Sep-12 5:05 
GeneralRe: Not an article Pin
Sonali Agarwal25-Sep-12 5:32
Sonali Agarwal25-Sep-12 5:32 

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

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