Home > DeveloperSection > Forums > Is it possible to iterate a loop in an Asp.Net web service
Sanjay Sharma
Sanjay Sharma

Total Post:45

Points:315
Posted on    December-11-2014 12:31 AM

 ASP.Net C#  Web Services 
Ratings:


 2 Reply(s)
 700  View(s)
Rate this:

I have a console application in which I am using an asp.net webservice.Also,I had declared a display method in my service which accepts and iterates a string array.But,when I executed the console application it showed an empty console window.Why the array contents are not being displayed?

 

Code 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]);

        }

    }

}



David Miller

Total Post:30

Points:210
Posted on    December-11-2014 6:34 AM

You probably need to use  System.Diagnostics.Debug.WriteLine(...) instead of Console.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 using string.Join

 

public string display(string[] arr)

{

    return Join(",", arr)

}


Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    December-11-2014 7:04 AM

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

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick