0
votes

I need to create ASP.NET CORE 2.1 web API from one class that is in Class library project(Targets 4.7.2).

I have added references to Microsoft.AspNetCore and Microsoft.AspNetCore.Mvc.Core.

Following call throws exception:

 CreateWebHostBuilder().Build().Run();      

Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Output window:

Exception thrown: 'System.IO.FileNotFoundException' in Microsoft.Extensions.DependencyInjection.dll
An exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Extensions.DependencyInjection.dll but was not handled in user code
Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I have tried adding System.Runtime.InteropServices.RuntimeInformation reference manualy with no success.

Am I missing something here?

1
So what does your ASP.NET Core project use as .NET version? .Net Core 2.1? - nvoigt
What I was trying to do here is just creating the api by running CreateWebHostBuilder().Build().Run() in class that is part of Class Library project that targets 4.7.2 with references to Microsoft.AspNetCore and Microsoft.AspNetCore.Mvc.Core. Is this wrong approach ? - FmAn
Which Visual Studio version do you use? - CodeCaster
VS professional 2017 15.8.2 - FmAn
You really shouldn’t attempt to do this. Properly building an ASP.NET Core web application kind of requires the Web SDK which you won’t use in class libraries. - poke

1 Answers

1
votes

If you have added the reference as nuget pakage you can try to add this property group in the csproj:

<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> </PropertyGroup>

This property will copy the nuget dll in the output directory instead of use the dll in the nuget memory cache. This can help you if in your solution you have many projects that refers different version of the same package (related dependencies too).

If you have added the reference as a simple "reference" to a known dll, you should verify the dll is in the output directory. Verify the version of that dll too. If it's different and the dll reference refers a strict version could be a problem.