Skip to main content


Showing posts from 2013

Deffered Ajax calls in jQuery

One of the useful features of jQuery Ajax function is the ability daisy chain a series of JS calls using ".done" directive. If you have a series of web service calls and want them to execute in a certain predetermined order you can simply add your function to a jQuery $ajax call and use the ".done" helper to call a web service when another call completes. Here is a Plunker example: Here is a quick plunkr example code.

Turning WCF Service into .asmx for debugging

Even though .asmx web services are becoming dinosaurs of the new .NET world of WCF. I missed the simplicity of debugging code right in visual studio without: Creating a client to consume WCF service Attaching w3p.exe process and Adding break points  One quick solution: Turn WCF service into .asmx service with few lines of code, debug your code with asmx, and turn .asmx off during deployment.  Detail steps: 1- First take your WCF class and add WebService attribute to it Code Snippet /// <summary> /// Dual mode with .ASMX and WCF /// </summary> [ WebService (Namespace = "" )] 2- Then add WebMethod attribute to a function you want to expose in .asmx Code Snippet [ WebMethod ] public List < PageController . Page > DualFunction() { 3- Take the .svc file from your solution - copy and rename the copied file [YourOriginalWCFFile.asmx]. Open up the copied file and rename "ServiceHost&qu

.NET 4.0 Bundle of Joy

It use to be that you would have to summon the mighty powers of YUI compressor and roll out the red carpets of MSBuild to minify CSS and JS files in your ASP.NET and MVC applications.  With the introduction of bundling in System.Web.Optimization (now Microsoft.AspNet.Web.Optimization) you can skip the formalities and bundle your JS and CSS file without introducing additional dependencies. To use bundling in a .NET 4.0 app. 1- Create an App_Start folder in your solution (if you don't have one already) 2- Add a class in your App_Start folder for your bundles and create a function to call all your CSS and JS files. E.g. (BundleConfig.cs) Code Snippet using System.Web; using System.Web.Optimization;   namespace YourGlobalNameSpace {      public class BundleConfig     {          public static void RegisterBundles(BundleCollection bundles)         {             bundles.Add( new StyleBundle( "~/styles/style" ).Include                 ( "~/s

Pre-fetching CDN calls

Pre-fetching has been around for  some time now  and is often used to load images and CSS files.  While your mileage may vary, you can also pre-fetch your CDN calls (e.g. Jquery) and make your apps a bit faster by loading scripts in the <head> section to load JS calls before your <script> get resolved by browsers. You can also CDN outage proof it (when it  rarely happens ) by having local JS libraries (for rainy days) using some quick jquery checks (based on code from + Hanselman ) Testing Without pre-fetch: Code Snippet < script src ="//"></ script > < script >      if ( typeof jQuery == 'undefined' ) {         document.write(unescape( "%3Cscript src='[yourlocalbackupjqueryfile].js' type='text/javascript'%3E%3C/script%3E" ));     }      var startTime = new Date();     jQuery.ready();      var endTime = new Date();      var dif

Disjoint queries in LINQ to SQL

I find LINQ to SQL expressions very useful when running queries against Entity Framework objects; however, running disjoint queries in LINQ (queries that require filtering one list and running the results against another) is not as simple using inner joins or correlated sub-queries [i.e.  in SQL "where [someID] not in ([some query])']. To run a disjoint query in LINQ to SQL you would have to write three separate queries. One with all data you need Code Snippet var myDataUniverse = from ent in [SomeEntity]                       select ent; another with items to exclude Code Snippet var myExcludedList = from ent in  [SomeEntity]                        // where some logic to filter excluded entities                       select ent; and finally disjoin them. Code Snippet var myFinalList = myDataUniverse.Except(myExcludedList); Caveat : Entities in the disjoint query must be of the same type or must have an IEqualityComparer  inte

A URL Inception: Sending Query String within a Query String

I came across a simple but interesting solution when working on a project that required sending query string that contains another query string (i.e. a URL Inception  :) ... ) First Pass: Which works fine as long as you are sending a single query string. However, if you have additional query string your data breaks at the first ampersand of the second query string. i.e. will be interpreted as: after the URL is translated in a browser. The Solution: Using good old URL encoding . Changing the ampersand in the second query string to %26  fixes the issue. i.e. %26 subdream1=subdreamvalue1 Will have all query strings in their respective URL.

Optimizing ASP.NET/MVC 3.0 Site

I stumbled upon the Google Page Speed Insights tools  when testing ASP.MVC 3 site performance few days ago. After running the performance test, I found out that the web page I was testing has a performance index of 50% - i.e.things such as loading JS, CSS, and static files are taking away about half of the website loading time.  Here are some steps I took to optimize the site and recover much needed speed. Plan of Action 1- Enabling Gzip Compression Gzip  is a compression algorithm that is used by several web servers and browsers to send and receive compressed http responses. By default gzip is disabled on IIS (bummer ...). You could use IIS GUI to change the settings - but for those of you who are XML ninja's here are the few lines of code that will recover some speed for your .NET 4.0 /3.5 app running on IIS7. Code Snippet < system.webServer >    < urlCompression doStaticCompression = " true " doDynamicCompression = " true " />

.NET 4.0 Evaluating Properties and Methods at Runtime

One of the enhancement that I really like about .NET 4.0 is the ability to work with DynamicExpression class. Pre .NET 4.0 if you want to pass a List to a function you would have to do something like: Code Snippet public static void DoSomethingWithThisList( List < SampleClass > tempList) {      //Do something:: With parameter      foreach ( var itemList in tempList)     {          var getProperty = itemList.SampleProperty;     } } where SampleClass is an a class or struc (below) containing your properties. Code Snippet public class SampleClass {      public string SampleProperty { get ; set ; } } Fast forward in .NET 4.0 & 4.5 you could easily pass an anonymous object or list of objects and let the compiler evaluate the methods and properties associated with your custom objects on runtime. To unleash DynamicExpression: 1- Add reference to "Microsoft.CSharp" or "Microsoft.VisualBasic" to your project 2- Star

Troubleshooting GAC Dlls with tasklist command

Troubleshooting  a DLL installed in GAC can be tricky, especially if you are working on a project that requires loading multiple DLL libraries. One tool that I found useful in troubleshooting GAC Dll's is the tasklist command. If say, for example you are using GAC installed server side document processing library such as iTextSharp.dll , you can easily tell if the dll is currently loaded by running "tasklkist /m iTextSharp.dll" from the command line. More information on usage: