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.

Publishing web project on disk during build

During a build you can ask MsBuild to deploy on build using the switch /p:DeployOnBuild=true as I described in previous posts. This is mainly used to deploy the site on IIS thanks to WebDeploy, but you can also use WebDeploy to deploy on a disk path. The problem is that the path is stored in publication settings file, but what about changing that path during a Build?

The answer is simple, you can use the /p:publishUrl=xxx to override what is specified inside the publication file and choose a different directory for deploy. Es.

msbuild WebApplication1.csproj /p:Deploy
OnBuild=true /p:PublishProfile=Profile1 /p:publishUrl=c:\temp\waptest

Thanks to this simple trick you can instruct MsBuild to store deployed site in any folder of the build server.

Gian Maria.

Detect Client-side reconnection with SignalR

Signalr is really good on keeping alive the connection between server and the client and make sure that the client automatically reconnect if there are connection issue. To verify this you can write a simple test with a simple hub that each second broadcasts to all clients current server timestamp with a simple timer.

 private static Timer testTimer = null;
 static MyHub()
 {
     testTimer = new Timer();
     testTimer.Interval = 1000;
     testTimer.Elapsed += (sender, e) =>
     {
          var context = GlobalHost.ConnectionManager.GetHubContext();
          context .Clients.All.setTime(DateTime.Now.ToString());

Now you can simply reference the hub on a page, register for the setTime method and watch the page dynamically update each second.

function SignalrTestViewModel(option) {

    var self = this;
  
    //signalr configuration
    self.myHub = $.connection.myHub;

    self.serverTime = ko.observable('no date from server');

    self.myHub.client.setTime = function (time) {
        self.serverTime(time);
    };

This is a simple KnockoutJs view model, you can now bind a simple span to the property serverTime and watch everything works.

image

Figure 1: Web page automatically updated from the server

The interesting part is that you can now kill the w3wp.exe process from the task manager (if you are using IIS) or whatever hosting server you are using, and you can verify that almost immediately w3wp.exe process is bring to life again and the timer continues to count. This happens because when the client detect that the server is dead, it tries automatically to reconnect, then IIS creates another worker process and everything starts working again.

The only drawback is that the server had lost every volatile information it has collected during its life. In my situation each clients initialize some javascript code calling certain method on the hub Es. RegisterViewRoom, and I keep such information in static variables inside the hub. This works, except that if the server process goes down for whatever reason (scheduled IIS worker process recycle) these information are lost. I do not want to bother with storing data on the server, my typical situation is no more than 5 clients at a time and I want the simplest thing that could possibly work.

The simplest solution to this problem is letting the client javascript code detect when a re-connection occurs, whenever there is a reconnect, the client can call registration function again. Registration call is idempotent so there is no problem if the reconnection happens because of connectivity problem and not for a restart of the server. To detect in signalr a re-connection you can use this piece of code.

 $.connection.hub.stateChanged(function (change)
    {
        self.signalrState(change.newState);

        if (change.newState === $.signalR.connectionState.reconnecting)
        {
                self.messageHub.server.registerViewRoom(self.conversationId());
        }
  });

This simple code is used to detect when the state of the connection changed, I store this information inside a KnockoutJS View Model variable, to be informed of the actual status, then I simply detect if the new state is reconnecting and I simply call initialization function on the server to re-register information for this client connection.

Signalr is really one of the most powerful and interesting Javascript library I worked with in the past years :).

Gian Maria