Scott Hanselman

When in doubt, turn on Tracing

January 1, '13 Comments [12] Posted in ASP.NET | ASP.NET Web API
Sponsored By

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. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Tuesday, January 01, 2013 1:16:48 AM UTC
"You have to know to ask about Tracing (and hence, Google) before you can unlock its power."

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.

Tuesday, January 01, 2013 11:01:30 AM UTC
Hi. Your mention of "black box" reminded me if this brilliant IT Crowd clip which might make you smile

http://youtu.be/iDbyYGrswtg
Shaun Austin
Tuesday, January 01, 2013 11:41:20 AM UTC
I ran into this same issue a while ago in a webapi application and found the actual problem with webapi tracing too (using NLog to do the webapi tracing). The issue itself is actually an open workitem on codeplex for asp.net (as it makes no sense to need the DataMember attribute on there as well as the Required attribute)

http://aspnetwebstack.codeplex.com/workitem/270

Steve
Tuesday, January 01, 2013 5:19:50 PM UTC
I had this same issue and don't think that I had to enable tracing to see this error message in Visual Studio output window.
Output window helps solve issues such as this one quite often (i.e. WCF initialization failing with 500, output shows all the details).
Donatas
Tuesday, January 01, 2013 8:07:50 PM UTC
> Eddie points out that he's Googled (with Bing) all over and found nothing.
Not quite politically correct :-)
Great blog, greetings from Poland.
wg
Tuesday, January 01, 2013 9:44:48 PM UTC
Thanks for sharing this experience Scott. I learned at a higher level how fat and lazy managed code makes us at times and there are layers underneath that can give us a lot more information if we take the time to look.

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!
Eddie
Wednesday, January 02, 2013 1:03:27 PM UTC
Very true. It should be noted that people should also instrument their code with tracing to enable others to find production issues which remain unsolvable otherwise.

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
Thursday, January 03, 2013 11:39:51 PM UTC
Hello Scott,

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
. Becuase unlike WP, Microsoft applications like DNN, orchard takes 250mb for a decent configuration .

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
am
Friday, January 04, 2013 3:55:31 AM UTC
This is one of the more frustrating things as we learn! there have been some poor experiences with the hosts especially hosting Orchard.
Friday, January 04, 2013 10:44:29 PM UTC
Great points Scott.

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
Monday, January 07, 2013 11:02:40 AM UTC
Remember that fructose as an isolate is obviously different to fructose as part of the whole fruit with its vitamins, minerals and fiber intact.

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.
Friday, January 18, 2013 3:00:00 PM UTC
It is really great thing to know about tracing. Seriously someone will get knocked off if its turned off and no proper exception message is shown.
Thanks for such awesome share.
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.