When in doubt, turn on Tracing
One of my favorite posts that I've done is "The Internet is not a black box. Look inside."
I really want to encourage folks to look inside "black boxes" - or more accurately, what they perceive as black boxes - in order to solve their issues.
I got a great question from blog reader Eddie F. He also knows I don't like to waste keystrokes so he mentioned that I could blog the result if I answered his question. Hat tip to Eddie.
I have a case with an Entity Framework 5.0 entity that refuses to "PUT" to my ASP.NET WebApi controller (all .NET 4.5 RTM) if I add the [Required] data annotation to a property on my entity class which I find really bizarre.
So Eddie has an entity with the [Required] attribute on a property. When he PUTS (not POSTS) he gets an error:
var response = client.PutAsJsonAsync("/api/MagicPants/Update", entry).Result;
//HERE IS THE POINT OF FAILURE when the [Required] attribute
// is added to the EF poco type PortfolioEntry.
If he removes [Required] it works. If he uses any other DataAnnotation like RangeValidation, it also works.
Eddie points out that he's Googled (with Bing) all over and found nothing. He has asked lots of friends and StackOverflow before asking me. Very thoughtful.
I replied with this to get more info.
What happens underneath? You're describing an interaction with a black box without pushing under....what does tracing say? What does Fiddler say?
That question, along with your provided hyperlink to Henrik’s blog (bookmark… check) allowed me to discover web api tracing. Genius! I feel like I’ve been riding in the car, but now I feel like I’ve taken the wheel. Thank you!
As an ASP.NET guy I take this to mean that Tracing isn't easily discoverable in the product. However, once you know it exists (through all of ASP.NET) things get interesting. You have to know to ask about Tracing (and hence, Google) before you can unlock its power. Most products have tracing or logging or "turn diagnostics up to 11."
Tracing was very clear about the problem:iisexpress.exe Error: 0 : Operation=FormatterParameterBinding.ExecuteBindingAsync,
Property 'OrderOfMagnitude' on type 'TFSPortfolio.Models.PortfolioEntry' is invalid.
Value-typed properties marked as [Required] must also be marked with
[DataMember(IsRequired=true)] to be recognized as required.
Consider attributing the declaring type with [DataContract]
and the property with [DataMember(IsRequired=true)].
at System.Web.Http.Validation.Validators.ErrorModelValidator.Validate(ModelMetadata metadata, Object container)
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
I had the DataMember attribute on the property but not the IsRequired=True
So I add the attribute and bingo!
Even better, Eddie knows about T4 and is code-generating his Entities so he adds:
I’ll have to make another minor tweak to the T4 template for this attribute to fix this globally… gotta love T4 for this job.
A great result via a clarifying question about a question. When in doubt, turn on tracing!