Friday, December 4, 2009

JSON Serialization

The Problem:
Here is the error that took me a few days of back-and-forth with an India programming team to figure out:

DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON.
at System.Runtime.Serialization.Json.JsonWriterDelegator.WriteDateTime(DateTime value)
at System.Runtime.Serialization.XmlWriterDelegator.WriteDateTime(DateTime value, XmlDictionaryString name, XmlDictionaryString ns)

The Solution:
I found that I had an uninitialized date time property (meaning it was equal to DateTime.MinValue) in my data transfer object (DTO). If your WEST of GMT, this doesn't cause a problem; however, if your EAST of GMT, the DataContractJsonSerializer will puke.

Saturday, November 7, 2009

WCF REST HttpStatusCode.Unauthorized status code does not work

Alternate title: WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized does not work.
Alternate title: HttpStatusCode.Unauthorized turned into HttpStatusCode.NotFound

The problem:
I spent at least three hours tracking this stupid thing down. The problem that I ran into was Forms Authentication. What's happening is that I am returning HttpStatusCode.Unauthorized (401), but forms authentication is routing the error to the login page. If you don't have a Login.aspx, you will get a resource not found error (HttpStatusCode.NotFound - 404).

If you can change your authentication mode to Windows and the problem goes away, you have this problem.
If you use the browser to hit your REST web service that requires authentication (it returns a 401 status code) and it routes you to your aspx login screen, you have this problem.

Possible solutions:
  • Check out this MSDN article: Supporting HTTP Authentication and Forms Authentication in a Single ASP.NET Web Site . If for some reason this link dies, search for Mixed Authentication Disposition ASP.NET Module (MADAM), which the name of the HttpModule that the MSDN article talks about that allows you to use both HTTP authentication (in this case basic/digest) and Forms authentication in the same web site.
  • I did run across a couple of post were people wanted to use both Windows and Forms authentication together. In those post, it was suggested that the people create a virtual directory in IIS so that you could have two different web.config that specify different authentication methods. I don't know if this will work for this problem, but it is a possible alternative.

Thursday, September 10, 2009

Remote Desktop Drive sharing not working

I ran into a Windows 2003 server on our local network today that would not allow me to share drives so that I could upload/download files to/from the server.

My remote desktop settings look like this:

However, when I connected and opened up windows explorer, my local drives did not appear in the tree view on the left.

Update ->  There two ways to fix this.

Way 1:
After a little bit of digging, I discovered that the group policy for drive sharing (a.k.a. "Drive redirection") was not configured on the Windows 2003 server. This can be fixed by using the MMC snap in called "Group Policy Object Editor":

After navigating to "\Local Computer Policy\Computer Configuration\Administrative Templates\Windows Components\Terminal Services\Client/Server data redirect", I changed the "Do not allow drive redirection" from "Not Configured" to "Disabled" under and it worked fine afterward:

Way 2:
This can also be accomplished this way on Windows Server 2003:

Steps from the Administrative tools window under the control panel.
  1. Double click on "Terminal Service Configuration"
  2. Left click "Connections" on the dialog
  3. Right click "Rdp-Tcp" in the right hand windows.
  4. Uncheck "Drive mapping" on the "Client Settings" tab.

Thursday, July 9, 2009

CurrentSessionContext call results in "No current session context configured."

Well, I was playing with NHibernate today and trying to create a single session in a windows application that would stay open till I was ready to close it. I was using CurrentSessionContext.Bind(someSession) when I generated this error: "No current session context configured." After some searching around, I discovered that I was missing a line in my Hibernate.cfg.xml file.

< name="current_session_context_class" > thread_static < /property >

Apparently, it can have several settings:
  • managed_web
  • call
  • thread_static
  • web


Thursday, June 25, 2009

RichTextBox with better spacing

It took a bit of looking around today to figure out how to create a resource file entry to style my RichTextBox so that it doesn't have ridiculous spacing. I found this, which shows how to do it inside the page or window. However, I wanted to put it in a resource file so that I could reuse it. I finally came up with this:

<ResourceDictionary xmlns=""
<Style x:Key="RichTextWithBetterSpacing" TargetType="{x:Type RichTextBox}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontFamily" Value="Arial"/>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>