9

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?

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
  • 1
    Custom Attributes are not in the process of create a new class instance. – Fals Nov 18 '13 at 18:30
  • Ok fair enough but then what is the correct approach to avoid this problem. Surely others have tried to create cross project compatible Portable Class Libraries? – Maxim Gershkovich Nov 19 '13 at 06:28

5 Answers5

5

Unfortunately DataAnnotations is not currently portable. While a bit complicated, you can probably work around that by writing your own DataAnnotation attributes in a PCL, and creating an assembly with the same name for .NET Framework projects which type-forwards the attributes to the "real" versions. See this answer for some more details on this.

Community
  • 1
  • 1
Daniel Plaisted
  • 16,674
  • 4
  • 44
  • 56
  • Hi Daniel, thank you for the answer but it appears that there is a portable version of the DataAnnotations attributes that can be referenced via the Silverlight assemblies. – Maxim Gershkovich Nov 20 '13 at 07:31
5

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 .

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
1

Data annotations is supported in certain PCL profiles.

Supported profiles:

  • .NET 4.0.3 and up
  • Windows Store 8 and up
  • Silverlight 4 and up

Most notably, the latest Windows Phone is not supported (8.1 at the time).

See full PCL feature table in: http://msdn.microsoft.com/en-us/library/gg597391%28v=vs.110%29.aspx

angularsen
  • 8,160
  • 1
  • 69
  • 83
  • 1
    Why on earth do Microsoft insist on making these things so obscured on their own platforms? – Jammer Jun 23 '15 at 11:02
0

1) The process of create a new class instance doesn't involve read custom attributes, that are loaded by reflection.

2) The System.ComponentModel.DataAnnotations is exclusive for ASP.NET

The System.ComponentModel.DataAnnotations namespace provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.

Fals
  • 6,813
  • 4
  • 23
  • 43
0

The portable version of System.ComponentModel.DataAnnotations seems incomplete (eg no MaxLengthAttribute).

There is this library:

https://github.com/ryanhorath/PortableDataAnnotations:

Install-Package Portable.DataAnnotations

Your PCL needs to target Silverlight 8, otherwise you will get multiple class definition errors.

Michael Ribbons
  • 1,753
  • 1
  • 16
  • 26