Tag Archives:

Unit Test Internal Members

Applies to .Net application in Visual Studio 2010 and up.

Say I have C# project (call it QC project). In the project, I have class that has internal member(s) like this:

namespace QC
    public class PersonService
        public bool PromotePerson(string userName)

        internal bool FindPerson(string userName)

For unit test, I’d created separate project (call it QCTest project). Because internal members can only be accessed in same assembly, I won’t be able to invoke `QC.PersonService.FindPerson(string userName)` method from QCTest project.

To overcome this issue, in `AssemblyInfo.cs` of project being tested (in my case, QC project), simply add

[assembly: InternalsVisibleTo("QCTest")]

After rebuilding my solution, I am able to access `QC.PersonService.FindPerson(string userName)` in my QCTest project.

As an added bonus, if you use mock library like Moq or Rhino and try to mock internal members, you most likely need to add:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

What is `DynamicProxyGenAssembly2` ?

DynamicProxyGenAssembly2 is a temporary assembly built by mocking systems that use CastleProxy like Moq or NSubsitute. It is generated when the mock is needed and disposed of after the tests are finished.


Leave a comment

Posted by on January 18, 2019 in General


Tags: , , , , , ,

WebApi ExceptionHandling.ExceptionHandler Sometimes Does Not Handle Global Exception. What ?!

See previous post on how to implement ExceptionHandler.

ExceptionHandler supposed to handle exception globally, but sometimes it does not… what ?!

There are few cases where ExceptionHandler will not capture exception. One of them is when CORS is enabled, see this.

To ensure ExceptionHandling to always capture exception, try:
1. If inherit from ExceptionHandler, override its method `ShouldHandle()`.

        public override bool ShouldHandle(ExceptionHandlerContext context)
            return true;

2. Instead of ExceptionHandling.ExceptionHandler, inherit and implement it’s interface `ExceptionHandling.IExceptionHandler`.

Leave a comment

Posted by on November 2, 2018 in General


Tags: , , , ,

Application Insights Intrumentation Key in Web.config

When using Azure Application Insights in ASP.Net application, by default, Visual Studio insert IntrumentationKey in ApplicationInsights.config.

To allow multiple environments tracking, move IntrumentationKey to Web.config by following this steps:

  1. Remove IntrumentationKey from ApplicationInsights.config. If you have MVC application, don’t forget to modify ApplicationInsights’ script (usually in View\Shared\_Layout.cshtml), replace:

    {instrumentationKey:"your instrumentation key"}


  2. Add new app settings for IntrumentationKey in Web.config under <appSettings>

    <add key="InstrumentationKey" value="your instrumentation key" />
  3. In Global.asax.cs, Application_Start() method, add:

    Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = System.Web.Configuration.WebConfigurationManager.AppSettings["InstrumentationKey"];

That’s it for the configuration changes. Everything else is the same including tracking custom event or page view.
With this configuration, you will be able to define InstrumentationKey in Release management for each environments.

1 Comment

Posted by on May 18, 2018 in General


Tags: , , , , , ,

String as a Reference Type and Immutable

In .Net, string object is reference type, however when we pass the value like this

void Main()
    string s = "Test";

    Console.WriteLine("TestString: " + s); // Output is: TestString: Test

public static void TestString(string s)
    s = "TestString";

Why is the output “TestString: Test”, we know for sure string is a reference type and we assign new value to our parameter in TestString() method.

Turn out, beside being a reference type, string is also an immutable, which mean its value can’t be modified or changed. So when we assign new value to our parameter in TestString() method, it actually create new string object. It acts like value type but it isn’t a value type, it’s a reference type with immutable behavior.

Leave a comment

Posted by on May 15, 2018 in General


Tags: , , , ,

State and Session Management in WIF (.Net 4.5)



var sessionToken = new SessionSecurityToken(principal, TimeSpan.FromHours(10));



WS2007FederationHttpBinding supports WS-SecureConversation.

Leave a comment

Posted by on May 23, 2013 in General


Tags: , , ,

%d bloggers like this: