简体   繁体   English

无法从程序集 System.Web 加载类型“System.Web.HttpPostedFileBase”

[英]Could not load type 'System.Web.HttpPostedFileBase' from assembly System.Web

I am working on a solution which has two projects, one is a .NET Core 3 project and the other is a .NET Framework 4.7.2 project.我正在开发一个有两个项目的解决方案,一个是 .NET Core 3 项目,另一个是 .NET Framework 4.7.2 项目。 The .NET Framework 4.7.2 project has a class which uses HttpPostedFileBase on a method. .NET Framework 4.7.2 项目有一个在方法上使用HttpPostedFileBase的类。

The problem is that when I use Autofac to register that class on my .NET Core project, I get this error:问题是,当我使用 Autofac 在我的 .NET Core 项目上注册该类时,出现以下错误:

Could not load type ' System.Web.HttpPostedFileBase ' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.无法从程序集“System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”加载类型“ System.Web.HttpPostedFileBase ”。

I am aware that HttpPostedFileBase does not exist on System.Web on .NET Core 3, so my guess is that when I try to register the class with Autofac, the System.Web being used is the one from my .NET Core project and not the one from the .NET Framework project.我知道 .NET Core 3 上的 System.Web 上不存在HttpPostedFileBase ,所以我的猜测是,当我尝试使用 Autofac 注册该类时,所使用的 System.Web 是我的 .NET Core 项目中的一个,而不是.NET Framework 项目中的一个。 Would it be viable to try to use only the System.Web from the other project, into my .NET Core project?尝试仅将来自其他项目的 System.Web 用于我的 .NET Core 项目是否可行?

I've tried adding the System.Web.dll from the other project, in to my .NET Core project but I get the error:我尝试将 System.Web.dll 从另一个项目添加到我的 .NET Core 项目中,但出现错误:

One or more errors occurred.发生了一个或多个错误。 The reference is invalid or unsupported.引用无效或不受支持。

I hope this answer helps you get on the right track.我希望这个答案可以帮助您走上正轨。

Few options to resolve your problem come to my mind:我想到了解决您的问题的几个选项:

1. Completely abandon .NET Framework 1.完全放弃.NET Framework

Change project using .NET Framework 4.7.2 to use .NET Standard if that is possible and rewrite method in question completlty abandoning .NET Standard.如果可能,将使用 .NET Framework 4.7.2 的项目更改为使用 .NET Standard,并完全放弃 .NET Standard 重写有问题的方法。 This is in some ways easier options and I advise you to take it if possible.这在某些方面是更简单的选择,我建议您尽可能采用它。

2. Target both .NET Standard and .NET Framework 2. 同时面向 .NET Standard 和 .NET Framework

If this is not possible, because of some issues eg project is used in other solutions that cannot target .NET Standard you can make this project target both platforms.如果这是不可能的,因为某些问题,例如项目用于其他无法针对.NET Standard解决方案,您可以使该项目针对两个平台。

You can do second option through VisualStudio or directly in csproj file editing TargetFramework attribute in newer csproj format (I strongly advise you to migrate project to new format with some wizard).您可以通过 VisualStudio 或直接在 csproj 文件中以较新的 csproj 格式编辑TargetFramework属性(我强烈建议您使用某些向导将项目迁移到新格式)执行第二个选项。 Then you should add condition on ItemGroup in csproj to only include System.Web if you are using .NET Framework 4.7.2 and .NET Standard counterpart if using .NET Standard .然后,如果您使用的是.NET Framework 4.7.2.NET Standard对应版本,则您应该在 csproj 中的ItemGroup上添加条件以仅包含System.Web 。如果使用.NET Standard Then write the method in question again using .NET Standard , but do not delete the old one.然后使用.NET Standard再次编写有问题的方法,但不要删除旧方法。 Instead you should make compiler choose the right one with #if compiler instructions.相反,您应该使用#if编译器指令让编译器选择正确的。

Below you can find examples with new csproj format.您可以在下面找到具有新 csproj 格式的示例。 If you use old one you can use eg THIS WIZARD to automatically migrate.如果您使用旧的,您可以使用例如此向导来自动迁移。

<TargetFramework>net472</TargetFramework>

Should be changed to (mind s ):应改为 (mind s ):

<TargetFrameworks>net472;netstandard2.0</TargetFrameworks>

And then to include System.Web only when using net472 :然后仅在使用net472时包含System.Web

<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
    <Reference Include="System.Web" />
</ItemGroup>

And later the same for .NET Standard.后来对于 .NET Standard 也是如此。

Last part should be done in C# code itself:最后一部分应该在 C# 代码中完成:

#if NETFRAMEWORK
... method using **HttpPostedFileBase** ...
#elif NETSTANDARD
... its .NET Standard counterpart ...
#endif

Constants used above - NETSTANDARD and NETFRAMEWORK are wildcards for more specific version of mentioned platforms.上面使用的常量 - NETSTANDARD 和 NETFRAMEWORK 是上述平台的更具体版本的通配符。 You can find them all HERE你可以在这里找到它们

You might be forced to do similar things with callers of method in question.您可能被迫对相关方法的调用者做类似的事情。

If those things do not help you might want to check assembly binding redirects if you have any as this issue I did post about might not be the only one there is.如果这些事情没有帮助,您可能需要检查程序集绑定重定向(如果您有任何问题),因为我发布的这个问题可能不是唯一的。

Good luck, I hope my answer helps in any way.祝你好运,我希望我的回答有任何帮助。

for some reason when I had registered the class I was talking about with Autofac like this: builder.RegisterType() .EnableClassInterceptors().InterceptedBy(typeof(LogInterceptors), typeof(CacheInterceptor));出于某种原因,当我注册我正在谈论的 Autofac 类时,就像这样: builder.RegisterType() .EnableClassInterceptors().InterceptedBy(typeof(LogInterceptors), typeof(CacheInterceptor)); and then removed the class interceptors and registered the class like builder.RegisterType();然后删除类拦截器并注册类,如 builder.RegisterType(); , then the error disappeared. ,然后错误消失了。

暂无
暂无

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

相关问题 无法从程序集&#39;System.Web,...&#39;加载类型&#39;...&#39; - Could not load type '…' from assembly 'System.Web, …' ASP.NET Core TypeLoadException无法从程序集“ System.Web”加载类型“ System.Web.PreApplicationStartMethodAttribute” - ASP.NET Core TypeLoadException Could not load type 'System.Web.PreApplicationStartMethodAttribute' from assembly 'System.Web' NUnit 测试错误:无法从程序集 'System.Web,版本 = 4.0.0.0 加载类型 'System.Web.HttpApplication' - NUnit Test Error: Could not load type 'System.Web.HttpApplication' from assembly 'System.Web, Version=4.0.0.0 无法从程序集“System.Web,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50 - Could not load type 'System.Web.UI.ParseChildrenAttribute' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50 无法从程序集“System.Web,版本=4.0.0.0,Cult=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Web.UI.ParseChildrenAttribute” - Could not load type 'System.Web.UI.ParseChildrenAttribute' from assembly 'System.Web, Version=4.0.0.0, Cult=neutral, PublicKeyToken=b03f5f7f11d50a3a' 如何通过将字符串转换为System.Web.HttpPostedFileBase将图像保存在db中 - How to save Image in db by converting string to System.Web.HttpPostedFileBase 非System.Web替代HttpPostedFileBase? - Non System.Web substitute for HttpPostedFileBase? 无法识别HttpPostedFileBase,但可以识别“使用System.Web” - HttpPostedFileBase is not recognized, but “using System.Web” is 是否可以将单个System.Web.HttpPostedFileBase图像(任何类型)转换为位图对象? - Is it possible to convert a single System.Web.HttpPostedFileBase image (any type) to a bitmap object? 选择Linq不能将类型byte []隐式转换为System.web.httpPostedFileBase - Select Linq cannot implicitly convert type byte[] to System.web.httpPostedFileBase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM