简体   繁体   English

在 ASP.NET Core3.1 中使用包含“System.Web.HttpContext”的旧项目

[英]Using legacy projects containing 'System.Web.HttpContext' in ASP.NET Core3.1

We have some legacy projects, written with .Net Framework which should be used by an API project which is written by ASP.NET Core3.1.我们有一些遗留项目,用 .Net Framework 编写,应该由 ASP.NET Core3.1 编写的 API 项目使用。 The problem is that these legacy projects are using 'System.Web.HttpContext' that you know does not exist in .net core anymore.问题是这些遗留项目正在使用您知道在 .net 核心中不再存在的“System.Web.HttpContext”。 Now we are receiving this error: ' Could not load type 'System.Web.HttpContext' from assembly 'System.Web , Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.'现在我们收到此错误:“无法从程序集“System.Web ,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Web.HttpContext ”。
Have you faced this problem before?你以前遇到过这个问题吗? Can anyone help please?有人可以帮忙吗?

.NET Framework libraries are not technically compatible with .NET Core. .NET Framework 库在技术上与 .NET Core 不兼容。 .NET Standard libraries are, and since both .NET Framework and .NET Core implement .NET Standard, Microsoft made a special exemption in the compiler to allow .NET Framework libraries to be referenced by projects that target .NET Core, with the caveat that they may not actually work. .NET Standard库是,并且由于 .NET Framework 和 .NET Core 都实现了 .NET Standard,因此 Microsoft 在编译器中进行了特殊豁免,以允许面向 .NET Core 的项目引用 .NET Framework 库,但需要注意的是它们实际上可能不起作用。 You get a warning to this effect when you reference a .NET Framework library from a .NET Core project.当您从 .NET Core 项目引用 .NET Framework 库时,您会收到有关此效果的警告。

The .NET Framework library will work as long as it only uses APIs compatible with .NET Standard 2.0.只要 .NET Framework 库使用与 .NET Standard 2.0 兼容的 API,它就可以工作。 System.Web.HttpContext is not one such API, so it will not work in a .NET Core project. System.Web.HttpContext不是这样的一个API,这样就不会在.NET的核心项目。 Period.时期。 Nothing you can do about that.你对此无能为力。

You can attempt to multi-target your libraries and sub-in .NET Core compatible APIs using compiler directives.您可以尝试使用编译器指令多目标您的库和子 .NET Core 兼容 API。 That'll work, but gets messy quick.这会奏效,但很快就会变得一团糟。 Otherwise, you'll just have to write new versions of the libraries for ASP.NET Core, since presumably you're trying to maintain support in the current libraries for ASP.NET.否则,您只需为 ASP.NET Core 编写新版本的库,因为您可能正在尝试维护对 ASP.NET 的当前库的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM