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
http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onerrorEX.htm.
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.

Advertisements

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

https://github.com/andrewdavey/cassette
http://stackoverflow.com/questions/7026029/alternatives-to-cassette

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

http://www.west-wind.com/weblog/posts/2008/Jul/07/Inclusion-of-JavaScript-Files
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



v

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 (!
String.IsNullOrEmpty(between))
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 (!
String.IsNullOrEmpty(between))
sRet = thisString.Replace(openTag + between + closeTag, openTag + newValue + closeTag);
return sRet;
}

Code style Tip: Case insensitive string comparison

Good
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 =
“”;

           try
           {
               var writer = LINQPad.
Util.CreateXhtmlWriter(true);
               writer.Write(objectToSerialize);
               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: 10.222.25.129]
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
http://blogs.msdn.com/b/psssql/archive/2010/03/09/what-spn-do-i-use-and-how-does-it-get-there.aspx