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.