Dudemanword
Dudemanword

Reputation: 704

4.6.2 Library Appears to Be Referenced but Cannot be Resolved in Core Application

I have been messing with the new .NET Core 1.1 and am trying to get a dll written in .NET 4.6.2 resolved. I added the project TestLibrary as a project reference in the project.json:

"frameworks": {
    "net462": {
      "dependencies": {
        "TestLibrary": {
          "target": "project"
        }

      }
    },

...

This appears to load the assembly:

Loaded Assembly

Intellisense seems to recognize the assembly:

Recognize

But, I am getting a compilation error:

error CS0246: The type or namespace name 'TestLibrary' could not be found (are you missing a using directive or an assembly reference?)

The full project.json is below:

{
  "dependencies": {

    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net462": {
      "dependencies": {
        "TestLibrary": {
          "target": "project"
        }

      }
    },
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.1.0",
          "type": "platform"
        }
      },
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

The full class I am using to test is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestLibrary
{
    public class Class1
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

What is happening here and how do I resolve it?

Upvotes: 0

Views: 128

Answers (1)

Tseng
Tseng

Reputation: 64229

Error message says it.

Your application target net462 and netcoreapp1.1. But your library only targets net462, that's why the netcoreapp1.1 can't find the class.

You either have to target netstandard1.x in your TestLibrary (if you don't use any net462 only APIs which may not be available in netstandard1.x or you remove netcoreapp1.1 and only target the full .NET Framework (and not .NET Core).

Some background information

When you have more than 1 target in the framework section, then Visual Studio (and the dotnet build) will build multiple executable files, one per target. Your TestLibrary fulfills the requirement for net462 target, but it doesn't for netcoreapp1.1, because netcoreapp1.1 doesn't reference this library.

Update

If for example you want to use some functions which is only available on one plattform (i.e. PerfCounters), then you would reference this library in net462 and use it only in binary which compile for net462 and remove it from netcoreapp1.1

#if NET462
    var perfCounter = new PerformanceCounter("Category", 
        "CounterName", 
        false);
    // do something with it here
#endif

Now you can use it as extra feature in net462 and the .NET Core version (netcoreapp1.1) won't have it included, because this API is unavailable in .NET Core 1.1.

Upvotes: 3

Related Questions