我可以将.net核心与遗留的.net框架dll一起使用吗?答案似乎不是……但我只能找到引用project.json的资源,而这些资源已经不存在了.
我创建了一个新的.net核心库,并试图引用一个遗留的.net框架DLL.当我试图调用DLL时,vs2017抱怨我没有Stream对象正在寻找.
它建议我引用mscorlib.dll或installa Nuget包.
快速帮助未能引用mscorlib.dll.如果我手动引用它,我会收到以下错误:
The type ‘TargetFrameworkAttribute’ exists in both ‘mscorlib,
Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089’ and
‘System.Runtime,Version=4.1.0.0,
PublicKeyToken=b03f5f7f11d50a3a’ C:\Users…\AppData\Local\Temp.NETCoreApp,Version=v1.1.AssemblyAttributes.cs
NuGet包是Microsoft.NETFx2.0.快速帮助无法安装它.如果我从命令行运行它:
> PM> install-package microsoft.netfx20 GET > https://api.nuget.org/v3/registration2-gz/microsoft.netfx20/index.json > OK > https://api.nuget.org/v3/registration2-gz/microsoft.netfx20/index.json > 46ms Restoring packages for ... Install-Package : Package > Microsoft.NetFX20 1.0.3 is not compatible with netcoreapp1.1 > (.NETCoreApp,Version=v1.1). Package Microsoft.NetFX20 1.0.3 supports: > net20 (.NETFramework,Version=v2.0)At line:1 char:1 > + install-package microsoft.netfx20 > + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > + CategoryInfo : NotSpecified: (:) [Install-Package],Exception > + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand > Install-Package : One or more packages are incompatible with > .NETCoreApp,Version=v1.1.At line:1 char:1 > + install-package microsoft.netfx20 > + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > + CategoryInfo : NotSpecified: (:) [Install-Package],NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand > Install-Package : Package restore Failed. Rolling back package changes > for .At line:1 char:1 > + install-package microsoft.netfx20 > + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > + CategoryInfo : NotSpecified: (:) [Install-Package],NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand > Time Elapsed: 00:00:00.8035644
解决方法
困难的话题.通常.NET Framework和.NET Core不兼容.它们针对一组不同的程序集(mscorlib与System.Runtime),这会导致不兼容,因为所有类型的用法都以类型所在的程序集为前缀.
从.NET Core 2(当前处于预览版)开始,您可以通过不可见的兼容性垫片引用.NET Framework程序集.这允许您引用程序集并成功编译.
它不保证应用程序将成功运行,因为.NET Core不提供.NET Framework中的所有API.如果是这样的话,你会在运行时得到PlatformNotSupportedException或MissingTypeException和friends.