开发者

Deploying Silverlight WCF service to IIS server gives error "Consider marking with DataContractAttribute"

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 D开发者_JAVA技巧ataContractAttribute 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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜