4.6.2库似乎被引用但无法在核心应用程序中解析

时间:2016-11-23 19:38:07

标签: c# asp.net-core

我一直在搞乱新的.NET Core 1.1,并试图让用.NET 4.6.2编写的dll得到解决。我将项目 TestLibrary 添加为project.json中的项目引用:

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

      }
    },

...

这似乎加载程序集:

Loaded Assembly

Intellisense似乎认出了集会:

Recognize

但是,我收到了编译错误:

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

完整的project.json如下:

{
  "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%" ]
  }
}

我用来测试的完整课程如下:

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;
        }
    }
}

这里发生了什么,我该如何解决?

1 个答案:

答案 0 :(得分:3)

错误消息说出来。

您的申请目标net462netcoreapp1.1。但是您的图书馆仅定位net462,这就是netcoreapp1.1无法找到课程的原因。

您必须在netstandard1.x中定位TestLibrary(如果您不使用net462中可能无法使用的任何netstandard1.x API或删除netcoreapp1.1 {1}}并且只针对完整的.NET Framework(而不是.NET Core)。

一些背景信息

如果框架部分中有多个目标,则Visual Studio(以及dotnet build)将构建多个可执行文件,每个目标一个 。您的TestLibrary符合net462目标的要求,但不适用于netcoreapp1.1,因为netcoreapp1.1未引用此库。

更新

例如,如果您想使用一些仅在一个平台上可用的函数(即PerfCounters),那么您将在net462中引用此库并仅在二进制文件中使用它来编译{ {1}}并将其从net462

中删除
netcoreapp1.1

现在您可以将它用作#if NET462 var perfCounter = new PerformanceCounter("Category", "CounterName", false); // do something with it here #endif 中的额外功能,并且.NET Core版本(net462)不会包含它,因为此.NET在.NET Core 1.1中不可用。