2

I have .net core web API and when I isolated Startup.cs in different assembly all APIs return 404 and if I return Startup.cs back to the same assembly where controllers exist, they work again.

Here is my Program.cs of my web API:

public class Program
{
    public static void Main(string[] args)
    {
      CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
      WebHost.CreateDefaultBuilder(args)
       .UseStartup<Startup>()
       .ConfigureAppConfiguration((hostContext, configApp) =>
       {
           configApp.SetBasePath(Directory.GetCurrentDirectory());
           configApp.AddJsonFile("appsettings.json", false, true);
           configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", false, true);
       });
}

And my Startup.cs :

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
                .AddDataAnnotationsLocalization(options =>
                {
                    options.DataAnnotationLocalizerProvider = (type, factory) =>
                        factory.Create(typeof(ValidationMessages));
                });
    }


    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

So I need to put startup class in a different assembly and then use it inside multiple Web API projects

1 Answer 1

6

Replace the .UseStartup with the following lines:

.UseStartup<Application.AppComponents.Startup>() 
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Program).GetTypeInfo().Assembly.FullName)

Where Application.AppComponents.Startup is the namespace of your startup file in the class library.

Sign up to request clarification or add additional context in comments.

1 Comment

Tachyon, can you explain why this would fix it? I have a very similar issue, but this change does not correct it. stackoverflow.com/questions/61980951/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.