简体   繁体   中英

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. The .NET Framework 4.7.2 project has a class which uses HttpPostedFileBase on a method.

The problem is that when I use Autofac to register that class on my .NET Core project, I get this error:

Could not load type ' System.Web.HttpPostedFileBase ' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

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. Would it be viable to try to use only the System.Web from the other project, into my .NET Core project?

I've tried adding the System.Web.dll from the other project, in to my .NET Core project but I get the error:

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

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. This is in some ways easier options and I advise you to take it if possible.

2. Target both .NET Standard and .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.

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). 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 . Then write the method in question again using .NET Standard , but do not delete the old one. Instead you should make compiler choose the right one with #if compiler instructions.

Below you can find examples with new csproj format. If you use old one you can use eg THIS WIZARD to automatically migrate.

<TargetFramework>net472</TargetFramework>

Should be changed to (mind s ):

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

And then to include System.Web only when using net472 :

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

And later the same for .NET Standard.

Last part should be done in C# code itself:

#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. 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)); and then removed the class interceptors and registered the class like builder.RegisterType(); , then the error disappeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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