Tuesday, January 16, 2007

SGen XmlSerializers

My VS2005 C# web project was perfectly working in my development environment but I got the following error after deploying in a production environment:

Configuration Error
Parser Error Message: Cannot deserialize [C:\data.xml].

Using the fusion log viewer (FUSLOGVW.EXE) reveals a binding exception to Assembly.XmlSerializers.dll, where Assembly is the name of the assembly which contains the classes serialized in data.xml.

My ASP.NET application runs as a dedicated application pool under the credentials of a windows user which has very limited rights. The problem is related to the user rights which prevent the ASP.NET process from automatically generating XmlSerializers.

Visual Studio cannot generate XmlSerializers

On the build tab of your VS project properties, there is an option called “Generate Serialization Assemblies” which you can set to “On”.

If you look at MSBuild commands in the output window, doing so actually adds a command line which looks like the following:

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin\sgen.exe /assembly:"Assembly.dll" /proxytypes /reference:"reference1.dll" … /reference:"referenceN.dll" /compiler:/keycontainer:VS_KEY_5EFB7881D71082EDCF85DBBFCD748B9A /compiler:/delaysign-

Note the /proxytypes option which actually prevents SGen from generating your XmlSerializers as specified in the documentation for the XML Serializer Generator Tool (Sgen.exe).

Use SGen as a post-build event

As a consequence you need to add the SGen command as a custom post-build event on the Build Events tab of your VS project properties:

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" /force /assembly:"$(TargetPath)" /compiler:/keycontainer:VS_KEY_5EFB7881D71082EDCF85DBBFCD748B9A /compiler:/delaysign-

Other interesting links

Thursday, January 04, 2007

Unable to validate data error in relation to machine key

I have experienced the following error on a production web site:

Unable to validate data

at System.Web.Configuration.MachineKey.GetDecodedData (Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength)

Searching on the web, many developers seem to solve the problem at least partially by generating a static key as described in Microsoft's knowledge base.

This is a workaround but not an actual solution to the problem, at least in my scenario where my application runs in a dedicated application pool under limited privileges as described in http://msdn2.microsoft.com/en-us/library/ms998297.aspx.

The fix is to run aspnet_regiis.exe –ga DOMAIN\USER where USER is the identity of the application pool. Also make sure the user is part of the IIS_WPG group.

This command gives not only access to the IIS metabase but also creates the registry keys required in

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\2.0.50727.0\AutoGenKeys

for the application pool to generate machine keys.

Friday, December 22, 2006

Touchy connection strings

My ASP.NET 2.0 / SQL Server 2005 application was working perfectly in my development environment but I experienced the following error message when testing deployment in a Windows 2003 virtual machine:

"A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)".

My connection string was:

Server=localhost;Database=myDB;Integrated Security=SSPI;

Simply changing it to the following solved the issue:

Data Source=.;Initial Catalog=myDB;Integrated Security=True;

Considering both shared memory and TCP/IP where enabled in both environments, I can neither explain why the issue occurred or why the change above solved it. If someone can, please leave a comment.

Thursday, December 21, 2006

Diagnosing "aspnet_merge.exe exited with code 1” error in Web Deployment Projects

For those that have problems with Error 24 "aspnet_merge.exe" exited with code 1.

The error is related to a duplicate class name in your web project, i.e. two files which have the same name, generally in different directories.

Those files could be hard to find and the following is a quick way to isolate the problem and find the duplicate names.

In Visual Studio 2005:

  • Select Tools --> Options.
  • Then in the Projects and Solutions branch, select Build and Run.
  • You'll see a dropdown box for MSBuild Project Build Output Verbosity. Change this to Diagnostic, and OK.
  • Show your build output by selecting the View --> Output option.
  • Make sure that in the 'Show output from' box, you've selected "Build".
  • Then just build your project, and wait for the inevitable fail. You will see where the duplicates are when it fails to build.

This tip has been posted by Simon Morgan on http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx.

Tuesday, December 19, 2006

New incompatibility between ASP.NET Ajax 1.0 RC and Google AdSense

Run the following ASP.NET page in IE7 and you will get a Javascript error:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" >
<Scripts>
<asp:ScriptReference Name="PreviewScript.js" Assembly="Microsoft.Web.Preview" />
</Scripts>
</asp:ScriptManager>
<div>
<script type="text/javascript"><!--
google_ad_client = "pub-6623312146541354";
google_ad_width = 250;
google_ad_height = 250;
google_ad_format = "250x250_as";
google_ad_type = "text";
google_ad_channel = "";
google_color_border = "000000";
google_color_bg = "F0F0F0";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "008000";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</form>
</body>
</html>

ASP.NET Ajax page method on login page requires setting location in web.config

I have spent quite a while on this one, so I deliver my findings here hoping that it will help.

If you have an ASP.NET web site with forms authentication and your login page has an ASP.NET Ajax extensions page method, this method will not work unless you set:

<location path="login.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>


Otherwise, the call to ~/login.aspx/yourScriptMethod fails because it is not authenticated.

Thursday, December 14, 2006

“debugger”, the magic JavaScript statement

This post is dedicated to all the Javascript programmers who still use alerts to debug their code.

Not sure about you, but I have always struggled to set and enable Visual Studio breakpoints to debug my Javascript code until I have found about a magic statement named debugger.

To use it:
  1. Make sure “Disable Script Debugging” is unchecked in Internet Explorer, Tools (menu) -> Internet Options… (submenu) -> Advanced (tab) -> Browsing (node).
  2. Put the debugger; statement anywhere in your code where you want the breakpoint.
  3. Execute your web application and when the browser script engine interprets the statement, it launches the debugger and breaks on the statement line.

Monday, November 27, 2006

Migration from Atlas July CTP to ASP.NET Ajax Beta 2

I am developing an application which does not really make use of Atlas controls but has a significant amount of Atlas scripting code to migrate.

I am not found of declarative scripting but maybe I am old-fashioned: I like to step through my code to debug it. My opinion is that declarative scripting is only good as the output of code-generation tools. It is not something that developers should write in a text editor.

So, I have got script to migrate and I have read the documentation available at:

Downloading and installing ASP.NET Ajax Beta 2 is well explained in the documentation but I had to do a bit of guessing as well as exploring Microsoft’s code to migrate my own code. I deliver my findings below:

Migrating client script

  1. To use client scripting of html components, you need to add a reference to PreviewScript.js in your ScriptManager as shown below:

<asp:ScriptManager ID="ScriptManager" runat="server">
<Scripts>
<asp:ScriptReference Assembly="Microsoft.Web.Preview"
Name="Microsoft.Web.Resources.ScriptLibrary.PreviewScript.js"/>
</Scripts>
</asp:ScriptManager>

  1. $(id) is now $get(id)

  2. The Sys.UI namespace is now Sys.Preview.UI

  3. Sys.UI.Select is now Sys.Preview.UI.Selector

  4. Sys.UI.CheckBox.click.add(handler) is now Sys.UI.Preview.CheckBox.add_click(handler)

  5. Sys.UI.Select.selectionChanged.add(handler) is now Sys.Preview.UI.Selector.add_selectionChanged(handler)

  6. $addHandler/$removeHandler is the new way to add event handlers

  7. Sys.UI.Control.get_enabled() and set_enabled() no more work

Migrating web service calls

  1. You need to decorate your web services and page methods with new attributes as explained in http://blogs.msdn.com/sburke/archive/2006/10/21/hint-components-that-use-web-services-with-asp-net-ajax-v1-0-beta.aspx.

  2. The prototype of asynchronous calls to web services and page methods has been simplified, but this is well explained in the documentation listed above.

Friday, November 24, 2006

How to configure the IIS SMTP Service to send emails from ASP.NET code?

I have probably configured the IIS SMTP service a dozen times over the past five years but I still struggle with authentication and relay, so I have decided to write this note once and for all.

First, the source for your System.Net.Mail issues is definitely http://www.systemnetmail.com/, including some sample code.

Because we are all urged to deliver rapidly, I have provided a test application which you can download from http://www.velodoc.com/downloads/061124.blog.zip. Contrary to most samples that you will find on the Internet, this one uses a configuration file including system.net\mailSettings, which is probably the way you want to send emails from your ASP.NET code.

Obviously you need to install the SMTP Service, which is well explained at http://msdn2.microsoft.com/en-us/library/8b83ac7t.aspx.

Let’s say you need to send your emails from website@yourdomain.tld. In this case, it is recommended (but not required) that you rename the default domain into yourdomain.tld.

Display the default SMTP Virtual Server properties and on the Access tab:

  1. Configure authentication by removing Anonymous access and allowing Basic authentication and/or integrated Windows authentication;
  2. Grant relay to 127.0.0.1 and allow all computers which successfully authenticate to relay.


You can certainly be more restrictive depending on your requirements, once you got teh above configuration to work properly.

Finally, you can create a dedicated account and properly configure your settings:

<system.net><mailSettings>
<smtp deliveryMethod="Network" from="website@yourdomain.tld">
<network host="127.0.0.1" port="25" userName="DOMAIN\account" password="password"/>
</smtp>
</mailSettings>
</system.net>

What’s next after designing and hosting your web site?

You have now designed a nice web site which you have uploaded on a hosted server and you think you are ready to go. Not quite right. There are a few more steps required which will guarantee a better visibility of your web site.

I am not a search engine optimization (SEO) professional and qualified people are writing books about SEO which can be a very complex subject. But I have been confronted to the issue, I have searched for answers on the Internet and I publish here my findings. If you apply the 80-20 Pareto rule, you can get decent results in three steps that you may want to re-iterate periodically:

  1. Make your web site is right for search engine crawling robots;
  2. Submit your web site to search engines;
  3. Get referral links.

I may have done something right since I have managed to get our web site on the first page on MSN and Yahoo. I know from experience that the only way to get a high ranking on Google is to obtain referral links.

1) Make your web site right

First, you need to make sure that search engine robots can read your web site. You also need to assist search engine robots by telling them which pages to index and how to categorize your web site.

Make sure your HTML is compliant

A web design environment like Dreamweaver will warn you of any non-compliance issue. You can also use a text browser like Lynx to check that your web site displays properly to search engines. Note that search engine robots read sites in a similar way. You can also check your pages at http://www.smart-it-consulting.com/internet/google/googlebot-spoofer/index.htm.

Important also is to set the page encoding and content language. I personally recommend using utf-8 encoding even on English pages. Contrary to what people often think, utf-8 is not double byte and the size of an English page is the same in utf-8 and iso-8859-1.

Describe your web site properly

Title, meta description, meta keywords and images alternate text contribute to help search engines categorize your web site properly.

Getting your keywords right is a difficult exercise:

  1. you should limit yourself to 10 to 20 keywords;
  2. the more targeted your keywords the more efficient they are to help find you, but you do not want to be too narrow;
  3. As far as Google, Yahoo and MSN are concerned, you do not need both the singular and plural of a keyword and the order of words in a keyword seems to make no difference.

The following tools will help your build your keywords meta tag:

Add an address and a privacy policy

An address and a privacy policy will not give you a high ranking, but they may prevent you from getting one because you won’t look like a serious company. To add a privacy policy for your site, follow the steps at http://www.the-dma.org/privacy/creating.shtml#form.

Add content rating

To add content rating for your site, follow the steps at http://www.icra.org/label/

Create sitemaps

Sitemaps will tell search engines which pages to look for. To create sitemaps, follow the steps at http://www.xml-sitemaps.com/. You can get more information at http://www.sitemaps.org/.

You are encouraged to submit your sitemaps to search engines, in particular:

Create a robot.txt file

You create a robots.txt file essentially to tell which parts of your site not to index. More information about robots.txt files is available at http://www.robotstxt.org/wc/robots.html.

Add web site monitoring and statistics

If your web site is too often inaccessible, you will be downgraded by search engines. Accordingly, you need to monitor your web site and keep your ISP honest. The following monitoring services range from free to high-end:

Most hosting packages come with AWStats and Webalizer which give poor statistics. There is obviously http://www.webtrends.com/ but I can only recommend the free http://www.google.com/analytics/.

2) Submit your web site to search engines

Most ISPs offer search engine submission standard with hosting packages. Unless you are very lazy, do not pay for a submission service and do not buy submission software. Submitting your URLs to Google, MSN and Yahoo only takes 10 minutes and covers for more than 80% of the search engine traffic.

This is especially true if you already own a web site that is referenced. In this case, you just need a hyperlink from the referenced web site to the new web site and the search engine crawlers will automatically index your new site.

3) Get referral links

Getting referral links is the only thing that guarantees high rankings. Basically, the more a page is referenced on the web, the best the value of this page to the Internet community, so the highest ranking it gets in searches. Additionally, a reference on the http://www.bbc.co.uk/ web site is worth more than a reference on http://www.acme.tf/.

So you need to work on getting other web sites to reference your own web site. This is an everyday job that only your organization can successfully perform. You should get a decent ranking on Google (top 3 pages) with about 100 references but obviously some topics are more crowded than others. The earlier you start the better. Good luck!