dotnet / dotnet/aspnetcore

Invoking a Minimal Web API in .NET 6 via dotnet throws an exception but runs fine as .exe

Open
#41,578 12 comments 0 reactions 0 assignees View on GitHub
area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

A minimal web API in .NET 6 throws the following exception when invoked as `dotnet MyApp.dll`

```
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'dotnet, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'dotnet, Culture=neutral, PublicKeyToken=null'
```

However the same application runs without any errors when run directly from _Visual Studio_ or invoked as _MyApp.exe_

### Expected Behavior

The application should start successfully regardless of how it is started.

### Steps To Reproduce

1. Clone the following repo: https://github.com/NimaAra/WhereIsTheTruck
2. Publish the web API project via:
`dotnet publish WhereIsTheTruck.Web.App\WhereIsTheTruck.Web.App.csproj -c Release -o dist`
3. Set the required env variables:
```
set WhereIsTheTruck:TruckApiEndpoint=http://data.sfgov.org/resource/rqzj-sfat.json
set WhereIsTheTruck:TruckApiRefreshInterval=1:0:0
```
4. Run the application:
`dotnet .\dist\WhereIsTheTruck.Web.App.dll`

### Exceptions (if any)

```
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'dotnet, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'dotnet, Culture=neutral, PublicKeyToken=null'
at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)
at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.GetApplicationPartAssemblies(String entryAssemblyName)
at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateDefaultParts(String entryAssemblyName)
at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services, IWebHostEnvironment environment)
at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersCore(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers(IServiceCollection services)
at Program.<$>g__AddServices|0_3(AppConfig config, <>c__DisplayClass0_0& ) in D:\My-Code\GitHub\WhereIsTheTruck\WhereIsTheTruck.Web.App\Program.cs:line 84
at Program.<$>g__BuildWebApp|0_0(String[] args) in D:\My-Code\GitHub\WhereIsTheTruck\WhereIsTheTruck.Web.App\Program.cs:line 43
at Program.$(String[] args) in D:\My-Code\GitHub\WhereIsTheTruck\WhereIsTheTruck.Web.App\Program.cs:line 10
```

### .NET Version

6.0.202

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.