Reputation: 2784
I have to implement a simple conventional web service in VS2008 C#, which would receive an Order class object, which contains a list of Product class objects. The problem arises when I create an instance of an Order class object on the client. It is declared in the proxy and does not contain any methods, thus I have no opportunity to add Products, as List of Products is now simply a System.Array.
One method which works fine is to manually serialize into XML on the client and de-serialize in the web service. This way I would be using same class declaration which I isolated into a DLL shared between web service and client application. However, I am being told that it is possible to avoid using a shared DLL and manual serialization althogether, but I just don't understand how, i.e. how do I need to declare/define Order class and where in order for it to have methods on the client?
Currently the classes are defined as follows in the shared DLL:
namespace MyNS
{
[Serializable]
public class ProductInfo {
public Int32 ProductID;
public Int32 Quantity;
public decimal CurrPrice;
public VE_ProductInfo() {
ProductID = 0;
Quantity = 0;
CPrice = 0;
}
public ProductInfo(Int32 pId, Int32 quant, decimal cprice)
{
ProductID = pId;
Quantity = quant;
CPrice = cprice;
}
}
[Serializable]
public class OrderInfo
{
public Int32 CustomerId = 0;
public OrderInfo () {
Items = new List<ProductInfo>();
}
[XmlArray]
public List<ProductInfo> Items {get; set;}
public string SpecialInstructions;
public void AddProduct(ProductInfo pi)
{
Items.Add(pi);
}
}
}
This class is then used in a web service method as follows:
public bool CreateOrder(OrderInfo Order) {
// some code
}
And it is called as follows from the client:
WebService.MyWebService s;
WebService.OrderInfo o = new WebService.OrderInfo();
o.CustomerId = 1;
o.Items.Add(new ProductInfo(2, 4));
o.Items.Add(new ProductInfo(1, 2, 3.95m));
checkBox1.Checked = s.CreateOrder(o);
Upvotes: 0
Views: 3275
Reputation: 713
If you provide a default constructor and field initializers for the classes shared between the application and web service, then you can put the classes in a separate library project that both projects reference. When you add the web service reference to the application, it will generate a few proxy classes that match the data "shape" of your object, sans methods. You can delete those classes (I don't remember exactly where they are located) and add a using statement to the web service proxy class file itself to import your shared classes and they will work appropriately.
Upvotes: 1
Reputation: 1606
If your web service is a WCF one and you created a proxy for it by adding a web reference you should be able to change it by editing the refernce in Visual Studio. Right Click on it and select "Configure Service Reference" then choose a Collection type of List.
Upvotes: 1