---
title: "Is it possible to iterate a loop in an Asp.Net web service"  
description: "Is it possible to iterate a loop in an Asp.Net web service"  
author: "Anonymous User"  
published: 2014-12-11  
updated: 2014-12-11  
canonical: https://www.mindstick.com/forum/12782/is-it-possible-to-iterate-a-loop-in-an-asp-dot-net-web-service  
category: "asp.net"  
tags: ["c#", "web services"]  
reading_time: 2 minutes  

---

# Is it possible to iterate a loop in an Asp.Net web service

I have a [console application](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) in which I am using an [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [webservice](https://www.mindstick.com/forum/166/webservice-method).Also,I had declared a display method in my [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) which accepts and iterates a [string array](https://www.mindstick.com/forum/12803/check-for-any-item-in-string-array).But,when I executed the console application it showed an empty console window.Why the array contents are not being displayed?

[Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that uses the below service

```
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace usingWebServiceExample{    class Program    {        static void Main(string[] args)        {            string[] arr = new string[3] {"harry","ronn","sheldon" };            localhost.Service service = new localhost.Service();            service.display(arr);            Console.Read();        }    }}
```

## Service Code:

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]// 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 Service : System.Web.Services.WebService{    public Service()    {    }     [WebMethod]    public void display(string[] arr)    {        for (int i = 0; i < arr.Length; i++)        {            Console.WriteLine(arr[i]);        }    }}
```

## Replies

### Reply by Norman Reedus

You need to either return an array of string to print to the client, or use duplex binding and implement a callback on client side to print a message to console.

Here is a sample duplex callback implementation: Server:

```
[ServiceContract(CallbackContract=typeof(IConsoleCallback))]public interface IService1{     [OperationContract(IsOneWay = true)]    void Display(string[] arr);} public interface IConsoleCallback{    [OperationContract(IsOneWay = true)]    void WriteLine(string message);} public class Service1 : IService1{    public void Display(string[] arr)    {        var callback = OperationContext.Current.GetCallbackChannel<IConsoleCallback>();         for (int i = 0; i < arr.Length; i++)        {            callback.WriteLine(arr[i]);        }    }}
```

## Server web.config:

```
<protocolMapping>    <add binding="wsDualHttpBinding" scheme="http" />    <add binding="wsDualHttpBinding" scheme="https" /></protocolMapping>   
```

## Client:

```
class Program{    class ConsoleCallback : ServiceReference1.IService1Callback    {        public void WriteLine(string message)        {            Console.WriteLine(message);        }    }      static void Main(string[] args)    {        string[] arr = new string[3] { "harry", "ronn", "sheldon" };        var service = new ServiceReference1.Service1Client(new InstanceContext(new ConsoleCallback()));        service.Display(arr);        Console.Read();    }}
```

### Reply by David Miller

You probably need to use System.Diagnostics.Debug.WriteLine(...) instead of [Console](https://www.mindstick.com/blog/210/the-console-class).WriteLine() for asp.net and you would be able to see the results in output window in the visual studio.

Writing log might does not make much sense here, you can return the [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) using [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp).Join

```
public string display(string[] arr){    return Join(",", arr)}
```


---

Original Source: https://www.mindstick.com/forum/12782/is-it-possible-to-iterate-a-loop-in-an-asp-dot-net-web-service

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
