Wednesday 22 June 2016

Robocopy

Robocopy is remarkably different from other copy commands. It is often used in msbuild projects but there are a bunch of caveats which would appear non-intuitive to a first time user. The reason people consider using it is because it allows retries and after parameterised wait intervals.

The default syntax of robocopy takes folders as parameters and copies source folder to destination. To copy a file, the syntax is the following:

robocopy C:\source C:\destination filename.ext

Notice that filename is not part of the source here. Also, because of this limitation a file can not be copied to another location with a different name. Due to this, in msbuild projects, you need to copy the file using robocopy (assuming you use robocopy) and then rename it.

Monday 20 June 2016

Detailed changes of a stash

I have been using git for a few years now. Stashing is one of the very frequent operations I do on git. So, on a number of occasions, I find myself with the need of looking into what the stash contains (Perhaps, I should branch more and stash less). The following command, which shows the detailed changes in a particular stash, does come in handy in those situations.

 git stash show -p stash@{1}

Tuesday 14 June 2016

Bypassing SSL in WebRequests

Using C#, there are two prevalent ways of making HTTP requests:
- using HttpWebRequest
- using HttpClient

When you have a cancellation token that you want to use, only the later provides the facility for that. So, there are scenarios for each method to be adopted is what I am getting at.

While accessing HTTPS sites whose SSL certificates are not trusted, we manually allow access in case of the browser. The same can b achieved via code using the following methods.

1. Setting ServerCertificateValidationCallback for the request.
This method only for the first method of making HTTP requests.

// provide a custom callback
request.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(SSLValidationDelegate);

// define the callback to allow always
private static bool SSLValidationDelegate(Object o, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors errors){
    return true;
}


2. Setting ServerCertificateValidationCallback for the ServicePointManager class.
This method essentially does the same but instead of doing it at the request level it does that for all.

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
HttpClient client = new HttpClient();
CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(1000);
string url = "https://localhost/some/path";
HttpResponseMessage response = client.GetAsync(url, cts.Token).Result;



So when a cancellation needs to be used,we have to take the second route.