1

I wonder how to return a list of objects instead of a list of strings?

Returning a list of strings works, but when I replace everything by an object it fails..

Here's my Web Service

[WebMethod]
 public List<OpProduct> SearchProduct(string name) {
   BLProduct blProduct = new BLProduct();
   List<OpProduct> result = byproduct.SearchProducts(name);

   return result;
}

And here's my Business Logic Layer

public List<OpProduct> SearchProducts(string name)
{
  List<OpProduct> result = (from item in db.OpProducts where item.Name.StartsWith(name) select item).ToList<OpProduct>();

  return result;
}

And here's my error message (sorry, could not translate this)

System.InvalidOperationException: Er is een fout opgetreden bij het genereren van het XML-document. ---> System.InvalidOperationException: Er is een kringverwijzing aangetroffen tijdens het toepassen van serialisatie op een object van het type OpProduct. bij System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String name, String ns, Object o, Boolean writePrefixed, XmlSerializerNamespaces xmlns) bij Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_OpProduct(String n, String ns, OpProduct o, Boolean isNullable, Boolean needType)
bij Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_OpGeheugen(String n, String ns, OpGeheugen o, Boolean isNullable, Boolean needType)
bij Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_OpProduct(String n, String ns, OpProduct o, Boolean isNullable, Boolean needType)
bij Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write10_ArrayOfOpProduct(Object o) bij Microsoft.Xml.Serialization.GeneratedAssembly.ListOfOpProductSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer) bij System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) --- Einde van intern uitzonderingsstackpad --- bij System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) bij System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces) bij System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue) bij System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) bij System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) bij System.Web.Services.Protocols.WebServiceHandler.Invoke()

Can anyone help me?

Thomas More
  • 443
  • 3
  • 10
  • Your xml serialization fails. You can (and should) test it separately from the actual web method. My bet - either **OpProduct** is not serializable, or some fields contain invalid characters. – user270576 May 12 '14 at 16:57

1 Answers1

1

By default only the system primitive types and arrays of these primitive types (like int, float, double, string) can be used as a return value.

If you want to use your own classes as parameters you have to define them as complex types by using XML definition. Here is a MSDN link about this https://msdn.microsoft.com/en-us/library/aa480498.aspx

Hung Vu
  • 5,624
  • 2
  • 30
  • 27