Log JavaScript errors to the server.

Some time ago I found debugging – Automatic feedback on JavaScript error – Stack Overflow.  
and the discussion has  links to
Logging JavaScript Errors To ASP.NET(Unfortunately the link to download in the post is broken).
and custom control to help log JavaScript errors by sending error information to the server –thecodepage.com/post/JavaScript-Error-Notifications.aspx.
I’ve downloaded solution from  JSErrorNotifier.zip. When I wanted to include dll into my solution dependencies,  I decided to rename WebControls.DLL to more descriptive JSErrorNotifier.DLL.
I also decided to rename default namespace to be the same as DLL name.
It wasn’t a good idea, because control stopped to work. It took me some time and I had to call  StreamHelper.EnsureWebResourceValid function from my old post Check that embedded resource exist before calling ClientScript.RegisterClientScriptResource or GetWebResourceUrl
to do all consistent changes for namespace renaming.

Finally when I made control working again, I found that it doesn’t work in IE 9. After another half an hour of debugging I found that for my IE9 onerror event is not fired even for example from MS documentation
It worked on other browsers and on IE8, but on IE9 you need to disable script debugging in Internet Explorer (Tools, Internet Options, Advanced). The  same happens if IE9 changed to IE7 or IE8 mode.(Actually I’ve noticed that Developer Tools Browser mode IE7 or IE8 do not emulate correctly native script engine of older IE versions)

However native IE8 on XP fires onerror regardless if debugging is enabled or disabled.

I hope that not too many IE9 users with enabled debugging will have  JS errors in our application.

Case-insensitive Contains extension for list of strings

I wanted to have case-insensitive Contains method for list of strings.
Overload Enumerable.Contains<TSource> Method (IEnumerable<TSource>, TSource, IEqualityComparer<TSource>) expects custom IEqualityComparer class.I was about to create my own class IgnoreCaseEqualityComparer : IEqualityComparer<string>,
but then found StringComparer.Create Method, that allow to use standard class.

public static bool ContainsString(this IEnumerable<string> collection, string toFind, bool ignoreCase = true)
return collection.Contains(toFind,
StringComparer.Create(CultureInfo.InvariantCulture, ignoreCase));

Select tool to minimize JavaScript and CSS size

There are multiple ways and techniques how to combine and minify JS and CSS files.
The good number of links can be found in http://stackoverflow.com/questions/882937/asp-net-script-and-css-compression
and in http://www.hanselman.com/blog/TheImportanceAndEaseOfMinifyingYourCSSAndJavaScriptAndOptimizingPNGsForYourBlogOrWebsite.aspx

There are 2 major approaches- do it during build or at run-time.

In our application there are multiple user-controls, each of them required different JS or CSS files, and they loaded dynamically in the different combinations. We decided that loading all JS or CSS files for each page is not a good idea, but for each page we need to load different set of files.
Based on this combining files on the build stage does not looks feasible.
After Reviewing  different links I’ve decided that squishit should fit to our needs. http://www.codethinked.com/squishit-the-friendly-aspnet-javascript-and-css-squisher

Different limitations of using SquishIt.
We had some browser specific CSS files, that loaded conditionally depending of browser type(i.e IE and all other browsers). We had to put them in separate bundles,

For Resources and AXD files we decide to use HttpModule and HttpHandler created by Mads Kristensen

To GZIP html we are using wwWebUtils.GZipEncodePage() http://www.west-wind.com/weblog/posts/2007/Feb/05/More-on-GZip-compression-with-ASPNET-Content Just swap the order of which encoding you apply to start by asking for deflate support and then GZip afterwards.

Additional tips about SquishIt.

Use CDN: https://groups.google.com/group/squishit/browse_thread/thread/99f3b61444da9ad1
Support intellisense and generate bundle in codebehind http://tech.kipusoep.nl/2010/07/23/umbraco-45-visual-studio-2010-dotless-jquery-vsdoc-squishit-masterpages/

Links about other Libraries that were considered

A few links from http://stackoverflow.com/questions/5288656/which-one-has-better-minification-between-squishit-and-combres2

.Net 4.5 will have out-of-the-box tools for JS/CSS combining.
http://weblogs.asp.net/scottgu/archive/2011/11/27/new-bundling-and-minification-support-asp-net-4-5-series.aspx . It suggests default bundle of subfolder, but also seems supporting similar to squishit explicitly specified files.

http://www.codeproject.com/KB/aspnet/combres2.aspx  config XML file can specify expiry etc


Dynamically loaded JS files requireJS http://requirejs.org/docs/start.html  

Pack and minimize your JavaScript code size

YUI Compressor (from Yahoo)
JSMin (by Douglas Crockford)
ShrinkSafe (from Dojo library)
Packer (by Dean Edwards)

RadScriptManager  & RadStyleSheetManager -fromTeleric(not free)

Tools to optimize performance:

PageSpeed tools family http://code.google.com/intl/ru/speed/page-speed/download.html


ReplaceBetweenTags function with delegate to describe transformation

I’ve created a function that allow to replace content between XML tags with data, that depend on original content within tag, in particular to MAsk credit card number.

The function uses MidBetween extension from My StringHelper class

 /// <summary>
/// </summary>
/// <param name=”thisString”></param>
/// <param name=”openTag”></param>
/// <param name=”closeTag”></param>
/// <param name=”transform”></param>
/// <returns></returns>
/// <example>
/// // mask <AccountNumber>XXXXX4488</AccountNumber>
///requestAsString  = requestAsString.ReplaceBetweenTags(“<AccountNumber>”, “</AccountNumber>”, CreditCard.MaskedCardNumber);
///mask cvv
///requestAsString = requestAsString.ReplaceBetweenTags(“<FieldName>CC::VerificationCode</FieldName><FieldValue>”, “</FieldValue>”, cvv=>”XXX”);
/// </example>
public static string ReplaceBetweenTags(this string thisString, string openTag, string closeTag,
Func<string, string> transform)
//See also http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex
string sRet = thisString;
string between = thisString.MidBetween(openTag, closeTag, true);
if (!
sRet=thisString.Replace(openTag + between + closeTag, openTag + transform(between) + closeTag);
return sRet;
public static string ReplaceBetweenTags(this string thisString, string openTag, string closeTag, string newValue)
//See also http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex
string sRet = thisString;
string between = thisString.MidBetween(openTag, closeTag, true);
if (!
sRet = thisString.Replace(openTag + between + closeTag, openTag + newValue + closeTag);
return sRet;

Code style Tip: Case insensitive string comparison

if (String.Compare(myString, ALL_TEXT, StringComparison.OrdinalIgnoreCase) == 0)
                                        return true;
OK(not obvious what true means)

if (String.Compare(myString, ALL_TEXT, true) == 0)
                                        return true;
BAD: (non null safe)

if (myString.ToLower()==ALL_TEXT.ToLower()
                                        return true;

Dump an arbitrary object To Html String

For debugging purposes me and my collegue wanted to dump details of the arbitrary object, and created function that uses LINQPad Dump functionality (thanks to http://stackoverflow.com/a/6035014/52277 and original http://linqpad.uservoice.com/forums/18302-linqpad-feature-suggestions/suggestions/447166-make-dump-extension-method-available-in-visual-s discussion)
   public static string DumpToHtmlString<T>(this T objectToSerialize)
           string strHTML =

               var writer = LINQPad.
               strHTML = writer.ToString();
           catch (
Exception exc)
Debug.Assert(false, “Investigate why ?” + exc);

           return strHTML;

You will need to add the linqpad executable as a reference in your project.

TO DO similar in plain text ,look at https://github.com/ServiceStack/ServiceStack.Text StringExtensions , e.g. JsonSerializer/CsvSerializer

or http://objectdumper.codeplex.com/

Disable messages from SQL Server “Login failed for user” in Event log

I’ve noticed multiple messages from SQL Server in EventLog on my machine
Login failed for user ‘NT AUTHORITYANONYMOUS LOGON’. Reason: Token-based server access validation failed with an infrastructure error. Check for previous errors. [CLIENT:]
I’ve found that there are machines of my co-workers, but they were not sure, which processes tried to access my SQL server.

I’ve tried a few things and finally in SQL Server Configuration Manager disabled tcp, as it was suggested in

Specify Linq To SQL ConnectionString explicitly

When modifying Linq to  Sql data model in Visual Studio 2010,  it re-assigns ConnectionString that is available on developer’s machine.
Because the name can be different on different machines, Designer often replace it with something like ConnectionString1, which causes errors during deployment.
It requires developers to ensure that ConnectionString stays unchanged.
More reliable way is to use context constructor with explicit ConnectionString name instead of parameterless default constructor

  var ctx = new MyModelDataContext(Settings.Default.ConnectionString);
Not good:
         var ctx = new MyModelDataContext();

Do not expose enum in WCF response

We had a backward compatibility problem in WCF client, when in Service application a new value was added to one of enums. We discussed different ways to avoid this backward compatibility issues, and I found recommendation do not expose enum in wcf response in http://stackoverflow.com/a/788281/52277.

It is still required to create new versions of our service interfaces to replace each enum fields with string field, that expects only documented values, and describe, what should be default behavior, if field has an unexpected value.


As a workaround for backward- compatibility of enums, they can be declared as [DataContract] and only old values set as [DataMember] , but left new values without the attribute until the new MAJOR release.Members that are not marked are not serialized.  See Enumeration Types in Data Contracts.

Visual Studio 10 crashed when tried to open one of solutions

Visual Studio 10 crashed when I tried to open  one of my solutions. Closing Visual Studio and rebooting the machine didn’t help.
The error message that was logged(see below), didn’t give any useful ideas.
Finally It was fixed after I’ve deleted MySolution.suo file, which was quite big, and also Resharper folders.

Log Name:      Application
Source:        Application Error
Event ID:      1000
Task Category: (100)
Level:         Error
Keywords:      Classic
User:          N/A
Faulting application name: devenv.exe, version: 10.0.40219.1, time stamp: 0x4d5f2a73
Faulting module name: msenv.dll, version: 10.0.40219.1, time stamp: 0x4d5f2d48
Exception code: 0xc0000005
Fault offset: 0x00355770
Faulting process id: 0x1dc0
Faulting application start time: 0x01cd1836888599f4
Faulting application path: C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEdevenv.exe
Faulting module path: c:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEmsenv.dll
Report Id: 9924b2f9-844e-11e1-bc19-782bcba513ea
Event Xml:
<Event xmlns=”http://schemas.microsoft.com/win/2004/08/events/event”>
   <Provider Name=”Application Error” />
   <EventID Qualifiers=”0″>1000</EventID>
   <TimeCreated SystemTime=”2012-04-12T03:21:31.000000000Z” />
   <Security />
   <Data>C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEdevenv.exe</Data>
   <Data>c:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEmsenv.dll</Data>