Templates icon indicating copy to clipboard operation
Templates copied to clipboard

Reporting a weird error with .NET 7 and changing hostingContext.HostingEnvironment.ApplicationName

Open VictorioBerra opened this issue 11 months ago • 0 comments

Describe the bug

https://github.com/dotnet/aspnetcore/issues/45022#issuecomment-1319135290

I updated one of my projects to .NET 7 and started getting the following error:

Message: 
System.IO.FileNotFoundException : Could not load file or assembly 'My Awesome App, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

  Stack Trace: 
RuntimeAssembly.<InternalLoad>g____PInvoke|47_0(NativeAssemblyNameParts* pAssemblyNameParts, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Int32 throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)
RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
Assembly.Load(AssemblyName assemblyRef)
ApplicationPartManager.GetApplicationPartAssemblies(String entryAssemblyName)
ApplicationPartManager.PopulateDefaultParts(String entryAssemblyName)
MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services, IWebHostEnvironment environment)
MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)
IServiceCollectionExtensions.AddApiExplorerServices(IServiceCollection services)
IServiceCollectionExtensions.AddVersionedApiExplorer(IServiceCollection services)
Startup.ConfigureServices(IServiceCollection services) line 38
<7 more frames...>
WebApplicationFactory`1.CreateHost(IHostBuilder builder)
WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
WebApplicationFactory`1.EnsureServer()
WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
WebApplicationFactory`1.CreateClient()
MyCoolAppControllerTest.ctor(ITestOutputHelper testOutputHelper) line 22
InvokeStub_MyCoolAppControllerTest.ctor(Object, Object, IntPtr*)
ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

The error went away when I commented out the following line:

https://github.com/Dotnet-Boxed/Templates/blob/main/Source/ApiTemplate/Source/ApiTemplate/Program.cs#L59C21-L59C70

I believe the error is because the Product name does not remotely match the assembly name.

Why do we set this? Can we remove it? Should we do this another way?

Steps to reproduce

  1. Create API from Boxed Template using a --title that does not match the app name
  2. Update everything to .net 7
  3. Get error

Expected behaviour

App does not error

Template

API

.NET Boxed Version

Latest

Visual Studio Version

VS22

.NET Version

.NET 7

VictorioBerra avatar Jul 20 '23 22:07 VictorioBerra