Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing assemblies using Portable Class Library with DataAnnotations

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?

like image 891
Maxim Gershkovich Avatar asked Nov 18 '13 18:11

Maxim Gershkovich


People also ask

Is there a portable version of dataannotation?

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.

Can I use the same class library projects on different platforms?

This means that each platform can only use class libraries that are targeted to the same profile so they would appear to require separate class library projects for each platform. There are three major approaches to code sharing that address this problem: .NET Standard projects, Shared Asset Projects, and Portable Class Library (PCL) projects.

How do I add a portable class library to my solution?

Adding a Portable Class Library to your solution is very similar to adding a regular Library project. In the New Project dialog select the Multiplatform > Library > Portable Library option: When a PCL is created in Visual Studio for Mac it is automatically configured with a Profile that works for Xamarin.iOS and Xamarin.Android.

Which portable class libraries are not available in mono for Android?

Community.CsharpSqlite.WP7). The Portable Class Library subset may not include classes that would otherwise be available in both MonoTouch and Mono for Android (such as DllImport or System.IO.File). Portable Class Libraries have been deprecated in the latest version of Visual Studio, and .NET Standard Libraries are recommended instead.


2 Answers

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.

like image 57
Daniel Plaisted Avatar answered Oct 06 '22 12:10

Daniel Plaisted


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 .

like image 45
Maxim Gershkovich Avatar answered Oct 06 '22 13:10

Maxim Gershkovich