Search code examples
c#web-servicesservice-reference

Webreference vs servicereference. Only one works ? Serialization?


I've got two applications.

One uses webreference to my webservice, and second use servicereference to my webservice.

There is metohod which I'm invoking:

[WebMethod]
public Car[] GetCars(string carCode)
{
 Cars[] cars= ModelToContract.ToCars(MyFacade.GetCars(carCode);
return cars;
}

Car has two pools:

string Code {get;set;}
CarType Type {get;set;}

public enum CarType
{
Van=0,
Pickup=1
}

I'm debuging this webMethod, and...

at the end webservice throw good collection of cars, which has one car: code="bmw",Type.Van

But... Application with webrefence receives the same collection and application with servicereference gets collection, where field code is null...

Invoking servicereference:

MyService myService=new MyService()
Cars[] cars= client.GetCars(carcode);

Invoking webservice:

MyService.MyServiceSoapClient client = new MyServiceS.MyServiceSoapClient();
            Cars[] cars= client.GetCars(carcode);

Solution

  • Hum it's a serialization problem.

    Your model should be like this :

    [Serializable]
    public class Car
    {
        [XmlElement]
        string Code {get;set;}
    
        [XmlElement]
        CarType Type {get;set;}
    }
    

    You may ensure the return type too :

    MyService myService=new MyService()
    MyService.Car[] cars= myService.GetCars(carcode);
    

    -

    MyServiceS.MyServiceSoapClient client = new MyServiceS.MyServiceSoapClient();
    MyServiceS.Car[] cars= client.GetCars(carcode);