ASP.NET Core ์›น ์‘์šฉํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋•Œ, ์›น ์‘์šฉํ”„๋กœ๊ทธ๋žจ ํ”„๋กœ์ ํŠธ์—์„œ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ๋กœ ๋ถ„๋ฆฌํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋ฉด ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ๊ฒ€ํ† ํ•ด ๋ณด์‹ญ์‹œ์˜ค.

์‹ ๊ทœ ํ”„๋กœ์ ํŠธ ์ž‘์„ฑ

์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋ถ„๋ฆฌํ•  ํ”„๋กœ์ ํŠธ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

.NET Standard ํด๋ž˜์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ…œํ”Œ๋ฆฟ ์‚ฌ์šฉ

$ mkdir Blog.Backend.Api.Account
$ cd Blog.Backend.Api.Account
$ dotnet new classlib

ํ•„์š”ํ•œ Nuget ํŒจํ‚ค์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

$ dotnet add package Microsoft.AspNetCore.Mvc --version 2.2.0
$ dotnet add package Microsoft.Extensions.Logging --version 2.2.0

์˜ˆ์ œ: porject file

์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ ํ”„๋กœ์ ํŠธ๋กœ ๋ณต์‚ฌํ•˜๊ณ , ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

IServiceCollection ์— ํ™•์žฅ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

public static class ServicesConfigureExtesion
{
    public static IServiceCollection AddAccountApiController(this IServiceCollection services)
    {
        // ์˜ฎ๊ธด ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๋ฅผ IoC ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.
        services.AddTransient<UserController>();
        services.AddTransient<AccountController>();
        return services;
    }

    public static IMvcBuilder AddAccountApiController(this IMvcBuilder builder)
    {
        var currentAssembly = typeof(ServicesConfigureExtesion).GetTypeInfo().Assembly;

        builder
            .AddApplicationPart(currentAssembly)
            ;

        return builder;
    }
}

์˜ˆ์ œ: IServiceCollection ํ™•์žฅ๋ฉ”์„œ๋“œ

์›น ์‘์šฉํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ ๋ณ€๊ฒฝ

๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ๋กœ ์˜ฎ๊ธด ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ์›น ์‘์šฉํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœ์ ํŠธ ์ฐธ์กฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , Startup ํด๋ž˜์Šค์˜ ConfigureServices ๋ฉ”์„œ๋“œ์— ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ์—์„œ ์ •์˜ํ•œ IServiceCollection ํ™•์žฅ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

public void ConfigureServices(IServiceCollection services)
{
    /* ์ค‘๋žต */

    services
        /* IServiceCollection ํ™•์žฅ๋ฉ”์„œ๋“œ */
        /* IoC ์ปจํ…Œ์ด๋„ˆ์— ํƒ€์ž…์„ ๋“ฑ๋ก */
        .AddAccountApiController()
        .AddMvc()
        /* IMvcBuilder ํ™•์žฅ๋ฉ”์„œ๋“œ */
        /* ๋‹ค๋ฅธ ์–ด์…ˆ๋ธ”๋ฆฌ์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๊ตฌ์„ฑ */
        .AddAccountApiController()
        .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2)
        .AddControllersAsServices()
        ;

    /* ์ค‘๋žต */
}

์˜ˆ์ œ: Startup.cs ์˜ˆ์ œ

์ถ”๊ฐ€

๋ผ์šฐํŠธ ๊ตฌ์„ฑ์ด ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋ฉด Startup ํด๋ž˜์Šค์˜ Configure ๋ฉ”์„œ๋“œ์—์„œ ๋ผ์šฐํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ: Startup.cs ์˜ˆ์ œ