Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving references in ASP.Net 5, "IDisposable is ... not referenced"

I did the following:

  • Created a new Web.Api project: "WFW3". I used the "Web API" template under ASP.Net 5.
  • I created a new class library, "Foo.Domain", again using ASP.Net 5.
  • I added a reference to it from the API project.
  • I installed Neo4j.Driver (a portable class library) from Nuget into the Foo.Domain project. Neo4j-NuGet

Everything seemed fine to this point. Everything compiled, though it did nothing.

In Foo.Domain I created a class with a method that referenced the GraphDatabase class inside a 'using' statment. This is where it broke.

I received this error message (and others like it):

The type 'IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Foo.Domain..NET Framework 4.5.1 C:\dev\WFW3\src\Foo.Domain\FooRepository.cs

My understanding is that binding redirects are not available in ASP.Net 5. Is this correct? How can I solve this issue not referencing the correct version of System.Runtime? Items found in System.Runtime are available to me. It seems to be looking for an older version of the System.Runtime from within the Neo4j.Driver.V1 assembly. I tried the solution found here (Nathan's answer), but then it started complaining that I was trying to import two different types of the Runtime library and I needed to remove one. But which one should I remove, and how?

API project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Foo.Domain": "1.0.0-*",
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

Foo.Domain project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Foo.Domain": "1.0.0-*",
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

FooRepository code (in Foo.Domain):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Neo4j.Driver.V1;

namespace Foo.Domain
{
   public class FooRepository: IFooRepository
    {

        public Foo GetById(string Id)
        {
            // The next lines inside the 'using' are getting the error.
            using (var driver = GraphDatabase.Driver("http://localhost:7474/"))
            using (var session = driver.Session())
            {
                var result = session.Run("CREATE (n) RETURN n");
            }
            return null;
        }
    }
}
like image 708
MalcLear Avatar asked May 02 '16 02:05

MalcLear


1 Answers

Referencing this similar question : What do I do when ASP.NET 5 (vNext) can't redirect bindings?

The problem in that question appeared to be related to the fact that the dnxcore50 framework was included in the project.

"frameworks": {
    "dnx451": { },
    "dnxcore50": { }
}

and removing it solved the issue.

"frameworks": {
    "dnx451": { }
}

This worked in their case and should also work for you. I believe you are getting this because of omitting dependencies required by the Core CLR (dnxcore50) framework as referenced here:

Frameworks

This snippet will build for Desktop (dnx451) or Core CLR (dnxcore50). Core CLR has many extra dependencies as the packages that make up the BCL need to be referenced...

{
  "frameworks": {
    "dnx451": {},
    "dnxcore50": {
      "dependencies": {
        "System.Collections": "4.0.0.0",
        "System.Collections.Concurrent": "4.0.0.0",
        "System.ComponentModel": "4.0.0.0",
        "System.Linq": "4.0.0.0",
        "System.Reflection": "4.0.10.0",
        "System.Runtime": "4.0.20.0",
        "System.Runtime.InteropServices": "4.0.10.0",
        "System.Threading": "4.0.0.0",
        "System.Threading.Tasks": "4.0.0.0"
      }
    }
  }
}

Which is why when it was removed from the previous question the project worked. Most of the other posts I saw related to this issue also suggested removing dnxcore50

You can also look at this for a better understanding...

Framework-specific dependencies

You can also add dependencies for a particular framework like this:

{
  "frameworks": {
    "dnxcore50":{
      "dependencies":{
        "System.Runtime": "4.0.0.0"
      }
    },
    "dnx451":{}
  }
}

In the above example, the System.Runtime dependency is only needed for the dnxcore50 target, not dnx451. It is often the case that you will have extra dependencies on Core CLR, because there are packages that you need to depend on in Core CLR that are part of .NET 4.5.x.

Note

While it is technically true that you do not need the System.Runtime package on .NET 4.5.1, it also doesn’t matter if you add it as a top level dependency. Each of the System.* packages will work as a top level dependency. So, you don’t always have to have this separation. You could add System.Runtime as a top level dependency and it will not impact your application when on .NET 4.5.1.

Additional reference material:

Diagnosing dependency issues with ASP.NET 5

How can I diagnose missing dependencies (or other loader failures) in dnx?

like image 174
Nkosi Avatar answered Oct 15 '22 23:10

Nkosi