<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>voyce &#187; FormatException</title>
	<atom:link href="http://www.voyce.com/index.php/tag/formatexception/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.voyce.com</link>
	<description>Programming and debugging tidbits</description>
	<lastBuildDate>Wed, 11 Aug 2010 03:56:45 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>FormatException in WPF DataBinding</title>
		<link>http://www.voyce.com/index.php/2009/10/14/formatexception-in-wpf-databinding/</link>
		<comments>http://www.voyce.com/index.php/2009/10/14/formatexception-in-wpf-databinding/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 11:09:26 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[DataBinding]]></category>
		<category><![CDATA[FormatException]]></category>
		<category><![CDATA[visualstudio]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=446</guid>
		<description><![CDATA[A FormatException is generated by the WPF DataBinding diagnostics if the original exception text contains curly brackets.]]></description>
			<content:encoded><![CDATA[<p>While working on some F#/C# WPF code the other day, I kept hitting a fatal FormatException when running under the debugger. Annoyingly, the app would quit with:<br />
<code><br />
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll<br />
Additional information: Input string was not in a correct format.<br />
</code><br />
But it worked fine when started from Expression Blend, or when run using Start Without Debugging in Visual Studio. Let&#8217;s take a closer look&#8230;<br />
<span id="more-446"></span><br />
Looking at the output window, there seemed to be an intial exception before the FormatException, and from the stack trace the second one seemed to be generated as WPF was trying to log the first one:</p>
<p><a href="http://72.47.193.211/wp-content/uploads/2009/10/callstack.png"><img src="http://www.voyce.com/wp-content/uploads/2009/10/callstack-300x113.png" alt="callstack" title="callstack" width="300" height="113" class="size-medium wp-image-449" /></a></p>
<p>I had a look at the text of the first exception, and from that it was obvious:</p>
<p><code><br />
Unable to cast COM object of type 'System.__ComObject' to interface type 'IWhatever'. This operation failed because the QueryInterface call on the COM component for the interface with IID '<b>{</b>CAD939D0-5E5D-11D7-AA0B-0002B33FE9DX<b>}</b>' failed due to the following error: Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE)).<br />
</code></p>
<p>The message contains curly brackets around the IID, and they&#8217;re being intrepreted as insertion points by a call to String.Format!</p>
<h2>Workaround</h2>
<p>So that explained why it only happens in debugger runs; as <a href="http://blogs.msdn.com/mikehillberg/archive/2006/09/14/WpfTraceSources.aspx">this post</a> helpfully points out the WPF databinding is enabled by default in that case. To workaround it, you can disable logging programmatically or using the App.config file to only display <code>Critical</code> output. So if your app is named foo.exe, create a file foo.exe.config that contains:</p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">system.diagnostics</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">sources</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">source</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">System.Windows.Data</span>"<span style="color: blue;"> </span><span style="color: red;">switchName</span><span style="color: blue;">=</span>"<span style="color: blue;">SourceSwitch</span>"<span style="color: blue;">/&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">sources</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">switches</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">add</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">SourceSwitch</span>"<span style="color: blue;"> </span><span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">Critical</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">switches</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">system.diagnostics</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span></pre>
</div>
<h2>Fix&#8230;?</h2>
<p>Hopefully there&#8217;ll be a fix for this at some point soon, as disabling the logging is a pretty large hammer to crack this small nut.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2009/10/14/formatexception-in-wpf-databinding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
