Sharing assemblies using Portable Class Library with DataAnnotations
-
02-01-2020 - |
Question
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?
Solution 2
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 .
OTHER TIPS
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.
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
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.
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.