views:

121

answers:

2

Hi,

I've webservice and WebMethod

[webMethod]
[XMLInclude(typeof(ContractCar[]))
public object GetObjects(Cars[] cars)
{
return Translator.ToObjects(Facade.GetObjects(cars);

}

public static object GetObjects(Cars cars)
{
List<Car>  cars =new List<Country(...fillingcollection)
return cars.ToArray(),
}

public static object ToObjects(object collection)
{
if(collection is Car[])
{
return ConvertModelCarsToContractCars(collection),
}

public ContractCar[]  ConvertModelCarsToContractCars(Cars[] collection)
{
...there is rewriting pool...
}

And I get exception at side of client:

There was an error generating the XML document.

I'm using this function to check collection which I would send to the client and it works.

 public static void SerializeContainer(object obj)
        {
            try
            {
                // Make sure even the construsctor runs inside a
                // try-catch block
                XmlSerializer ser = new XmlSerializer(typeof(object));

                TextWriter w = new StreamWriter(@"c:\list.xml");


              ser.Serialize(w, obj);
                w.Close();

            }
            catch (Exception ex)
            {
                DumpException(ex);
            }
        }

Interesting is when collection has only One element [webmethod] works fine, but when is more it brokes

A: 

You need to indicate to the serializer and in the WSDL that a Cars can be a ModelCar using the XmlIncludeAttribute:

[WebMethod]
[XmlInclude(typeof(ModelCar))]
public object GetObjects(Cars[] cars)
{
    return Translator.ToObjects(Facade.GetObjects(cars);
}

You need to indicate what is the possible type that you are returning from the method. As in the method signature you are returning object, the serializer doesn't know about the actual type you are returning at runtime.

And in your serialization example:

XmlSerializer ser = new XmlSerializer(
    typeof(object), 
    new Type[] { 
        // List all the possible types here that you are using
        typeof(Foo),
        typeof(Bar)
    }
);
Darin Dimitrov
I have still error:ModelCar[] may not be used in this context…
A: 

i change object into objects[]