【问题标题】:How to add reference to an external dll file in asp.net core project如何在 asp.net 核心项目中添加对外部 dll 文件的引用
【发布时间】:2017-05-03 02:39:35
【问题描述】:

每次我尝试将非 asp.net 核心项目中的任何 dll 文件的引用添加到我的新 asp.net 核心项目时,我都会收到以下错误消息:

.NET Core 项目仅支持在此版本中引用 .NET 框架程序集。参考 其他程序集,它们需要包含在 NuGet 包中,并且 参考那个包。

这里应该发生什么?有什么特殊的方法吗?接缝我在这里缺少的东西与所有以前的 asp.net 版本不同

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc


    【解决方案1】:

    到目前为止,您不能直接将完整的 .NET 框架 dll 直接添加到 ASP.NET 核心项目 (netcoreapp1.0) 中。您必须创建 NuGet 包。

    如果是项目特定的 dll,则创建本地 NuGet 包。这些是我们在项目中生成 NuGet 包所遵循的步骤-

    1.下载Nuget.exe并将其放在.csproj文件所在的文件夹中。

    2.打开 cmd 并输入 nuget spec。将创建带有.nuspec 扩展名的文件。

    3.打开创建的文件并添加标签:

    <files> <file src="..\..\SomeRoot\**\*.*" target="libs\net461" /> </files>
    

    4.在cmd中执行nuget pack A.csproj –IncludeReferencedProjects。扩展名为 .nupkg 的文件被创建。

    5.转到视觉工作室。在 NuGet 包管理器设置中,添加“包源”并提供您的 .nupkg.nuspec 文件所在的路径。

    6.关闭 Nuget 包管理器并再次打开它。现在您可以在浏览选项卡下创建的包源中找到它。

    注意:您的.nuspec 文件应该是这样的:

    <?xml version="1.0"?>
    <package  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <metadata  xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
        <id>ABC.XYZ.FL</id>
        <version>1.0.0.0</version>
        <title>ABC.XYZ.FL</title>
        <authors>ABC</authors>
        <owners>ABC</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Framework that will be used to create objects in XYZ world</description>
        <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
        <copyright>2016</copyright>
        <tags>ABC.XYZ.FL</tags>
      </metadata>
      <files>
        <file src="bin\Debug\*.dll" target="lib\net461" />
      </files>
    </package>
    

    以下链接包含有关创建 nuget 包并将其托管在本地的更多详细信息:

    https://docs.nuget.org/create/creating-and-publishing-a-package

    https://docs.nuget.org/create/hosting-your-own-nuget-feeds

    https://docs.nuget.org/consume/nuget-config-file

    看看这是否有帮助。

    【讨论】:

    • @ShortlyFD 它只是项目根目录的一个示例。请参阅我的答案的示例底部。 &lt;file src="bin\Debug\*.dll" target="lib\net461" /&gt;
    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 2021-05-15
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    相关资源
    最近更新 更多