views:

411

answers:

2

I have a .Net web service with web method which take a Interface object as parameter, whenever I try to access the method I get exception saying : Cannot serialize member Product IProduct because it is an interface.

Any suggestions to solve the problem ??

[WebMethod]
Public double CalculateTotal(IProduct product, int Quantity)
{
  return product.Price * Quantity;
}
+2  A: 

Try adding an XmlInclude attribute to your method:

[WebMethod]
[XmlInclude(typeof(Product))]
Public double CalculateTotal(IProduct product, int Quantity)
{  
    return product.Price * Quantity;
}

Edit

Just incase you are getting confused with my use of the class "Product". Replace this class with whatever class in your assembly that implements IProduct e.g.

[Serializable]
public class Product : IProduct
{
     public Product(string name, double price)
     {
         this.Name = name;
         this.Price = price;
     }

     public string Name { get; private set; }
     public double Price { get; private set; }
}

public interface IProduct
{
    string Name { get; }
    double Price { get; }
}

....

[Web Method]
[XmlInclude(typeof(Product))]
Public double CalculateTotal(IProduct product, int quantity)
{
     return product.Price * quantity;
}

Basically when you pass an interface into a webservice it cannot find any schema for it, hence if you use XmlInclude attribute and pass in the concrete class it will be able to recognise the type.

James
return type is double
Prashant
Not worked for me :(
Prashant
Make sure Product is marked with [Serializable]
James
If I understand this correct Only product can be used as IProduct? what about FooProduct and BarProduct?
Carl Bergquist
I would imagine there would be a base class called Product.
James
OK, late to the thread here, but how is this any advantage over the (abstract?) base class? In fact, if Product is the base class, and there's no difference in the interface between IProduct and Product, it seems like you're adding work for little to no gain.
Harper Shelby
@harper: Wow long time ago, I never actually suggested it's more beneficial than using an abstract class. Using an abstract base class instead of normal class is fine, it just means you would always have to derive from Product (which in this particular scenario would be fine). The above was an example, product could have a hell of a lot more properties/methods so using an interface is just a nicer way of limiting what properties/methods are exposed to the specific methods. Using an interface (even if it mimics the class) makes it easier for changing implementation in the future.
James
@James: But in this case, you always have to inherit from the base class that's mimicking the interface, which seems to negate any benefits that you might gain, since the interface is tied directly to the base class by the XmlInclude.
Harper Shelby
@Harper, there is no right or wrong answer both would be absolutely fine. If the OP has not mentioned *anything* in relation to actually having to always derive from product which is why I used a normal base class `Product` rather than an abstract class.
James
+1  A: 

hi prashant please try to do like this..

In place of this

 [WebMethod]
    Public double CalculateTotal(IProduct product, int Quantity)
    {  
     return product.Price * Quantity;
    }

Just Add an abstract class becoz you need a type to serialize it..

  [Serializable]
  public abstract class ProductAbstract : IProduct
  {
    // define all methods/attributes of interface IProduct here as abstract methods/attributes
  }


    [WebMethod]
    Public double CalculateTotal(ProductAbstract product, int Quantity)
    {  
       return product.Price * Quantity;
    }
Jaswant Agarwal