Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IFormFile not load type Microsoft.AspNetCore.Http.Internal.FormFile' from assembly 'Microsoft.AspNetCore.Http 3.0'

When using IFormFile I am getting this error at running time:

Could not load type 'Microsoft.AspNetCore.Http.Internal.FormFile' from assembly 'Microsoft.AspNetCore.Http, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

I have tried adding packages:

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.0" />
using Microsoft.AspNetCore.Http.Internal;
IFormFile f = new FormFile(memoryStream, 0, memoryStream.Length, "test", "test.pdf");

Documentation exists for FormFile Aspnetcore 3.0. However when checking my sdk, instead of using 3.0. It is only available in .net core 2.2. I have both versions 2.2 and 3.0 installed

region Assembly Microsoft.AspNetCore.Http, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60

// C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.dll
#endregion

using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Http.Internal
{
    public class FormFile : IFormFile
}
like image 393
Exec21 Avatar asked Nov 17 '19 19:11

Exec21


2 Answers

I was facing the same error and could not find anything directly related to this error and after two days battling with this issue finally, I have found the solution.

The problem occurs when you try to instantiate FormFile or FormFileCollection class. If the project does not have a framework reference to Microsoft.AspNetCore.App, it will throw the above error.

Solution:

Microsoft.AspNetCore.App framework reference needs to be added manually whichever project is instantiating FormFile or FormFileCollection class.

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

https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#framework-reference

like image 86
RL89 Avatar answered Nov 07 '22 00:11

RL89


I am under .NET 5 and the solution of @RL89 conceptionally works but in practice it changed.

You need to change to first line in your *.csproj file to:

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

Notice that it is Microsoft.NET.Sdk .Web

You solution will not compile unless you have a main method. I simply created a fake Program.cs like this

namespace MyClassProjThatTurnedIntoWebProject
{
    using System;

    public class Program
    {
        public static void Main(string[] args)
        {
            throw new NotImplementedException("This project is not meant to be run. https://stackoverflow.com/a/67097605/828184");
        }
    }
}

Then I could do fun stuff like creating a new FormFile() like this:

FormFile file;
string path = "/file/path.json";
using (var stream = System.IO.File.OpenRead(path))
{
    file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name));
}
like image 42
CodingYourLife Avatar answered Nov 07 '22 00:11

CodingYourLife