Wednesday, October 17, 2012

Bundling new feature in ASP.NET MVC 4



Why need of bundling?

When we want to use any javascript and or CSS we include it in page one after other like follows -

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/someJavascript1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/someJavascript2.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/someJavascript3.js")" type="text/javascript"></script>
…..

When we access the page browser needs to load this page to load this page browser needs to load all of the above links for CSS and JavaScript's. To load these files browser creates HTTP request for each one of them. Page is not fully loaded until each of these are loaded and as number of references increases page load time increases and performances decreases.  Problem here is number of HTTP request by browser increases, if we reduces number of HTTP requests we can increase the performance.

Bundling in ASP.NET MVC 4?

ASP.NET  MVC has namespace system.web.optimization which provides this bundling feature. Bundle is logical group of files that can be referenced by name and can be loaded by a single HTTP request. In case of ASp.NET MVC 4 project you will see following line in page _Layout.cshtml

@Scripts.Render("~/bundles/jquery")

Here ~/bundles/jquery is the bundle name, When browser render this page it only sends one HTTP request but loads all file. So number of HTTP request is reduced and hence load time is also reduced.

Where do we Register bundle name?

From above we have seen that bundling can help improving performance; Let us see how to register the bundles in ASP.NEt MVC 4. If you look at the global.asax.cs  then you will find function Application_Start

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }


Highlighted line is the call to  method RegisterBundles in class BundleConfig this method define / register all bundles, You will find this class under folder App_Start.
Let us see the code inside this class.


    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
        }
    }

In above code highlighted part create a new JavaScript bundle with name ~/bundles/jquery that contains all scripts files from the folder Scripts that matches pattern  jquery-{version}.js


Creating your own bundle

You can simply create your own bundle by creating object of class Bundle and adding your files to it. Once that is done you need to add your class to bundleCollection.
 


References






Tuesday, September 25, 2012

Intimate client when Server is down in WCF using AnnouncementEndoint



In client server application when server stops or goes offline , It is important that client is intimated about it immediately, So that client can act on that.
WCF 4 provides feature of announcement using AnnouncementEndoint, by which server can announce their availability like coming up and going down (Hello and Bye messages). Client on the other hand can listen for these announcement and act on them.

AnnouncementEndoint configuration in Server

On server side we can configure to send the announcement by using following configuration in app.config 
  

As highlighted above config of server part should contain endpoint with name udpDiscoveryEpt whose kind is udpDiscoveryEndpoint ; This is a standard endpoint provided by the WCF. Note that this is UDP endpoint so that it will multicast announcement. above figure also highlight the announcement behaviors.

At Server side we need to do only this much no code nothing and server will start multicasting announcement when it comes up and goes down.

Configuration in Client

When writing a client application for this server , first step is add reference to this server it will generate proxy and all config entries automatically. no special configuration needs to be done at client. Following code needs to be added in client for receiving these announcement.

  
WCF 4 has provided new class AnnoucementService this class is present in System.ServiceModel.Discovery namespace for that you need to first add reference to the .NET dll System.ServiceModel.Discovery. We first create object of class AnnoucementService and then attach handler for event OfflineAnnouncementReceived, This event shall be called when client receives udp packet saying that server is down. After this we host this AnnoucementService using service host.

Note from MSDN

"Announcements are sent when the service host opens and closes. If these calls do not finish properly the announcement message may not be sent out."

You can find the code for this article here