.NET core configuration array with Bind

New configuration system of .NET core is really nice, but it does not works very well with arrays, I have a configuration object that has an array of FirewallRules

        public FirewallRule[] Rules { get; set; }

This rule is simply composed by four simple properties.

public class FirewallRule
{
    internal FirewallRule()
    {
    }

    public FirewallRule(string name, int udpPort, int tcpPort, string secret)
    {
        Name = name;
        UdpPort = udpPort;
        TcpPort = tcpPort;
        Secret = secret;
    }

    public String Name { get; set; }

    public Int32 UdpPort { get; set; }

    public Int32 TcpPort { get; set; }

    public String Secret { get; set; }
}

Ok, nothing complex, now I’m expecting to being able to write this json configuration file to configure a single rule.

{
  "Rules" : [
    {
      "Name": "Rdp",
      "UdpPort": 23456,
      "TcpPort": 3389,
      "Secret": "this_is_a_secret"
    }
  ]
}

And being able to configure using standard Bind utilities of .NET configuration extensions.

            const string testValue = @"
{
  ""Rules"" : [
    {
      ""Name"": ""Rdp"",
      ""UdpPort"": 23456,
      ""TcpPort"": 3389,
      ""Secret"": ""this_is_a_secret""
    }
  ]
}";
            using var ms = new MemoryStream(Encoding.UTF8.GetBytes(testValue));
            IConfiguration configuration = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonStream(ms)
                .Build();

            var config = new Configuration();
            configuration.Bind(config);

Now I’m disappointed because the test fails because in Rules array I have only a single null element. It seems that Bind does not work perfectly with arrays or am I doing something wrong.

The solution was quite simple, I created a method inside the Configuration object that will allows binding from a standard .NET Core IConfiguration object

public void Bind(IConfiguration configuration)
{
    configuration.Bind(this);

    List<FirewallRule> rulesList = new List<FirewallRule>();
    var rules = configuration.GetSection("Rules").GetChildren().ToList();
    foreach (var rule in rules)
    {
        var firewallRule = new FirewallRule();
        rule.Bind(firewallRule);
        rulesList.Add(firewallRule);
    }

    Rules = rulesList.ToArray();
}

As you can see I explicitly get the section Rules, and for each child I explicitly created a FirewallRule and populate properties with standard Bind() method. And everything worked perfectly.

I’am a little bit puzzled, because I suspect that I’m doing something wrong because base Bind() method should work out of the box, but at least with this fix all tests are still green.

Gian Maria.

Error when .NET461 full framework references .NETStandard nuget packages

After updating MongoDb driver in a C# big project I start having a problem in a Web Project where we have this error after a deploy in test server (but we have no error in local machine of developers)

An assembly with the same identity ‘System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ has already been imported. Try removing one of the duplicate references.

This happens because MongoDB driver > 2.8 reference in the chain System.Buffer 4.4.0. If you take an empty .NET Full Framework 4.6.1 and references MongoDb 2.8.x driver you can verify that you have

image

Figure 1: System.Buffers reference for the project

This seems an innocuous reference, but if you look closely to what is inside System.Buffer you can verify that there is no package for full framework. Just check in the packages directory to verify that there is no .NET full framework package.

image

Figure 2: There is no full framework package inside System.Buffers

This imply that, when you build the project, in the bin directory you will find all the dll of netstandard project, because actually you are referencing netstandard 2.0 dll.

image

Figure 3: All netstandard dlls included in the bin directory of the project.

Now you have a problem, because you have netstandard runtime included in your project, most of the time it is safe, but there are situation where this can generate errors.

You can find the issue in GitHub #39362, that in turn is a duplicate of #39362 (my original issue).

Actually, if you want to avoid problems, you should be 100% sure not to reference any netstandard dll in a .NET 4.6.1 full framework project, or you should use full framework 4.7.2 or greater.

Gian Maria.

Error with dotnet restore, corrupted header

I’m trying to compile with dotnetcore 2.0 a project on Linux, but I got this strange error when I run the dotnet restore command.

image

Figure 1: Error restoring packages

The exact error comes from NuGet.targets and tells me: a local file header is corrupt and points to my solution file. Clearly this project builds just fine on another computer.

Since I’m experiencing intermittent connection, I suspect that nuget cache can be somewhat corrupted, so I run this command to clear all caches.

dotnet nuget locals --clear all

This clear all the caches. After this command run, I simply re-run again the dotnet restore command and this time everything went well.

Gian Maria.

.NET core 2.0, errors installing on linux

.NET Core 2.0 is finally out, and I immediately try to install it on every machine and especially in my linux test machines. In one of my Ubuntu machine I got an installation problem, a generic error of apt-get and I was a little bit puzzled on why the installation fail.

Since in windows the most common cause of .NET Core installation error is the presence of an old package (especially the preview version), I decide to uninstall all previous installation of .NET core on that machine. Luckly enough, doing this on linux is really simple, first of all I list all installed packages that have dotnet in the name

sudo apt list --installed | grep dotnet

This is what I got after a clean installation of .NET core 2.0

image

Figure 1: List of packages that contains dotnet in the name

But in that specific virtual machine I got several versions and a preview of 2.0, so I decided to uninstall every pacakge using the command sudo apt-get purge packagename, finally, after all packages were uninstalled I issued a sudo apt-get clean and finally I tried to install again .NET core 2.0 and everything went good.

If you have any installation problem of .net core under linux, just uninstall everything related with dotnet core with apt-get purge and this should fix your problems.

Gian Maria.