20 December, 2012

Webservice (asmx) invoke method error after upgrade to .NET 4 (disable compression)

After upgrading some of our old webprojects to .NET 4, we stumbled upon some strange errors when invoking asmx webservice methods. We got all kinds of strange responses back from the server and also on localhost the responses was looking realy strange.


Response in IE9:
"Http 500 Internal Server Error"
The website cannot display the page

Response in Chrome:
This webpage is not found
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.

Response in FireFox:
� ����� �� ` I�%&/m�{J�J��t� �` $ؐ@ ����� iG#)�*��eVe]f @�흼��{���{��;�N'���?\fd l��J�ɞ!��� ?~| ?"^_7m� 7���ݶ�W�/Z�M�YY̲������
�<J��U��˶���:�eˋ���M�U�"���u�^eM:��|��� i;��iY���ֻE�4k��~�/ZW��/,�Ó�n��N�e� � �G��X��3���4M�6�#<V���m],/��`3J�iU�� 
��ʯG o7��պ��i-����wn��l��$zt�nE>K���o����A�X��y}YL�f�ڊ �л� �_f5����<��� ?�<� �[�mW�8��g ���� ?�X?�v�����IU��=5k 7�� ����77�{� ��


We tried debugging the code, but none of the breakpoints was hit, no event log errors, just a strange response from the webserver. So we googled around to see if this was a know bug, but unfortunately not most was found that helped us out, however a lot issues poped up that looked related but in the end was not, apparently not many are upgrading old asmx webservices to a .NET 4 website.

After testing various solutions and combinations of ideas finally we found out that the root bug was hidden for us behind the a response filter that was handling the GZIP/DEFLATE compression of the response. So we disabled the compression on that path/folder and then the root cause was clear, it was the requestValidationMode it needed to be set to: <httpRuntime requestValidationMode="2.0" />

This bug took the most part of a day to fix, so the leason is:
Allways disable HTTP Compression when debugging.

Some reference sources of problem:
stackoverflow.com/.../resource-not-found-when-trying-to-invoke-web-service-method
stackoverflow.com/.../invoke-of-asmx-webmethod-fails-with-xmlexception
go4answers.webhost4life.com/./invoke-web-service-get-http-404-object-209471


An attempt was made to load a program with an incorrect format

On 64 bit versions of windows you can recive an anoying error stating that "An attempt was made to load a program with an incorrect format".

This can sometimes be caused by the fact that you are trying to reference a 64 bit dll in a 32 bit project, but it can strangely enough also be caused by a bug in VS 2010 involving resx files.

I gathered a few reference links, about the issue:
stackoverflow.com/.../an-attempt-was-made-to-load-a-program-with-an-incorrect-format
connect.microsoft.com/VisualStudio/feedback/details/532584/....
msdn.com/b/visualstudio/.../resgen-exe-error-...-program-with-an-incorrect-format
stackoverflow.com/..../could-not-load-file-or-assembly-or-one-of-its-dependencies

I ran into this problem a few times, after any resource (resx) files was modified, and decided to find a solution that would work, and could be put inside the project/solution build cycle.

Add to the project "pre-build" event
FORFILES /P $(ProjectDir) /M *.resx /S /C "CMD /C RegexConsole.exe @path \"AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w\" \"AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w\" I"

It's important the the line of code is on a single line and that the RegexConsole.exe file is placed in a accesable path, put it in the system32 folder or another global %PATH% registered location.

Downloads:
VS-Fix-resx-files-pre-build-event.txt
RegexConsole.exe

29 November, 2012

Umbraco 4.7.1.1 using Safari insert image in tinymce (RTE)

Some of our new customers are using Safari, and they have stumbled upon some issues around using the Umbraco rich-text-editor (TinyMCE v3), they have problems with inserting images and macros.

We googled the problem a little, surely we couldent be the only Umbraco hosting company, which have end-clients using the Safari browser in the backend of Umbraco. But the top hits on google only revealed that there definitely was and is and long existing bug in Umbraco when it comes to the tinymce editor.

The specific problem our clients was facing was that when trying to insert an image or macro the editor just rendered out the text "mce_marker" in the spot where they where trying to insert the element.

We are currently running an Umbraco 4.7.1.1 install with a few custom patches that have yet to be implemented in the most resent versions of the Umbraco core + we are running MySQL as our db. So it's not that simple just to upgrade to the next release without testing, even though some forum entries state that the bug might be fixed in the most recent release, however I can not a resolved item in Umbraco issue tracker:
issues with mce_marker
Issue: can't paste into tinymce with Safari
Issue: can't insert youtube video in Umbraco (not sure if it applies to all browsers)
And of course the frequent dead link to the old issue tracker.

None of the issues was marked as resolved, so of to open the source and see where the underlying bug was happening. The bug was actually happening in the Safari plugin code for tinymce, but unfortunatly I'm not working on a Mac, so it was a little difficult to debug exactly why it was happening, but have a look at the file your-umbraco-site.com/umbacro_client/tinymce3/plugins/safari/editor_plugin_src.js - Line #116-117
both these lines use the "mce_marker" text as a reference to where the html that needs inserting should be placed in the tinymce html. And this is the only place in the entire Umbraco source that there exists a text called "mce_marker".

So how to fix it ?
I firstly checked if the bug was any where to be found on the TinyMCE forums and issue trackers, and I found this bug in tinymce that is a almost identical description of my clients bug. Basicly it states that the bug did exist and that actually the tinymce safari plugin had been removed more than a year before the last entry, so sometime around 2010-09-09 (unfortunately I could not find the version number of tinymce where the plugin was removed). But...

Hmmm, did Umbraco really not upgrade tinymce since 2010 (2 years ago), I looked into the tinymce implementation in umbraco and found that the file : 
your-umbraco-site.com/umbacro_client/tinymce3/tiny_mce_src.js - Line #6-10
contains the date 2011-03-10, so it seems the tinymce was updated after the Safari plugin was removed, but still the Safari plugin was included and loading in Umbraco, but properly it was no longer needed.

So I tacked down the code where the plugin is loaded in the file: \components\editorControls\tinyMCE3\TinyMCE.cs - Line #99-103
The code looks like this:

// safari compatibility
if (HttpContext.Current.Request.Browser.Browser.ToLower().Contains("safari"))
{
        _plugins += ",safari";
}


So to sum up there are two possible solutions to this bug, both are tested on a 4.7.1.1 install and seems to be working fine.
  1. Remove the contents of the files (dont delete them it will cause loading errors) and remember to do a backup of the files first.
    your-umbraco-site.com/umbacro_client/tinymce3/plugins/safari/editor_plugin.js
    your-umbraco-site.com/umbacro_client/tinymce3/plugins/safari/editor_plugin_src.js
    This solution does not require any modifications to the umbraco source code and hence rebuilding of the core modules, and can be used as a "in-place" fix.
  2. Uncomment line 99-103 in the file \components\editorControls\tinyMCE3\TinyMCE.cs and rebuild the umbraco.editorControls.dll, this will ensure that the plugin is not loaded.
    You can download a patch file here that can fix the changeset "
    umbraco_250b9089253a"
Hope it helps you :-) and as always just ping me if you need help with this

04 November, 2012

De-serialize to class with private or no empty constructor

When transferring DTO objects around in .NET sometimes you need to deserialize data to an object with no or private/internal empty constructors. What I have normally done is adding a public empty constructor to my own classes that just does nothing, but I always deemed it as an ugly hack, and have marked them all with TODO comments in the code...

Today I stumbled upon a this method in .NET FormatterServices.GetUninitializedObject that actually can instantiate a class without using any of the constructors in the class. The best part is that it works in all .NET versions, because we are targeting our DTO objects for .NET Framework 2.0 to ensure compatibility with all our clients.

I found these detailed guides about using the method:
http://www.rvenables.com/2009/08/instantiating-classes-with-internal-constructors/
http://mikeoff.blogspot.dk/2006/07/creating-object-instance-without.html

09 July, 2012

Fix : Sorting thousands of nodes in Umbraco

When drilling down into the Umbraco source code I found out that for each node in the sorting level, Umbraco uses .NET's XmlDocument.Clone() method for the entire contentxml cache file. This has a HUGH performance overhead and will cause the server to grind to a halt at some point, either when the number of nodes sorted or number of total nodes gets very high.

We have both :-(
300+ nodes needs sorting, and a total of 45000+ nodes in our umbraco install.

So I created a patch and sumbitted it to Umbraco see more here: http://umbraco.codeplex.com/workitem/30895

#### December 20 - 2012 Update ####
Since umbraco has shutdown/removed the workitems from codeplex, without any redirects or references to the new site, I have uploaded the patch here: download patch





12 June, 2012

Start writing productive code

I read this awesome post today (http://jhovgaard.net/how-i-stopped-writing-awesome-code) that really hits the nail on the head.

I have several times over the last 10 years experienced developers (my self included) falling into the trap, where new technologies is like a drug for developers "if it's new it should be used !!". I actually heard several times from our development team, that they thought we where falling behind in their skill level, because we where not implementing and using the new technologies and methodologies.

My best practice today is following the mantra "speed to market", by trying to deliver things as fast as possible and accepting bugs as a part of the development. Of course this can be frustrating at times, but whenever a bug is discovered our product is improved so it actually means that the number of bugs found equals a increased product value. And when a bug is found, that is system wide, we have built helpers to handle this. This means that we are not focusing on doing a lot of unit testing code for "what-if-scenarios", but instead evolving and improving our products instead of improving test-code.

We do however stumble upon cases where it would have been nice to be able to extend a base functionality without having to re-write 10 classes, but in real life it's not that hard to add additional functionality later to 10 classes, it just requires ten extra Ctrl-V commands.

And in my development history (15 years) whenever I tried to abstract functionality into a Interface structure, because I could foresee that down the line it could/would require changes. It actually always turned out that the changes required later was so extensive that a complete re-write would be faster and easier.