Archive for the ‘CodeZone’ category


git extensions–syntax error near unexpected token

gitExtensions  GitExtensions is a GUI, Visual Studio plugin and shell extensions for Git.

Problem :

Whenever you use newly installed GitExtensions to pull ne repo from remote repo you may face below error.

Syntax error near unexpected token ‘(‘

Below is full error log :

"C:\Program Files (x86)\Git\bin\git.exe" push --recurse-submodules=check 
--progress "origin" refs/heads/master:refs/heads/master
\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\" 
get: -c: line 0: syntax error near unexpected token `('
\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\" 
get: -c: line 0: `\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\" get'
\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\" 
erase: -c: line 0: syntax error near unexpected token `('
\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\" 
erase: -c: line 0: `\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\" erase'
fatal: Authentication failed for 'https://<username>@bitbucket.org/<username>/<repo>.git/'
Done

Solution :
The problem is syntax error in default gitconfig file. You can find default config file in C:\Users\<UserName>\gitconfig
Try to find entry below for [credential] section. It looks like below 

[credential]
     helper = !\\\”C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\\\”

Notice the 3 slashes after ! in the start of entry and 3 slashes at the end of the entry. Remove two unnecessary slashes so it will look like this :

[credential]
     helper = !\”C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\”

Go and run your command again, it should work.

Regards


git–push large commits over http fail

Problem : When you try to commit a large size file or trying to commit huge amount of data in single commit you may face this issue. This issue can also be encountered when you are trying to move your source control from other tools like VFS, SVN to git as you will have to move your existing source files in huge commits. Normally you are greeted with below error codes :

fatal: The remote end hung up unexpectedly error: RPC failed; result=22, HTTP code = 413

$ git push origin master
Username for '<HOST>: <USER_EMAIL>
Password for 'http://<USER_EMAIL>@<HOST>':
Counting objects: 3322, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3010/3010), done.
Writing objects: 100% (3322/3322), 29.87 MiB | 8.07 MiB/s, done.
Total 3322 (delta 2379), reused 371 (delta 249)
error: RPC failed; result=22, HTTP code = 413
fatal: The remote end hung up unexpectedly
fatal: recursion detected in die handler
Everything up-to-date

Solutions : Try below solutions as the problem can be caused by several factors :

1)  Change protocol from http(s) to git/ssh

Try to move your repo’s remote connection url from http(s) to git/ssh. You can add remote for your repository using add remote command.$ git remote add origin [email protected]:username/project.git

2) Increase git http post buffer size

The next place to look is git http buffer size settings. To increase git http post buffer size 
you can use below command in your git command prompt
git config http.postBuffer 524288000

3) Increase server (nginx) buffer size

If above does not work than you should be now looking at web server buffer size. git over http will use some kind of web server to process http requests. For example in GitLab it will be nginx by default. To increase buffer size in nginx you need to change the config parameter client_max_body_sizeAdd it to /etc/nginx/sites-available/gitlab on ( in Ubuntu ). In CentOS it be should in /etc/nginx/nginx.conf.

http {
    # ...
    client_max_body_size 16M;
    ...#  }

Change client_max_body_size to whatever you want.


Asp.Net website : Exclude folder from compilation

Problem :

We used to have asp.net website projects, which does not come with project files like .csproj. We also have fckEditor folder in our project. So now every time when I build my project it unnecessarily also build fckEditor folder also which is really annoying to me ( I hate when something takes too long to build :) ).

If you are working on Asp.Net Web Application then the solution is very simple, you just have to “Exclude it” from the project. BUT we are working on WebSite project not web application project.

Solution :

In fact it is quiet simple. Just hide the folder, I mean, Make the folder ( the one that you don’t want to be included in compiling process) hidden and it will not be there in the build list.  So simple. :)


Winforms splash screen – Great work by Tom

Recently I came across a great splash screen for Winforms developed by Tom Clement on CodeProject

I loved this splash screen. Tom has intially created this splash screen using .Net 1.0. Butthis great piece of code had some threading issues with .Net 2.0.

I have seen a lot of comments of users that they are facing few minor problems ( flickering, cross thread operation, use of registry ) and also other users have submitted respective solutions also.

I have just incorporated all those changes in .Net 3.5 version of this great splash screen. I have not written any code here, I have just read the comments and implemented suggestions they have displayed there.

Again Great work by Tom and other contributors who has helped this great splash screen by contributing their valuable suggestions ans solutions in comments.

Please find the attached zip file which contains source code of this splash screen for winforms modified to work with .Net 3.5

SplashScreen.zip (193.83 kb)


Global.asax – using directive, Debugging

A quick ASP.Net tip

1) If you want to use any custom namespace or class in your global.asax you can use following syntax to use corresponding namespace.

<%@ Import Namespace="TristonFramework.Web" %>

or

<%@ Import Namespace="System.Web" %>

 

2) When you put a breakpoint in Global.asax and if it not firing than consider following tips

   a. Make sure you are using Debug environment ie <compilation debug="true">

    Warning : make sure you have switched compilation element to false before using the web.config in production. Otherwise it has a huge performance panelty. You can read about it here.

 
b. Try to edit the Global.asax by just hitting few enters in blank space. Sometimes when Global.asax is not changed after last build, asp.net does not consider it for debugging. i dont know why ? But this trick works almost always.

 

Thanks and Regards,

Mahin Gupta


Checking Internet Connection

So basically there are 3 ways to do it. We will check each of them and will find the best out of them.

 1) Send WebRequest

                private static bool IsInternetConnected()
        {
            HttpWebRequest req;
            HttpWebResponse resp;
            bool bRet = false;
            try
            {
                req = (HttpWebRequest) WebRequest.Create("
http://www.google.com");
                resp = (HttpWebResponse) req.GetResponse();
                bRet = resp.StatusCode.ToString().Equals("OK");
                resp.Close();
            }
            catch (Exception exc)
            {
                bRet = false;
            }
            finally
            {
                // resp.Close();
            }
            return bRet;
        }

Remarks : You need System.Net namespace to use HttpWebrequest and HttpWebresponse.

 

2) InternetGetConnectedState()  – wininet api

 

       private static bool APIInternetGetConnectedState()
        {
            bool bRet = false;
            try
            {
                int Desc = 0;
                bRet = InternetGetConnectedState(ref Desc, 0);
            }
            catch (Exception exc)
            {
                bRet = false;
            }
            return bRet;
        }

Remarks : You need to create extern function to use this api and have to use runtime interop assemblies. So you need to reference namespace  

        System.Runtime.InteropServices and include following code in your class definition.

        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(ref int description, int ReservedValue);

 

3 )InternetCheckConnection – wininet api

        private static bool APIInternetCheckConnection()
        {
            bool bRet = false;
            try
            {
                bRet = InternetCheckConnection("
http://www.google.com", 0x01, 0);
            }
            catch (Exception exc)
            {
                bRet = false;
            }
            return bRet;
        }

Remarks : You need to create extern function to use this api and have to use runtime interop assemblies. So you need to reference namespace  

        System.Runtime.InteropServices and include following code in your class definition.

        [DllImport("wininet.dll")]
        private extern static bool InternetCheckConnection(string description,int flags, int ReservedValue);

 

Comparision :

Table shown below compares all the methods. Interestingly InternetGetConnectedState() reports connected even if we are not connected to Internet, and that is very natural since it has no way to detect if you are connected to actual Internet ( ie particular server ) unless you send a request to web server. You can find a good explanation about it here.Again you have to use interop services to use both this api which can cause performance issues.

Using Google server as server to request has also pros and cons. 1) Google is used heavily – but again it effectively uses CDN, so I don't think this argument holds a point. 2) You are almost very sure that Google server will be up and running in almost all circumstances, but yet for the sake of precaution use this as parameter and get it from either Database or App.Config

So after all of this for the time being I will go with Method 1 ie by using Send Web Request.

Method No Connection Connected to Intranet Connected to Internet
Send WebRequest false (0 ms) false (0 ms) true (550 ms)
InternetGetConnectedState false ( 30 ms ) true ( 0 ms ) true ( < 10 ms )
InternetCheckConnection false ( more than 2 seconds ) false ( 400 ms ) true ( 78 ms )

 

There exist other methods also to check Internet connectivity ie Create a socket connection to Web server, Use Ping or tracert, Open a telnet connection but many of this can create problems when firewall is on. yet, I am still interested in finding a better method than this. Please let me know if you know one.

 

Best Regards,

Mahin Gupta