Sharing assemblies using Portable Class Library with DataAnnotations

asp.net-mvc, c#, data-annotations, portable-class-library, windows-phone-8

Solution

OK, so it turns out that my original assumptions were completely wrong. You absolutely can reference the `System.ComponentModel.DataAnnotations` namespace from a Windows Phone 8 project.

Basically it comes down to counterintuatively referencing the silverlight version of the dll which can be located in `C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.ComponentModel.DataAnnotations.dll`

For more information about how to build portable class libraries I suggest referring to this article .

Problem

I have created a portable class library called `DataContracts` that contains my projects `Messages` and `Views`. Standard stuff like `GetStockItemByIDRequest` and `StockView` are contained in it. The problem lies when I attempt to add `DataAnnotations` by using `System.ComponentModel.DataAnnotations` for some of my `Views` as such. ``` [DataContract] public class StockView { [Required] [DataMember] public Guid StockID { get; set; } [Required] [DataMember] public string Name { get; set; } } ``` I can successfully add the `System.ComponentModel.DataAnnotations` to my Portable Class Library project and can successfully reference it in my Windows Phone 8 app and can even create a new instance of my view as such `StockView View = new StockView();` within my Windows Phone App BUT if I try to use either `Newtonsoft.Json` or `System.Net.Http.HttpClient` by doing something like ``` HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("http://myservice.com"); T result = await response.Content.ReadAsAsync<T>(); ``` OR ``` T result = await Newtonsoft.Json.JsonConvert.DeserializeObjectAsync<T>("{}"); ``` ie: where deserialization is involved... I am faced with the error `Could not load file or assembly 'System.ComponentModel.DataAnnotations, Version=2.0.5.0'`. Which I assume is because it doesn't appear that `System.ComponentModel.DataAnnotations` is supported in Windows Phone 8 (but then why can I add it as a reference to my PCL?). So my questions are, why isn't this error invoked when I create a new instance of these classes directly and secondly how do I work around this?

Original source

Related problems