Welcome to Windows Communication Foundation (WCF)
Top Tasks :

WCF Community Bloggers

Friday, August 01, 2008 - Posts

  • Comments

    We recently switched our blog engine out, and I'm still getting the hang of the new system. Looks like due to a misconfiguration, several comments have been waiting for moderation for days or weeks. If yours was one of them, please accept my apology - I didn't have email notifications turned on, so I wasn't being notified that comments were coming in. I've since fixed the problem, so your comments should show up sooner. Sorry for any confusion! Read More...
  • Better exception reporting in ASP.NET

    In my last post , I commented on how ASP.NET health monitoring doesn't output stack traces for inner exceptions, which can be problematic due to its heavy reliance on reflection. I spent the morning doing some further spelunking with reflector , and my first solution was to implement a custom WebEvent that overrides ToString() to format itself with all of the data I care about. I then overrode the Error event via global.asax and raised my custom event, instead of letting ASP.NET raise its default event. This worked reasonably well with the SimpleMailWebEventProvider, but didn't seem to change anything at all with the event log provider. What I found is that the two providers were using entirely different means to format the events! The email provider calls ToString(bool, bool) on the event to ask it to format itself. But the EventLogWebEventProvider does its own formatting of individual fields of the event. Indeed, its ProcessEvent method has a big list of checks: if (eventRaised is WebBaseErrorEvent) AddErrorStuff(); if (eventRaised is WebAuthenticationSuccessAuditEvent) AddLogonStuff(); So it seemed like a better approach would be to write my own provider. I left the event log provider alone, and I wrote a custom email provider to display errors in a more useful way. This also allowed me to drop some fields from the event report that aren't useful for us. And I was able to construct a much more concise and useful subject line (the subject line that SimpleMailWebEventProvider uses is rather clunky since it assumes it might be spitting out a whole bunch of buffered events in one go). Not only does my provider include the stack traces for all of the exceptions in the chain, but in the subject line, I display the type of error that is at the root of the problem. So if I am formatting a TargetInvocationException, I drill into its InnerException chain until I find a different exception type, and display that exception type instead. Oh, one other benefit of building the custom provider instead of using a custom WebEvent was that I was then able to remove the Error handler from global.asax. All I had to do was replace the SimpleMailWebEventProvider with my own provider, and I got the behavior I wanted. Now my email notifications include detailed stack traces. I'll post the code for this provider once it's run for a little while in production and I'm satisfied that it works reasonably well. Read More...
  • Play games, help children, everyone wins!

    The Twin Cities XNA User Group is hosting a very special event next month... They're hosting a huge Halo 3 Tournament at Microsoft to raise money for Children's Hospitals and Clinics of Minnesota . They will also have Rock Band 2 and a few standalone machines for people to check out XNA Community Games. Even if you aren't a gamer, you probably know one. You might even have one in your family. This is an excellent cause worth donating to, and best of all: Magenic is matching funds raised by the event (up to the first $3000)!! For more information, and to register (or just donate), please visit http://www.charityfragathon.com Read More...
  • ASP.NET Health Monitoring doesn't log inner exception stack trace

    This can be a problem, especially when an ObjectDataSource starts throwing exceptions. The stack trace looks the same because of the way the methods are invoked (via reflection) - you end up with a stack trace for a TargetInvocationException, which basically says, "I used reflection to invoke some method, and it threw an exception. See the inner exception for details." ASP.NET's health monitoring system does list the inner exceptions (apparently up to a maximum depth of two, from spelunking the code with reflector ), but it does not emit the stack traces for these exceptions, which would be really helpful . I've spent some time this morning trying to figure out how I'd customize things to emit this, and it looks like what I'd have to do is catch the exception and generate a custom WebEvent that overrides ToString(bool, bool) and does everything that WebRequestErrorEvent does, but also generate the inner stack trace. That seems a bit ugly. A search for "ASP.NET web event inner exception stack trace" yielded no interesting results, so if you've dealt with this and have a cleaner solution, let me know. I'll post my solution once I get it worked out. Read More...
  • Simulating Email in .NET

    I use email as a notification mechanism a lot, and often in class I'll demo sending email via a technique that I use frequently when developing code. It allows you to simulate sending an email message. The trick to doing this is not to hardcode things like host, port, etc. for your SMTP server when you use System.Net.Mail to send mail. Instead, use the default ctor for SmtpClient as I've done in the code below. static void Main( string [] args) { // note the use of the MailAddress class // this allows me to specify display names as well as email addresses MailAddress from = new MailAddress( "admin@fabrikam.com" , "Fabrikam Website" ); MailAddress to = new MailAddress( "mari@fabrikam.com" , "Mari Joyce" ); MailMessage msg = new MailMessage(from, to); msg.Subject = "Testing 123" ; msg.Body = "This is only a test!" ; // note use of default ctor // this looks in config to figure out how to send mail new SmtpClient().Send(msg); } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } What you're telling .NET by using the default ctor for SmtpClient is, "please use my config file to figure out how to send mail". Now you can use the system.net/mailSettings/smtp section in config to specify the details of your mail server, and all of the code in your app that is written to use the default SmtpClient ctor will inherit these settings. Here's an example of what the config on a production server might look like (if you put passwords in your config files, be sure to encrypt those sections ): < configuration > < system.net > < mailSettings > < smtp deliveryMethod ="Network" > < network host ="mail.fabrikam.com" port ="25" userName ="WebsiteMailAccount" password ="whatever" /> </ smtp > </ mailSettings > </ system.net > Read More...
  • From the &quot;Team-Building Exercise&quot; Department

    This crossed my Inbox, and I have to say, I'm stunned at this incredible display of teamwork. Frankly... well, see for yourself . Enterprise consulting, mentoring or instruction. Java, C++, .NET or XML services. 1-day or multi-day workshops available. Contact me for details . Read More...

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us