Category: .NET Core

InternalsVisibleTo usage

I do not know if you know  InternalsVisibleToAttribute  .

I have used on a project , that deals with World Bank API to get information

Let’s take an example: Countries:

I have had a class CountriesRepository, that deals with transforming countries Json from WorldBank to Country classes. How can I test it WITHOUT relying on reading data from the internet ?


1. Make an interface to grab JSON , IJSonData

    public interface IJsonData
        Task<string> JsonData(int page = 1);

2. Make a class JSONCountry that reads JSON from HTTP and implements IJSonData

3.  Make the CountryRepository class with 2 constructors  – one with IJSonData and the default calling JSONCountry

[assembly: InternalsVisibleTo("WorldBank.Test")]
namespace WorldBank.Repository
    public class CountriesRepository
        private IJsonData data;
        public CountriesRepository():this(new JsonCountries())

//this is internal - could not see by outside projects with exception of WorldBank.Test
//see InternalsVisibleTo above
        internal CountriesRepository(IJsonData data)
   = data;

Optional: when testing manually, save the json into same files on the hard disk – you will need for point 5

4. The code that will use CountryRepository in official way will use the default – will not see the one with IJSonData

var c = new CountriesRepository();
var data = c.GetCountries().Result;

5. The testing code will define another class JSONFromHard that implements IJSonData and reads data from Hard

class JsonFromHard : IJsonData
//code to read files from disk 
// you have saved those when testing manually 
//see point 2 the optional part 

6. Because I have defined InternalsVisibleToAttribute to the WorldBank.Test, the Test dll can see the constructor with IJSonData  and then it class with JSONFromHard   – and I have no dependecy on the Http


public void GetAndInterpretData()
            //uses [assembly: InternalsVisibleTo("WorldBank.Test")]
            var c = new CountriesRepository(new JsonFromHard("Countries"));
            var data = c.GetCountries().Result;
            Assert.Equal(218, data.Length);

You can download the project from  to see working.

ImageTagHelper in .NET Core–add data URI scheme

The ImageTagHelper in ASP.NET Core MVC ( is lacking one attribute that I find somehow useful: a data uri to embed the image as base64 ( )

I was curious about how fast it will be to make such a modification on my PC.

So the process was:

  1. Downloading branch rel/1.1.1 from as a zip file
  2. Unlock the file, unpack, run  MVC.sln with VS2015
  3. Compile the entire project.
  4. See tests  ( also run dotnet test in the Mvc-rel-1.1.1\test\Microsoft.AspNetCore.Mvc.TagHelpers.Test to figure what was missing)
  5. Started modifying the file ImageTagHelper( adding
     public bool RenderBase64 

    and creating FileBase64ContentProvider.cs with the (approximately) same content as FileVersionProvider

  6. Adding tests to  mvc-rel-1.1.1\test\microsoft.aspnetcore.mvc.taghelpers.test\imagetaghelpertest.cs  ( also modifying mocker –
    for my purposes, to read the file, I need file length

                    .Setup(m => m.CreateReadStream())                
                    .Returns(() => new MemoryStream(Encoding.UTF8.GetBytes("Hello World!")));
    mockFile.SetupGet(f => f.Length).Returns(12);
  7. Modifying HtmlTargetElement attribute from ImageTagHelper
  8. Adding “Microsoft.AspNetCore.StaticFiles”: “1.1.0”, and app.UseStaticFiles();  to HtmlGenerationWebSite and going to http://localhost:5551/HtmlGeneration_Home/image

And that was all. Now I will see how to fork and submit changes 😉

Later Edit: Submit a bug, fork, pull request. Approved.

ASP.NET Core and Windows Authentication

Just a reminder of how to configure ASP.NET Core and Windows Authentication

1. Configure IIS

2. Go to properties folder, file launchSettings.json and modify

"iisSettings": {     "windowsAuthentication": true,     "anonymousAuthentication": false,    //other data

3. When you compile and run , the Web.Config will be modified for forwardWindowsAuthToken :

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="true" stdoutLogEnabled="false" />