views:

311

answers:

2

I have a Silverlight application with a Silverlight-enabled WCF service. The service passes along a small POCO class with a few string properties, and a List<> of an enum defined in the class. Everything works fine when running using the ASP.NET development server, but when I move the service over to an IIS server (Windows 2003) I get the following error when I try to browse the .svc file:

Type 'MyProject.Web.MyClass' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.

Even though it's working development side, I've tried adding the decorations... but so far without effect.

Any ideas as to what might be causing this difference of outcomes between development workstation and server?

+1  A: 

Make sure that (1) the .NET Framework 3.5 SP1 is installed on the server and (2) that the website is running in ASP.Net 2.0 mode and not 1.1 mode.

The Web Platform Installer is an easy way to install the updated framework if it isn't already installed.

Bryant
Thanks. It was SP1 for .NET Framework 3.5 that was needed. From the MSDN site about SP1 benefits - "WCF improvements that give developers more control over the way they access data and services Streamlined installation experience for client applications"
Traples
+1  A: 

Your data elements (POCO classes) ought to be marked as DataContracts for WCF, so that WCF knows explicitly what it'll need to serialize to send across the wire.

Contrary to the Xml Serializer, the DataContractSerializer in WCF uses an "opt-in" model - only things that you explicitly mark as [DataContract] and [DataMember] will be serialized - anything else will be ignored.

[DataContract]
class YourPocoClass
{
   [DataMember]
   private int _ID;

   [DataMember]
   string CustomerName { get; set; } 

   public decimal CustomerOrderAmount { get; set; } 
}

In this example, from YourPocoClass, you'll have both the _ID field and the CustomerName property be serialized into the WCF message - but the CustomerOrderAmount will not be serialized - public or not.

So, best practice is: explicitly mark all your complex types that you need to send around in WCF with [DataContract] (for the class) and [DataMember] (for each member to be sent across inside that class).

Marc

marc_s
Why does is serialize just fine in development without decorations?
Traples
That's a "simplification" that's been introduced, but it seems to work only half of the time. Do it explicitly - it's easier and states your intent much more clearly.
marc_s
+1 Marc, thank you very much for your help. I gave the accepted answer to Bryant because he answered very specifically the cause of the problem I encountered, but I very much appreciate your advice and clarity on best practices. Thanks!
Traples