The type 'xxxx' is defined in an assembly that is not referenced. System.Runtime.
If you recognize the error from the title of this post, you can jump to the solution.
The problem
I have a ASP.NET 5 solution with two projects, a web application project and a class library project. After adding the class library I was very surprised to get this error -
The type 'IEnumerable<>' 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'``.
All I was doing was querying an Entity Framework context for some simple data and returning the result as an IEnumerable.
This is not the code but is close enough.
var members = _context.Members.Where(m => m.FirstName.Contains(firstName));
To verify that I wasn’t messing up something very simple I tried the same code from the web api controller in the web application, it compiled and worked fine. I looked in the references for both projects for an indication that I had left something out of my class library but could find nothing.
I checked the project.json
for both and messed around in nuget for a while but all looked fine.
The project.lock.json files
Finally, I had a look in the project.lock.json
files and noticed this section in the web application file.
1{
2 "locked": false,
3 "version": 2,
4 "targets": {
5 "DNX,Version=v4.5.1": {
6 "EntityFramework.Core/7.0.0-rc1-final": {
7 "type": "package",
8 "dependencies": {
9 "Ix-Async": "1.2.5",
10 "Microsoft.Extensions.Caching.Abstractions": "1.0.0-rc1-final",
11 "Microsoft.Extensions.Caching.Memory": "1.0.0-rc1-final",
12 "Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
13 "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc1-final",
14 "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
15 "Microsoft.Extensions.Logging.Abstractions": "1.0.0-rc1-final",
16 "Microsoft.Extensions.OptionsModel": "1.0.0-rc1-final",
17 "Remotion.Linq": "2.0.1",
18 "System.Collections.Immutable": "1.1.36"
19 },
20 "frameworkAssemblies": [
21 "Microsoft.CSharp",
22 "mscorlib",
23 "System",
24 "System.Collections",
25 "System.ComponentModel.DataAnnotations",
26 "System.Core",
27 "System.Diagnostics.Debug",
28 "System.Diagnostics.Tools",
29 "System.Globalization",
30 "System.Linq",
31 "System.Linq.Expressions",
32 "System.Linq.Queryable",
33 "System.ObjectModel",
34 "System.Reflection",
35 "System.Reflection.Extensions",
36 "System.Resources.ResourceManager",
37 "System.Runtime",
38 "System.Runtime.Extensions",
39 "System.Threading"
40 ],
There in the project.lock.json
for the web application project is "System.Runtime"
in the "frameworkAssemblies"
section.
But the same section in the class library’s file did NOT have a "System.Runtime"
.
1{
2 "locked": false,
3 "version": 2,
4 "targets": {
5 ".NETFramework,Version=v4.5.1": {
6 "EntityFramework.Core/7.0.0-rc1-final": {
7 "type": "package",
8 "dependencies": {
9 "Ix-Async": "1.2.5",
10 "Microsoft.Extensions.Caching.Abstractions": "1.0.0-rc1-final",
11 "Microsoft.Extensions.Caching.Memory": "1.0.0-rc1-final",
12 "Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
13 "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc1-final",
14 "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
15 "Microsoft.Extensions.Logging.Abstractions": "1.0.0-rc1-final",
16 "Microsoft.Extensions.OptionsModel": "1.0.0-rc1-final",
17 "Remotion.Linq": "2.0.1",
18 "System.Collections.Immutable": "1.1.36"
19 },
20 "frameworkAssemblies": [
21 "Microsoft.CSharp",
22 "mscorlib",
23 "System",
24 "System.ComponentModel.DataAnnotations",
25 "System.Core"
26 ],
I added "System.Runtime"
into the project.lock.json
of class library project and everything compiled and worked.
But, and this is a big but, the project.lock.json
file is generated from the project.json
file and any changes to nuget or to the porject.json
will lead to my project.lock.json
being overwritten.
At least I know the culprit and now I had to figure out how to get that entry for "System.Runtime"
to be generated and put into "frameworkAssemblies"
.
Note also that line 5 for the two files also differs, this I think is more telling problem.
"DNX,Version=v4.5.1": {
vs
".NETFramework,Version=v4.5.1": {
Time to compare the project.json
files.
The project.json files
This is project.json
in the web application project
1{
2 "version": "1.0.0-* ",
3 "compilationOptions": {
4 "emitEntryPoint": true
5 },
6
7 "dependencies": {
8 "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
9 "Member.Business": "1.0.0-* ",
10 "Member.DataLayer": "1.0.0-* ",
11 "Member.Domain": "1.0.0-* ",
12 "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
13 "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
14 "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
15 "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
16 "Microsoft.CSharp": "4.0.0",
17 "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
18 "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
19 "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
20 "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
21 "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
22 },
23
24 "commands": {
25 "web": "Microsoft.AspNet.Server.Kestrel"
26 },
27
28 "frameworks": {
29 "dnx451": { },
30 "dnxcore50": { }
31 },
32
33 "exclude": [
34 "wwwroot",
35 "node_modules"
36 ],
37 "publishExclude": [
38 "* * .user",
39 "* * .vspscc"
40 ]
41}
And this is in the class library project.
1{
2 "version": "1.0.0-* ",
3 "description": "Member.Business Class Library",
4 "authors": [ "bryan" ],
5 "tags": [ "" ],
6 "projectUrl": "",
7 "licenseUrl": "",
8 "frameworks": {
9 "net451": { },
10 "dotnet5.4": {
11 "dependencies": {
12 "Microsoft.CSharp": "4.0.1-beta-23516",
13 "System.Collections": "4.0.11-beta-23516",
14 "System.Linq": "4.0.1-beta-23516",
15 "System.Runtime": "4.0.21-beta-23516",
16 "System.Threading": "4.0.11-beta-23516"
17 }
18 }
19 },
20 "dependencies": {
21 "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
22 "Member.DataLayer": "1.0.0-* ",
23 "Member.Domain": "1.0.0-* "
24 }
25}
There are some serious differences between the two, most notably around the “frameworks” and “dependencies”. I have no idea why the two project.json
files are so different.
Solution A and B
I changed my project.json
in the class library project to the below adding the "frameworkAssemblies"
node.
1{
2 "version": "1.0.0-* ",
3 "description": "Fund.FundEntitlement.Business Class Library",
4 "authors": [ "bryan" ],
5 "tags": [ "" ],
6 "projectUrl": "",
7 "licenseUrl": "",
8 "frameworks": {
9 "net451": {
10 "frameworkAssemblies": {
11 "System.Runtime": "4.0.10.0"
12 }
13 },
14
15 "dotnet5.4": {
16 "dependencies": {
17 "Microsoft.CSharp": "4.0.1-beta-23516",
18 "System.Collections": "4.0.11-beta-23516",
19 "System.Linq": "4.0.1-beta-23516",
20 "System.Runtime": "4.0.21-beta-23516",
21 "System.Threading": "4.0.11-beta-23516"
22 }
23 }
24 },
25 "dependencies": {
26 "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
27 "Fund.FundEntitlement.DataLayer": "1.0.0-* ",
28 "Fund.FundEntitlement.Domain": "1.0.0-* "
29 }
30}
An alternative that also works is changing the project.json
in the class library to look more like the on from the web application project.
1{
2 "version": "1.0.0-* ",
3 "description": "Fund.FundEntitlement.Business Class Library",
4 "authors": [ "bryan" ],
5 "tags": [ "" ],
6 "projectUrl": "",
7 "licenseUrl": "",
8 "frameworks": {
9 "dnx451": { },
10 "dnxcore50": {
11 "dependencies": {
12 "Microsoft.CSharp": "4.0.1-beta-23516"
13 }
14 }
15 },
16 "dependencies": {
17 "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
18 "Fund.FundEntitlement.DataLayer": "1.0.0-* ",
19 "Fund.FundEntitlement.Domain": "1.0.0-* ",
20 "System.Collections": "4.0.11-beta-23516",
21 "System.Linq": "4.0.1-beta-23516",
22 "System.Runtime": "4.0.21-beta-23516",
23 "System.Threading": "4.0.11-beta-23516"
24 }
25}
Now everything compiles and all is good. I’m sadly sure it won’t be the last time I have screw around with project.lock.json
and project.json
. For more info about those files see http://davidfowl.com/diagnosing-dependency-issues-with-asp-net-5/