Saturday, February 4, 2012

Arrays in .NET Webservices

Web Service
public class Service1 : System.Web.Services.WebService
{
 [WebMethod]
 public int[] sort(int[] arr)
 {
  Array.Sort(arr);
  return arr;
 }
}
Then run the service.
Copy the URL of the WSDL
ex :  http://localhost:4886/Service1.asmx?WSDL

Then create a new desktop or console application. This application will consume the previously written web service.

Right click on the project -> Add service reference -> Give the address of wsdl of our web service -> Change the namespace name for a proper name.

consumer code.

class Program
{
 static void Main(string[] args)
 {
  MyCalcService.Service1SoapClient clie = new MyCalcService.Service1SoapClient();
  MyCalcService.ArrayOfInt x=new MyCalcService.ArrayOfInt();
  x.Add(-1);
  x.Add(76);
  x.Add(9);
  x.Add(-7);

  x = clie.sort(x);
  foreach (int i in x)
  {
   Console.Write(i+ " ");
  }
  Console.ReadKey();
 }
}
Remember that though we used int[] type for our array in web service we cannot use same array type at the client side. We have to use ArrayOfInt type in the service references namespace.

0 comments:

Post a Comment

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis