Reputation: 827
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
// 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
}
Upvotes: 13
Views: 14781
Reputation: 8586
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));
}
Upvotes: 2
Reputation: 1916
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>
Upvotes: 23