Documents.Open returns null, running Word automation under ASP.NET on x64

When building an ASPapplication that generates Word documents,  it was working fine on the local machine. A windows XP installation with Office 2007. When we deployed it to windows 2008 (64 bit) and Word 2007 we saw that Documents.Open returned null. Other than this, there are no exceptions.

The code that returned the error (simplified):

Application app = new Application();
Document doc = app.Documents.Open(ref name);

As it turns out, a 64 bit system stores temporary files in “C:WindowsSysWOW64configsystemprofileDesktop” folder.

The solution: create a new directory called “Desktop” inside “C:WindowsSysWOW64configsystemprofile”

Hope this helps.

Unhandled exception installing SQL Server 2008 R2 on a Windows XP / Windows 7 machine

System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings: Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified. (C:Documents and SettingsBoschRLocal SettingsApplication DataMicrosoft_CorporationLandingPage.exe_StrongName_ryspccglaxmt4nhllj5z3thycltsvyyx10.0.0.0user.config line 5) ---> System.IO.FileNotFoundException: Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.When installing SQL Server 2008 R2 on a Windows XP (or, according to the comments, Windows 7) development machine that has been around for quite some time, I got an error stating an unhandled exception occured in the application (see image). The error showed up directly upon startup and read:

An error occurred creating the configuration section handler for userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings: Could not load file or assembly ‘System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies.

Repairing the .NET 4.0 framework didn’t solve the issue. Neither did removing all SQL stuff through ‘Add or Remove Programs’. Looking into the error a bit further…

 The system cannot find the file specified. (C:Documents and Settings_USERNAME_Local SettingsApplication DataMicrosoft_CorporationLandingPage.exe_StrongName_ryspccglaxmt4nhllj5z3thycltsvyyx10.0.0.0user.config line 5) —> System.IO.FileNotFoundException: Could not load file or assembly ‘System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies. The system cannot find the file specified.

 So the problem probably wasn’t that System.dll couldn’t be found, but some user.config file for the landingpage executable. The solution is as simple as it is radical: remove the entire (temporary) folder ‘C:Documents and Settings_USERNAME_Local SettingsApplication DataMicrosoft_Corporation’ (where USERNAME_ is, of course, the current username). After this the setup should start up without any problems.

Error installing the Windows Azure AppFabric Tools for Visual Studio (CTP)

Windows Server AppFabric is installed and is not compatible with this release of Windows Azure AppFabric Developer Tools for Microsoft Visual Studio 2010 - June 2011 CTP. Please uninstall Windows Server AppFabric and rerun this setup if you want to install Windows Azure AppFabric Developer Tools for Microsoft Visual Studio 2010 - June 2011 CTP.When installing the Windows Azure AppFabric Tools for Visual Studio (found here), you might get the error message shown on the right, stating that Windows Server AppFabric is installed and that it is not compatible with this release of Windows Azure AppFabric Developer Tools for Microsoft Visual Studio 2010 – June 2011 CTP. It asks to uninstall Windows Server AppFabric and rerun this setup if you want to install Windows Azure AppFabric Developer Tools for Microsoft Visual Studio 2010 – June 2011 CTP.

When looking for the install of ‘Windows Server AppFabric’ to remove it through ‘Uninstall a program’ you won’t find this item in the list of installed programs. This might feel as a big problem heading your way…

Solution
This is, however, only a small problem on your way to Azure AppFabric development. Go to your list of installed programs and click ‘View installed updates’. Under Windows, you will find the update ‘Windows Server AppFabric (KB970622)’. Uninstall the update, restart your machine and try installing the Windows Azure AppFabric Tools for Visual Studio again…

Enjoy!

The Visual Studio 2010 Test Client for WCF services

When writing WCF services, most of us find ourselves writing quick test applications. To give you more time to do what a developer should be doing (adding business value to the project) the Visual Studio team added a WCF test client that can help you reach goals faster.

This is not a very difficult application, but it does the trick and is more than sufficient for simple testing. The WCF Test Client can be found at this location (without the ‘ (x86)’ for x86 machines): C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDE.

Taken from the help:

Windows Communication Foundation (WCF) Test Client (WcfTestClient.exe) is a GUI tool that allows users to input test parameters, submit that input to the service, and view the response that the service sends back. It provides a seamless service testing experience when combined with WCF Service Host.

WCF Test Client screenshots:
image image

Updated: Thanks to Marco for pointing out my typo…!

HowTo: Save a file from Silverlight using the SaveFileDialog

Saving a file from Silverlight using the SaveFileDialog, added in Silverlight 3, is easy. If you’re used to desktop development however, you might find yourself getting a SecurityException with the message ‘File operation not permitted. Access to path ‘xxx’ is denied.’. Here’s why:

In desktop development, you’re used to getting a filename from a SaveFileDialog. Next, you start doing whatever you need to be doing to the file, based on the filename. This would look something like this:

SaveFileDialog saveFileDialog = new SaveFileDialog();

if (saveFileDialog.ShowDialog() == true)
{
    StreamWriter streamWriter = new StreamWriter(saveFileDialog.SafeFileName);
    streamWriter.Write("Follow me on twitter: @rickvdbosch");
    streamWriter.Flush();
    streamWriter.Close();
}

.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; }

This results in the SecurityException. First time I saw the property, I genuinely thought it said SaveFileName. But, as you can see, it says SafeFileName. It is named safe, because the returned file name has all file path information removed for security purposes.

In Silverlight, if you want the above functionality, it should look something like this:

SaveFileDialog saveFileDialog = new SaveFileDialog();

if (saveFileDialog.ShowDialog() == true)
{
    StreamWriter streamWriter = new StreamWriter(saveFileDialog.OpenFile());
    streamWriter.WriteLine("Follow me on twitter: @rickvdbosch");
    streamWriter.Flush();
    streamWriter.Close();
}

.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; }The OpenFile method opens the file specified by the SafeFileName returning a Stream, giving you the opportunity to save whatever you want to save over there.

Hope this helps.

On a side note: the ‘== true’ part in the if condition is needed in this case. Because the ShowDialog method returns a nullable boolean it is not enough to use saveFileDiaolog.ShowDialog() in the condition. Just so you know… :)

‘Cannot find entry to delete’ deleting files from a Zip with (SL) SharpZipLib

For a project I’ll probably dedicate a post on this blog to in the future, I needed a ZIP library for Silverlight. After asking around on twitter (@rickvdbosch) I was pointed towards the SharpZipLib port on Codeplex, found on http://slsharpziplib.codeplex.com.

Basically what I wanted to do was replace a file inside the ZIP file with my version of that file. The file was located in a specific folder inside the ZIP and could be found by either iterating through the ZipEntries in the ZIP, or by getting it based on its name. But deleting the file gave me a ZipException with the error message ‘Cannot find entry to delete’. When I added a file myself, it could be deleted just fine, but the ZIP just did not want the existing file to be removed.

After searching around on some forums, I found there was a new development version of SharpZipLib which might solve the problem. But that was the full version, not the Silverlight port. So I searched around some more and found something about subfolders.

As it turns out, and existing file can be deleted when it is located in the root of the ZIP. Apparently there’s some bug keeping you from deleting files in subfolders. Anyhow, moving the file I need updated to the root of the ZIP solved my problem for me (for now), so I can continue my project. If and when slsharpziplib is updated in the future, I’ll have a look if they solved this in the port too…

Hope this helps.

Using Crystal Reports in ASP.NET on a 64 bit machine

The above scenario gave me the well-known could not load file or assembly error message. The assembly that couldn’t be found was  ‘log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304’. Or one of its dependencies of course… The situation in which I was getting this error message:

  • using the CrystalReportViewer in an ASP.NET application
  • having the ASP.NET application hosted in IIS (the error did not occur on the ASP.NET Development Server)
  • running on a 64 bit machine

Log4net was available in the GAC, with the exact version and PublicKeyToken stated in the error message. So that couldn’t be the problem. Or could it…? Having a second (and closer) look at the log4net assembly in the GAC one thing stood out.
log4net-x86

The Processor Architecture for the log4net assembly is set to x86. That means it can’t be loaded by IIS, which is x64 on this machine. OK, so now we know what’s wrong. Next: how to solve this?

Solution
I tried several things to get my (x64) IIS, Crystal Reports and log4net to get to play together nicely. Things that did not help to get this problem fixed included removing log4net from the assemblies section of the web.config, kicking my machine around the office and trying to recompile log4net for the x64 architecture.

As far as I have been able to find out, these two steps will get it working:

  1. Enable 32 bit ASP.NET IIS integration
    Open a command prompt, go to the non x64 framework folder of your choice (for instance C:WindowsMicrosoft.NETFrameworkv4.0.30319) and execute “aspnet_regiis –enable –i”
  2. Enable 32 bit apps for 64 bit IIS
    Open a command prompt and execute “cscript.exe %SystemDrive%inetpubAdminScriptsadsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 true
    If you do not have the admin scripts installed, install the from the ‘Turn Windows features on or off’ option in your Control Panel. The scripts are located under Internet Information Services – Web Management Tools – IIS 6 Management Compatibility – IIS 6 Scripting Tools.

I’m not sure of any negative side-effects at this point. If I run into any in the future I’ll let you know. For now my application is working like a charm!

Hope this helps.

By |January 5th, 2011|.Net, Error, x64|2 Comments

Little-heard-of ASP.NET feature: app_offline.htm

Although the feature has been around since ASP.NET 2.0, I still meet people that don’t know and/or use app_offline.htm. Do you know (and use) the feature…?

When working on an ASP.NET web application, you should notify your visitors in a decent way that your application is down. There’s a nice default feature available for these purposes since ASP.NET 2.0. Somehow, however, this feature is not very well known and rarely used. The “App_Offline.htm” feature in ASP.NET 2.0 provides an easy way to bring down an ASP.NET application while you make changes to it. This feature kicks in as soon as you place a file called app_offline.htm in the root of the application. When ASP.NET detects the file, it will shut-down the app-domain for the application and won’t restart it for requests. The app_offline.htm file will be sent as the response to all new dynamic requests for the application. When you’re done updating the site, just delete or rename the file and your site it will come back online.

Taken from (or actually inspired on) this blog post of Scott Guthrie:

Keep an eye on a feature of IE6(+) called “Show Friendly Http Errors”. This can be configured in the Tools->Internet Options->Advanced tab within IE, and is on by default with IE6. With this setting turned on, when a server returns a non HTTP-200 status code with less than 512 bytes of content, IE will not show the returned HTML and instead substitutes its own generic status code message.

If you use the app_offline.htm feature, make sure you have at least 512 bytes of content within it to make sure that your HTML shows up to your users instead of IE’s friendly status message. If you don’t want to have a lot of text show-up on the page, one trick you can use is to just add an html client-side comment with some bogus content to push it over 512 bytes. For example:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head>

    <title>Site Under Construction</title>

</head>

<body>

    <h1>Under Construction</h1>

 

    <h2>We’re currently working on our site…</h2>

   

<!–       

    Adding additional hidden content so that IE Friendly Errors don’t prevent

    this message from displaying (note: it will show a “friendly” 404

    error if the content isn’t of a certain size).

   

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2> 

    <h2>Gone to Florida for the sun…</h2>     

–>

</body>

</html>

Hope this helps

"AjaxControlToolkit is undefined"

Because we were still using an old Visual Studio 2005 solution which included a Web Site project, it was time to upgrade. We upgraded our Visual Studio 2005 solution to Visual Studio 2010 (and .NET 4.0), converted the Web Site Project to a Web Application Project and then, of course, the AjaxControlToolkit had to follow. I downloaded the latest build from their Codeplex site and updated the references in the different projects. I ran the website, and that’s where things got ugly…

Most pages worked nicely, but there were a few that gave an “AjaxControlToolkit is undefined” error. I removed all references to the AjaxControlToolkit, removed all the old versions of it from my machine, referenced the most recent version again, all to no avail. After Googling* Binging the error I found a LOT of possible solutions. These included:

  • Use the ToolkitScriptManager from the AjaxControlToolkit in stead of the built-in ScriptManager
  • When using the ToolkitScriptManager, set CombineScripts to false
  • When using the ToolkitScriptManager, set EnablePartialRendering to true
  • Clear the browser cache
  • Clear the ASP.NET Temporary Files directory
  • Use a (dummy) control to make sure the JavaScript files have been loaded correctly

Unfortunately, none of these possible solutions helped us with our specific problem. Our problem occurred when we, for instance, set the PositioningMode for a control from custom JavaScript using the AjaxControlToolkit.PositioningMode enumeration. The error was always from custom JavaScript. After looking around a bit more I found one site that mentioned something about changing the AjaxControlToolkit ‘namespace’ in JavaScript.  After playing around a bit I found out that changing the use of AjaxControlToolkit.XXX to System.Extended.UI.XXX provided the solution.

Hope this helps.

* Of course this is a weak attempt at a joke, but fact is that Google (the first 7 or so pages) only pointed me in the direction of the solutions that weren’t solutions for my situation. When I tried Bing for a change, I found something that put me on the right track pretty fast. Kudos to Bing!

“The project location is not trusted”

After installing Windows 7 and Visual Studio 2010 on my laptop, I ran into the well-known message “The project location is not trusted”. I thought all I had to do was follow the instructions in this MSDN article to get my development share to be trusted again, but this doesn’t seem to work.

I already tried adding the new child group under the Internet_Zone in stead of the LocalIntranet_Zone, and using the complete server name and the server IP address in stead of the name of the server. All of these steps did not solve the message popping up…

I’m open to any suggestions, ‘cause this problem is beginning to drive me nuts.