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.
He says:
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?
http://blogs.msdn.com/b/henrikn/archive/2012/08/15/asp-net-web-api-released-and-a-preview-of-what-s-next.aspx
Eddie replied:
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."
Eddie said:
Tracing was very clear about the problem:
iisexpress.exe Error: 0 : Operation=FormatterParameterBinding.ExecuteBindingAsync,
Exception=System.InvalidOperationException:
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!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
http://youtu.be/iDbyYGrswtg
http://aspnetwebstack.codeplex.com/workitem/270
Steve
Output window helps solve issues such as this one quite often (i.e. WCF initialization failing with 500, output shows all the details).
Not quite politically correct :-)
Great blog, greetings from Poland.
Now with web-API tracing in addition to WPF binding tracing and a nice EF monitor to see the translated SQL execute at runtime I feel like I have a good idea of what is happening under the hood.
I have learned a lesson about reaching in to black box that I often take for granted [.net runtime]. Thanks again!
A related thing that if you think that all is ok you should use a profiler to check if your codes does not something very stupid. The latest release of the Windows Performance Toolkit gives you a system wide profiler with full managed call stacks as well as unmanged code.
WPR and WPA have the potential to take out nearly all commercial profilers on the market. An example analysis can be found here: http://geekswithblogs.net/akraus1/archive/2012/12/28/151662.aspx
I am writing to request that Microsoft add one more additional piece of information, for the hosting providers to list (http://www.microsoft.com/en-us/default.aspx).
- I.e. Dedicated ram allocated to the account/per plan, basic/shared or VPS
Before I end the email, I am happy to find a collective list of hosts in your directory (http://www.microsoft.com/en-us/default.aspx). However, there have been some poor experiences with the hosts especially hosting Orchard. My clients have been complaining about the speed of the orchard site. After posting a questions on codeplex, it turns out that one orchard site at the minimum takes 200 MB (to 250 with plugins/new themes).
http://stackoverflow.com/questions/13437680/how-to-reduce-memory-consumption-for-orchard-cms-site-hosted-on-windows-azure-we
All hosting providers are not equal, and this provides a huge difference in making a decision.
thanks
Amy
But do you have any advice on how to help a senior dev see the importance of tracing (or any logging for that matter). Often I'm met with "We don't need to add logging because it's not in the requirements".
I'm shocked to hear any dev not see the need for basic diagnostics in a mid-large scale code base. I see it as being next to mandatory...
Cheers
Aside from high fructose corn syrup which is known to be harmful, when eating a high fruit diet and keeping to low fat, blood sugar levels are healthy, along with blood test results.
Thanks for such awesome share.
Comments are closed.
Indeed, this is one of the more frustrating things as we learn! Often, when we ask someone "how do I . . ." or "what do I need in order to . . . " what we really mean is "What should I be asking FOR?"
When I first began learning to code a few years back, I drove my IT guy at work nuts (he was very patient and helpful, -much appreciated, Alex!) not because I was unwilling to look underneath, or do my own research, but because, having zero context, I didn't even know what to ask.
It is good to know that even people who know what they are doing run into this problem.
While I have grown exponentially in my ability to suss out my own answers (usually after hours and hours of struggling!), occasionally there comes a point where I know I am missing some key information, but don't know how to ask the question, or where to look to find my way "underneath".
Great post. This is the type of thing which makes it easier for all of us.