Thursday, October 9, 2014

Niche performance tip for developers


Make sure Fusion log viewer (fuslogvw) is disabled. Only Enable it when you are debugging and switch it off once done.

Saturday, October 4, 2014

Some core system tools for Windows



1) Process Moniter (procmon)
If you need to moniter whats happening with your file system or registry, just open this tool and this will record all events at OS level. You can filter them according to your needs. I used this to debug one of installer issue where the installer is supposed to read from registry but was reading from wrong location because it was a 64bit OS machine.

2) Fuslogvw (Fusion log viewer)
Use this tool to check assembly bindings at runtime.

3) Debugview  (DbgView.exe)
Use this tool to debug traces that you can write in your .NET applications using System.Diagnostics.Trace.Write(“IIS site activated”). This will helpful in debugging application in UAT and PROD environment.

Increase your chances of a successful Tatkal booking in Indian Railways

1) Login 15 mins in advance before 10 AM


2) Keep the session active by refreshing every 2 mins.
Do not click "Refresh" button. Instead keep on clicking "Home" link.


3) Sync your laptop time with IRCTC time for accurate results. IRCTC time can be found from this link
http://www.indianrail.gov.in/train_Schedule.html (type a train number and submit. You will find IRCTC time in the bottom)


4) Fill all the form data (Name, Age etc) in advance using http://ctrlq.org/irctc/
Then click "I m feeling lucky"
Then Drag the "Magic Autofill" button to the IRCTC form fill window.
("Magic Autofill" button could also be dragged to Chrome Bookmarks bar to save, so in IRCTC page, just click it)


5) Most importantly have patience, Do not press "refresh" or "back" button or any other link while booking process is on.


6) Use the best network possible (office/home/remote desktop). Remember using "Remote Desktop" can cause UI lag and using
   using networks other than Home network can add to Bank validation steps.



7) Put Trains Start Point as "From" and Final destination as "To" for TATKAL. Use Station code as they are shorter.


8) Ask someone else to do the same (above) with their irctc username.


9) Don’t click on any option unless the page is fully loaded


10) Searching trains and staying on your selection before 10:00 would spoil everything. After logging in if you search your trains and stay on your preferred train selection, when you click Book option it would show a pop up “tatkal ARP not allowed before 10:00″ something like that. And It would not allow to book ticket even after 10:00.

So, exactly at 9:59/10:00 search train and do the rest of the process.

11) Keep all the payment related information (Card number/Net Banking Password/Security Ques) ready.


12) Make sure the capcha is correct in the first attempt itself.

Thursday, October 2, 2014

Digital Signatures

Digital signatures

Digital signatures are based on asymmetric cryptography which means – a message encrypted with one key is decrypted by a different key. Both keys in the scenario are mathematically related. So in other words – a message encrypted with public key can only be decrypted by its corresponding private key and a message encrypted with private key can only be decrypted by its corresponding public key.

In a general case a message is encrypted with public key (by the sender) and the receiver decrypts it with the private key (owned and held by him ONLY). This way all the intermediaries, even after getting hold of the message cannot decrypt as it can only be decrypted by the PRIVATE KEY (only held by the intended receiver).

In case of digital signature, the scenario is opposite. Here the intent is not encryption but rather AUTHENTICITY and INTEGRITY.  Explanation – I have a document. I make a hash out of it (called message digest). I encrypt the hash with my PRIVATE key. I send the original un-encrypted document along with the encrypted message digest. Explained with diagram below.





After receiving the pack (A and C). The receiver decrypts C (encypted hash) with his PUBLIC key. (Remember anything encrypted with PRIVATE key can be decrypted by its corresponding PUBLIC key and vice-versa).
If the decryption is successful then the authenticity of the sender is established as ONLY the SENDER had the private key.
Then the receiver hashes the original message and compares it with the decrypted hash (above step). If they are same it means the message is not tampered. Hence integrity is also established.


In the above process C (Hash encypted with PVK) is called the DIGITAL SIGNATURE.

Tuesday, September 16, 2014

Enable autostart in IIS 7.5 (without AppFabric)

You can create your own autostart provider for your WCF services and there is no need to use AppFabric for autostarting WCF services.

You will need/configure 4 things to enable autostart in WCF
            1) Autostartprovider (source code below)
            2) Add the reference of the above library to your WCF service
            3) Modify the svc file to use factory - 

<%@ ServiceHost Language="C#" Debug="true" Service="Service1" CodeBehind="Service1.svc.cs" Factory="Web.Hosting.ServiceBusHostFactory" %>

            4) Modify the applicationHost.config in C:\Windows\System32\Inetsrv\config in the following places

a) At AppPool level
                       <add name="AutoStartSite" managedRuntimeVersion="v4.0" autoStart="true" startMode="AlwaysRunning"  />



b) At Site level -
                      <site name="AutoStartSite" id="2" serverAutoStart="true">


c) At Virtual directory/Application level
                      <application path="/AutoStartVD" applicationPool="AutoStartSite" serviceAutoStartEnabled="true" serviceAutoStartProvider="MyAutoStartProvider">


d) serviceAutoStartProviders itself -
                       
In your applicationHost config file between <webLimits/> and    </system.applicationHost>, add the following section - 

         <serviceAutoStartProviders>
            <add name="MyAutoStartProvider" type="Web.Hosting.AutoStartProvider, Web.Hosting" />
        </serviceAutoStartProviders>



Thats it. Deploy your WCF service. Fire up DbgView.exe for debugging purposes as there are some diagnostics statements in the AutoStartProvider.




================START Autostartprovider source code=======================

File::::AutoStartProvider.cs

using System;
using System.Diagnostics;
using System.IO;
using System.ServiceModel;
using System.Threading;
using System.Web.Hosting;


namespace Web.Hosting
{
    public class AutoStartProvider : IProcessHostPreloadClient
    {
        
        public void Preload(string[] parameters)
        {
            bool isServceActivated = false;
            int attempts = 0;
            while (!isServceActivated && (attempts < 10))
            {
                Thread.Sleep(1 * 1000);
                try
                {
                    if (!System.Web.Hosting.HostingEnvironment.IsHosted) 
                        return;

                    var virtualPath = HostingEnvironment.ApplicationVirtualPath;
                    var physicalPath = HostingEnvironment.ApplicationPhysicalPath;
                    var svcFiles = Directory.GetFiles(physicalPath, "*.svc");

                    foreach (string svcFile in svcFiles)
                    {
                        ServiceHostingEnvironment.EnsureServiceAvailable(virtualPath + "/" + Path.GetFileName(svcFile));
                    }

                    isServceActivated = true;
                    System.Diagnostics.Trace.WriteLine(String.Format("Activated {0}", virtualPath));
                }
                catch (Exception exception)
                {
                    attempts++;
                    //continue on these exceptions, otherwise fail fast
                    if (exception is EndpointNotFoundException
                        || exception is ServiceActivationException
                        || exception is ArgumentException)
                    {
                        System.Diagnostics.Trace.WriteLine(exception.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
    }

}

-------------------------------------------------------------------------------------------------------------

File::::ServiceBusHostFactory.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.Threading.Tasks;

namespace Web.Hosting
{
    public class ServiceBusHostFactory : ServiceHostFactory
    {
    Type incomingServiceType;
    Uri[] incomingBaseAddresses;
    /// <summary>
    /// Reopen Service host on network intermittent error
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnServiceHostFaulted(object sender, EventArgs e)
    {
        ICommunicationObject faultedHost = (ICommunicationObject) sender;
        faultedHost.Abort();
        //Log failure
        bool IsSourceExist = EventLog.SourceExists(CustomEventLogInstaller.EventSourceName);
        if (IsSourceExist)
            EventLog.WriteEntry(CustomEventLogInstaller.EventSourceName,
                String.Format("Host {0} connection failed at address {1}",this.incomingServiceType.ToString(),
                        this.incomingBaseAddresses.FirstOrDefault()), EventLogEntryType.Error);
        ServiceHost host =  base.CreateServiceHost(this.incomingServiceType, this.incomingBaseAddresses);       
        host.Faulted += this.OnServiceHostFaulted;
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10)); // Sleeping here just to allow some time for the resource to come back.
        host.Open();
        //log host reopened
        if (IsSourceExist)
            EventLog.WriteEntry(CustomEventLogInstaller.EventSourceName,
                String.Format("Service bus connection reopened for {0}", this.incomingBaseAddresses.FirstOrDefault()), EventLogEntryType.Information);
    }

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
    this.incomingServiceType = serviceType;
    this.incomingBaseAddresses = baseAddresses;
    ServiceHost host = base.CreateServiceHost(this.incomingServiceType, this.incomingBaseAddresses);
    host.Faulted += this.OnServiceHostFaulted;
    return host;
    }
 }

}

================END Autostartprovider source code=======================

Uninstalling and reinstalling IIS 7.5 on Windows 7

If you want remove IIS 7.5 from your Windows 7 machine, make sure to remove IIS Hostable Web Core and Windows Process Activation Service (if already installed).

Uninstalling just IIS features and reinstalling them will not help and you will get - "An error occurred. Not all features are installed".

Here are steps-
1) Uninstall IIS, IIS HWC, WAS.
2) Restart the system
3) Rename Inetpub to Inetpub1
4) Reinstall IIS