running the dotnet swagger tofile command causes the solution to start running

I am running a solution with the following added to the csproj to build a custom swagger document

`

</PropertyGroup>
<Exec Command="dotnet tool restore" />
<Exec Command="dotnet swagger tofile --output myoutputfile myawesomecode.dll v1 " />

`

When I try to run a build the tools restore successfully however the solution starts running instead of writing the documentation. This is an issue because my service needs several other services (eg licensing) to be able to run.

In my company we have another project that does not suffer this issue and seems to have no additional code to prevent it running.

My question is how to create the swagger documentation without the source code running?

After 4 days of searching I found the problem with my code base thanks to this open ticket on the github repository.

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2290

This explained there is an open issue with the swashbuckle CLI and the Minimal Hosting Model that had been already implemented.

My Program.cs looked initially like this:

public class Program
    {
        public static async Task Main()
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Service.AddSingleton<ISomeOtherService, SomeOtherService>();
            builder.Services.AddSwaggerGen(options =>
                //custom swagger gen
                );

            builder.Services.AddMvc();
            //etc

            var app = builder.Build(); 

            if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
             await app.RunAsync();
        }
    }

The issue was that the dotnet swagger tofile code was looking for a class called ‘Startup.cs’ using reflection or some other process to be able to load the swagger documentation.
When Startup.cs is not present then dotnet swagger runs program main. To confirm this I ran the code twice once with the class called Startup.cs (which ran successfully) and the second time called SomethingElse.cs (which started my application).

To fix the problem, I had to introduce a class which must start with Startup like so:

    public class Startup(IConfiguration configuration, IWebHostEnvironment enviroment)
    {
        public IConfiguration Configuration { get; } = configuration;
        public IWebHostEnvironment Environment { get; } = enviroment;

        public void ConfigureServices(IServiceCollection services)
        {
            service.AddSingleton<ISomeOtherService, SomeOtherService>();
            services.AddSwaggerGen(options =>
                //custom swagger gen
                );

            services.AddMvc();
            //etc
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
          if (env.IsDevelopment())
          {
              app.UseSwagger();
              app.UseSwaggerUI();
          }
        }
    }

    public class Program
    {
        public static async Task Main()
        {
            var builder = WebApplication.CreateBuilder(args);
            var startup = new Startup(builder.Configuration, builder.Environment);
            startup.ConfigureServices(builder.Services);

            var app = builder.Build(); 

            startup.Configure(app, builder.Environment);

            await app.RunAsync();
        }
    }

On the official documentation for the minimal hosting model the code example references a Startup.cs class as below:

However the swagger documentation makes no mention that NOT having a Startup.cs would cause any issues.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật