Search code examples
c#.net-6.0iformfiletypeloadexception

How to resolve error in net 6: Could not load type Microsoft.AspNetCore.Http.Internal.FormFile from assembly Microsoft.AspNetCore.Http Version=6.0.0.0


I have this error when I run the project:

System.TypeLoadException: 'Could not load type 'Microsoft.AspNetCore.Http.Internal.FormFile' from assembly 'Microsoft.AspNetCore.Http, Version=6.0.0.0

I use dot net 6.0

These are my dependencies:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Autofac" Version="6.4.0" />
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Autofac.Extras.CommonServiceLocator" Version="6.0.1" />
    <PackageReference Include="AutoMapper" Version="12.0.0" />
    <PackageReference Include="FluentValidation" Version="11.4.0" />
    <PackageReference Include="MediatR" Version="8.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="2.2.0" />
    <PackageReference Include="RestSharp" Version="108.0.3" />
    <PackageReference Include="Serilog" Version="2.12.0" />
  </ItemGroup>

I don't know what's the problem!

I Tried different versions of 'Microsoft.AspNetCore.Http' and 'Microsoft.AspNetCore.Http.Features' packages, but the problem is exists and I search for solution, but it wasn't helpful!

Please sb help me!

Thank you.


Solution

  • Add the <FrameworkReference> element to your project file: target-aspnetcore

    <ItemGroup>
     <FrameworkReference Include="Microsoft.AspNetCore.App" />
     ...
    </ItemGroup>
    

    And check .csproj of project. The first line should be as follows as one of these:

    <Project Sdk="Microsoft.NET.Sdk">
    

    Or:

    <Project Sdk="Microsoft.NET.Sdk.Web">