ASMX Web Service - returns a custom class with properties

Hey, I'm trying to return a custom class from a web method. A class has properties and / or methods. Given the following web method:

[WebMethod]  
public List<MenuItem> GetMenu()  
{  
    List<MenuItem> menuItemList = new List<MenuItem>();  
    menuItemList.Add(new MenuItem());  
    menuItemList.Add(new MenuItem());  
    menuItemList.Add(new MenuItem());  
    return menuItemList;  
}

Now suppose this web service is consumed by adding a web link to a newly created console application. To check it, use the following code:

public void TestGetMenu()  
{  
    MenuService service = new MenuService.MenuService();  
    service.MenuItem[] menuItemList = service.GetMenu();  
    for (int i = 0; i < menuItemList.Length; i++)  
    {  
        Console.WriteLine(menuItemList[i].name);  
    }  
    Console.ReadKey();  
}  

First of all, this does not work if the MenuItem class contains properties ... Also, if the MenuItem class contains a method, the web method call is not interrupted, but the method is not in the generated proxy class .. for example: menuItemList [i] .getName ( ) does not exist. What for? What am I missing?

//This works  
public class MenuItem  
{  
    public string name;  
    public MenuItem()  
    {  
        name = "pizza";  
    }  
}



//This crashes / doesnt work  
public class MenuItem  
{  
    private string name;  
    public MenuItem()  
    {  
        name = "pizza";  
    }  
    public string Name  
    {  
        get { return name; }  
        set { name = value; }  
    }  
}



//This successfully calls web method, but the method does not exist during test  
public class MenuItem  
{  
    private string name;  
    public MenuItem()  
    {  
        name = "pizza";  
    }  
    public string getName()  
    {  
        return name;  
    }  
}
+5
source share
2

, , , MenuItem , client , MenuItem.

:

[Serializable]
public class MenuItem
{
   private string name;

   public MenuItem()
   {
      name = "pizza";
   }

   public string Name
   {
      get {
         return name;
      }
      set {
         name = value;
      }
   }

}
+6
  • , .
  • . ? 2. , .
0

All Articles