ASP NET CORE creation of the solution

0

My question is to understand the code when an ASP Net Core project is created. There are two classes, Program and Startup (created by default).

The main calls the following method:

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

Where as I understand you send the other existing class (Startup).

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
        //Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton<IUnitOfWork>(
                (options) => new CibertecUnitOfWork(Configuration.GetConnectionString("CibertecConnection"))
            );

        services.AddTransient<IProductoLogica, ProductoLogica>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

Now my question is how is it that on the other side, you can use this class Startup sent since as you will see they do not have a common parent or interface.

I hope you can help me, I can not sleep because I do not know how to do that.

    
asked by user93953 14.07.2018 в 09:58
source

1 answer

1

It is because what the UseStartup method is generic and expects any kind of class. that is, you can send an empty class and compile the program, but you will not have configured services or the handling of the requests.

public static IWebHostBuilder UseStartup<TStartup>(this IWebHostBuilder hostBuilder) where TStartup : class;

UPDATE

The method called UseStartUp belongs to an IWebHostBuilder extension and its content is as follows:

public static IWebHostBuilder UseStartup<TStartup>(this IWebHostBuilder hostBuilder) where TStartup : class
    {
        return hostBuilder.UseStartup(typeof(TStartup));
    }

Which using Reflection obtains the type of the class sent subsequently calls:

public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Type startupType)
    {
        var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name;

        return hostBuilder
            .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
            .ConfigureServices(services =>
            {
                if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
                {
                    services.AddSingleton(typeof(IStartup), startupType);
                }
                else
                {
                    services.AddSingleton(typeof(IStartup), sp =>
                    {
                        var hostingEnvironment = sp.GetRequiredService<IHostingEnvironment>();
                        return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName));
                    });
                }
            });
    }

Any questions about how ASP works can be reviewed Here

Greetings!

    
answered by 18.07.2018 в 17:51