{"record":{"id":"56b49df8d3ea7dd8","repo":"RicoSuter/NSwag","slug":"nswag-requires-the-assembly-assembly-getname-to-have-either","errorCode":null,"errorMessage":"NSwag requires the assembly {assembly.GetName()} to have either a BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method. See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x for suggestions on ways to refactor your startup type.","messagePattern":"NSwag requires the assembly (.+?) to have either a BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method\\. See https://docs\\.microsoft\\.com/en-us/aspnet/core/fundamentals/hosting\\?tabs=aspnetcore2x for suggestions on ways to refactor your startup type\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/HostApplication.cs","lineNumber":98,"sourceCode":"                        .CreateDefaultBuilder()\n                        .UseStartup(startupType)\n                        .Build()\n                        .Services;\n                    #else\n                    serviceProvider = new HostBuilder()\n                        .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup(startupType))\n                        .Build()\n                        .Services;\n                    #endif\n                }\n            }\n\n            if (serviceProvider != null)\n            {\n                return serviceProvider;\n            }\n\n            throw new InvalidOperationException($\"NSwag requires the assembly {assembly.GetName()} to have \" +\n                                                $\"either a BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method. \" +\n                                                $\"See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x \" +\n                                                $\"for suggestions on ways to refactor your startup type.\");\n        }\n\n        internal static IServiceProvider GetServiceProviderWithHostFactoryResolver(Assembly assembly)\n        {\n#if NETFRAMEWORK\n            return null;\n#else\n            // We're disabling the default server and the console host lifetime. This will disable:\n            // 1. Listening on ports\n            // 2. Logging to the console from the default host.\n            // This is essentially what the test server does in order to get access to the application's\n            // IServicerProvider *and* middleware pipeline.\n            void ConfigureHostBuilder(object hostBuilder)\n            {\n                ((IHostBuilder)hostBuilder).ConfigureServices((context, services) =>","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/HostApplication.cs#L80-L116","documentation":"To generate an OpenAPI spec from a running ASP.NET Core app, NSwag must instantiate the app's IServiceProvider. GetServiceProvider looks for a BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method on the given assembly; if none is found it throws InvalidOperationException. NSwag cannot construct the host for the target assembly.","triggerScenarios":"Running `nswag aspnetcore2openapi` (or /runtime /aspnetcore in an .nswag) against an assembly whose Program/Startup lacks any of BuildWebHost, CreateWebHostBuilder, or CreateHostBuilder entry points that NSwag can invoke.","commonSituations":"ASP.NET Core apps using WebApplication.CreateBuilder (minimal hosting, .NET 6+) where the pattern differs; custom Main that builds the host differently; target class path wrong so the wrong assembly is inspected; very old (1.x) hosting patterns.","solutions":["Add a CreateHostBuilder or CreateWebHostBuilder static method the generator can call, or","Upgrade NSwag — newer versions use HostFactoryResolver and support minimal hosting (WebApplication.CreateBuilder) directly.","Use the /runtime /aspnetcore injection mode which builds the host inside the app process instead of reflecting on the assembly.","Verify the /project and assembly arguments point at the assembly actually containing the host setup."],"exampleFix":"// before (Program.cs, minimal hosting only)\nvar builder = WebApplication.CreateBuilder(args);\n...\n\n// after: expose a builder factory\npublic static IHostBuilder CreateHostBuilder(string[] args) =>\n    Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(web => web.UseStartup<Startup>());","handlingStrategy":"try-catch","validationCode":"// reflect before generating\nvar hasBuilder = typeof(Program).Assembly.GetTypes()\n    .Any(t => t.GetMethod(\"CreateHostBuilder\") != null || t.GetMethod(\"CreateWebHostBuilder\") != null || t.GetMethods().Any(m => m.Name == \"BuildWebHost\"));\nif (!hasBuilder) Console.Error.WriteLine(\"Assembly has no BuildWebHost/CreateHostBuilder entry point; use runtime mode or add one.\");","typeGuard":null,"tryCatchPattern":"try { GenerateFromAspNetCore(project); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"BuildWebHost or CreateWebHostBuilder\")) { Console.Error.WriteLine(\"App lacks a host-builder factory method; add CreateHostBuilder or upgrade NSwag for minimal-hosting support.\"); }","preventionTips":["Keep a CreateHostBuilder static method even with minimal hosting APIs","Use recent NSwag versions that support WebApplication.CreateBuilder via HostFactoryResolver","Prefer /runtime /aspnetcore injection mode for .NET 6+ minimal hosting apps","Point /project at the assembly containing the host setup"],"tags":["aspnetcore","hosting","reflection","openapi"],"backgroundTag":"required-member-missing","analyzedSha":"63daf8fcc3a25151b62eb4b326a1e8ea048a0d41","analyzedAt":"2026-09-14T11:38:15.205Z","contentChangedAt":"2026-09-14T11:38:15.205Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}