Thursday, March 28, 2019

How to upload local project from windows to github

First you need to install Git for Windows, once it's done, using Command Prompt and enter following commands:

cd <the folder>
git init
git add .   //for all files under the folder
commit -m "adding files" git remote add origin https://github.com/<your-user-name>/<your-repository-name>.git
git push -u origin master

how-to-upload-a-project-to-github

Tuesday, May 22, 2018

Process with an ID #### is not running on Visual Studio 2015

When I installed both Visual Studio 2013 and 2015 on my computer,  and use 2013 to open my visual studio web project and doing my test/run at first without problem, and then use 2015 to open the same project and choose to debug, there is error "Process with an ID #### is not running" on Visual Studio 2015. I google the issue and there is following solution:

  1. Delete the \Documents\IISExpress folder using the following console command:
  2. Delete the applicationhost.config file which is placed within the \.vs\Config\ folder in your Visual Studio project root folder.
I tried both and the issue is gone. I think it's because 2013 had created some files which was used by 2015 but have issues.

https://www.ryadel.com/en/process-id-not-running-visual-studio-2015-fix/

Tuesday, May 8, 2018

Git Bitbucket Stage VS Unstage

According to Johannes Kilian, "the staging area is a container where git collects all changes which will be part of the next commit". Inside bitbucket, when you staging the file, you put the uncommited file into the staging area. "The next git commit will transfer all items from staging into your repository".

When you have multiple bugs to be fixed, you don't want to commit all your changes, you select the code you want to commit and do staging them. Through this way you will have a clean commit specific for one bug.

You can choose complete files, hunks (part of one file), or single line for staging.

https://community.atlassian.com/t5/Sourcetree-questions/Staged-vs-Unstaged/qaq-p/127916

Monday, April 23, 2018

UNIT TEST in ASP.Net MVC Part II - How to handle App_GlobalResources


Traditional ASP.Net approaches to Localization and Internationaliazation is to use one special App_GlobalResources folder to hold resource files and Visual Studio will use GlobalResourceProxyGenerator to generate a strongly typed internal class to wrap all internal resources.

Note those global resources are not embeded inside the project dll file. They actually were stored inside one special assembly named App_GlobalResources.dll so that the ASP.Net views can reference them. This is fine but the problem is the special assembly was created by ASP.Net runtime, there is no such assembly during the compilation, thus when you tried to execute your unit testing against the resources, following error will happen:

"Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.":"App_GlobalResources" System.IO.IOException {System.IO.FileNotFoundException}

The solution is to create one custom folder for your MVC project and put your resource files to that folder. For each resource file, update the file property as following:


The reason to change the Custom Tool to PublicResXFileCodeGenerator is because this tool will make the resource file available for your ASP.Net views. Alternately, you can also set the resource file to public inside the resource editor to do the same job.

Resource Files and ASP.NET MVC Projects

Friday, April 20, 2018

UNIT TEST in ASP.Net MVC Part I - How to add Unit Test project and testing private or internal properties

Unit testing can greatly improve your confidence on your own code. Visual Studio has included unit testing framework since version 2015. You can add unit test project through following steps: right click on your solution, and choose Add, and choose New Projcet, Visual Studio will popup a window of model project, choose test under c# project on the left pane,  an unit test project will be added into your solution.

After you added the test project, you need to Add Reference for your project which has the object to be tested. Note if the attributes of the object you want to test are set as Internal, when you are trying to reference your object to test, system will promot following error:

'MyTest' is inaccessible due to its protection level

You need a special trick because your test project can't see the internal attribute of another project.

using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("MyTests")]
Add the last one to the project info file, e.g. Properties\AssemblyInfo.cs,for the project which has the object you want to test, while the "MyTests" is the name of your unit test project assembly. You can find your unit test project assembly name inside your unit test project info file, namely, AssemblyInfo.cs.

Note if you still experienced the error of "inaccessible due to protection level", you need to check the test project properties, in most cases, the project assembly name is different than what you thought. 

Now your unit test project can see the object to be tested.

Note if you still get
You can add

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using YourApplicationToTest;

namespace YourTest
{
    [TestClass]
    public class UnitTestl
    {
        [TestMethod]
        public void TestMethodl()
        {
            YourClassToTest ct= new YourClassToTest();
            Assert.AreEqual( ct.Name, "I need test!" );
        }
    }
}
Note if you want to test the private methods or attributes of your testing object, please use following:
Class target = new Class();
PrivateObject obj = new PrivateObject(target);
var retVal = obj.Invoke("PrivateMethod");
Assert.AreEqual((int)obj.GetField("PrivateAttribute"), 0);
Assert.AreEqual(retVal, expectedVal);
For test the private static attributes of your object, use following:
PrivateType myType = new PrivateType(typeof(MyStaticClass));

myType.SetStaticFieldOrProperty("isMyTypeAttribute", false);
UNIT TESTING in Asp.Net: Complete Tutorial
C# “internal” access modifier when doing unit testing
InternalsVisibleTo attribute isn't working
Unit testing private methods in C#
InternalsVisibleToAttribute Class
How to access a Static Class Private fields to unit test its methods using Microsoft Fakes in C#
Unit testing in C# of private-static method accepting other private-static method as a delegate parameter

Wednesday, April 18, 2018

How to create a Restful API with authentication in Jwt

Asp.Net page usually do authentication and authorization with session. However, Restful means stateless, for doing so, we need to use Restful HTTP services to do authentication and authorization.

Sunday, March 12, 2017

A good start to learn Swift 3


I found Mac is much better to be used than windows system. The mouse is super good without wheel as the top part of mouse is actually touchable. To do copy/paste you need to use cmd key + C/V.

Following note are from what I learned for swift 3:

Swift simplified memory management using ARC - Automatic Reference Counting.

Using let for constant, var is used for variable:

let oneMillion = 1_000_000
You can use min and max properties for integer type to get maximum and minimum value.

// 定义类型别名 typealias
typealias AudioSample = UInt16 

// optional binding,只有当yyy是optional的时候才可以这样用。optional的yyy非空时为真,将yyy中的值取出赋给xxx,空时(nil)为假;

if let xxx = yyy {
     // do something
} else {
     // do other thing
}

// decompose一个tuple时,对于不想使用的元素用’_’接收
let http404Error = (404, "Not Found")
let (justTheStatusCode, _) = http404Error
println("The status code is \(justTheStatusCode)")
// prints "The status code is 404

let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int”,因为toInt()可能会失败(比如“123a”)导致返回nil
!== and === are identity operators and are used to determine if two objects have the same reference.
var arr1 = [1, 2, 3]
var arr2 = arr1
arr2[0] = 10;
arr1     // [10, 2, 3]
arr2     // [10, 2, 3]
arr1 === arr2  // true


把《The Swift Programming Language》读薄

Introducing Firebase with Swift 3: Login and Sign Up