Silverlight 2 Beta 2 – ASMX Services & XmlSerializer

It wasn’t clear to me from reading the Silverlight SDK docs whether or not you could call ASMX services from Silverlight 2 Beta 2 if calling those services meant involing the XmlSerializer.

Silverlight is using WCF to call services and WCF usually uses the DataContractSerializer. This is a clever serializer but one of the things that it doesn’t do ( which I never understood the rationale for ) is serialize XML attributes. It is as element-centric as a serializer can be.

So, I read in the Silverlight SDK docs;

Using XmlSerializer

  • Silverlight 2 Beta 2 supports XmlSerializer. The equivalent serialization support in WCF is described in XML and SOAP Serialization.
  • In Silverlight 2 Beta 2, generating SOAP messages as described in the preceding document is not supported.

and I didn’t understand whether those 2 sentences combined to say “Supported” or “Not Supported”. Having read it twice, I think what it means is that the XmlSerializer bits are supported but the bits referenced here are not. That’s what I think it means but I can’t be 100% sure.

And so I built a quick ASMX service as in;

public class Operand
{
  [System.Xml.Serialization.XmlAttribute]
  public int X { get; set; }

  [System.Xml.Serialization.XmlAttribute]
  public int Y { get; set; }
}

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
  public WebService()
  {
  }
  [WebMethod]
  public int Add(Operand op)
  {
    return (op.X + op.Y);
  }
}

and added a reference to it from my Silverlight 2 Beta 2 project and the proxy generation seemed to work ok and, specifically, I could see that the generated proxy code is using the XmlSerializer;

namespace SilverlightApplication3.ProxyCode {
    
    
    [System.ServiceModel.ServiceContractAttribute()]
    public interface WebServiceSoap {
        
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/Add", ReplyAction="*")]
        [System.ServiceModel.XmlSerializerFormatAttribute()]
        System.IAsyncResult BeginAdd(SilverlightApplication3.ProxyCode.Operand op, System.AsyncCallback callback, object asyncState);
        
        int EndAdd(System.IAsyncResult result);
    }

and then I called the service using the proxy and it all seemed to work fine. Note that I had to manually add a reference to System.Xml.Serialization.dll in order to get the client to compile but that’s fine.

So…”seems to work”.