Vincent
3์ 29, 2021, 3:38์ค์
1
๋ง๋ค๊ณ ์ํ๋ ๊ฒ์ .NET 5 Console Application์์ Azure Service Bus์ Queue๋ฐ์ดํฐ๋ฅผ 1์ด ๊ฐ๊ฒฉ์ผ๋ก POPํ๋ ๊ฒ์
๋๋ค.
๊ตฌ๊ธ๋งํด์ IHost๋ฅผ ํตํด ์ค์ ํ์ผ์ ๊ฐ์ ธ์ค๋ ๊ฒ๊น์ง๋ ์ฑ๊ณตํ์ต๋๋ค.
ํ์ง๋ง ์๋น์ค๋ฅผ Singleton์ผ๋ก ๋ฑ๋กํ๋๋ Service๋ฅผ ํธ์ถํ๊ธฐ ์ ๊น์ง๋ ์์ฑ์๊ฐ ํธ์ถ๋๋ ๊ฒ๋ ๋ณผ ์ ์์๊ณ , ๊ทธ๋ ๋ค๊ณ Main ํจ์์ RunAsync๋ฅผ ํ๊ธฐ์ ์ while ๋ฌดํ๋ฃจํ๋ฅผ ๋๋ฆฌ์๋ ์ด๊ฑด ์๋๊ฑฐ ๊ฐ์ต๋๋ค. ์ด๋ป๊ฒ ๊ตฌํ ํ ์ ์์๊น์?
class Program
{
static async Task Main(string[] args)
{
using IHost host = CreateHostBuilder(args).UseConsoleLifetime().Build();
await host.RunAsync();
}
static IHostBuilder CreateHostBuilder(string[] args)
{
IHostBuilder rtnValue = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, configuration) =>
{
configuration.Sources.Clear();
IHostEnvironment env = hostingContext.HostingEnvironment;
configuration.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
//configuration.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
IConfigurationRoot configurationRoot = configuration.Build();
AzureServiceBusSettings settings = new();
configurationRoot.GetSection(nameof(AzureServiceBusSettings)).Bind(settings);
Console.WriteLine($"AzureServiceBus Key={settings.Endpoint}");
Console.WriteLine($"AzureSerivceBus QueueName={settings.QueueName}");
})
.ConfigureServices((context, services) =>
{
services.Configure<AzureServiceBusSettings>(context.Configuration);
services.AddSingleton<AzureServiceBusService>();
});
return rtnValue;
}
}
dimohy
3์ 29, 2021, 3:48์ค์
2
//
// ์์ฝ:
// Adds a singleton service of the type specified in serviceType with an instance
// specified in implementationInstance to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
//
// ๋งค๊ฐ ๋ณ์:
// services:
// The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service
// to.
//
// serviceType:
// The type of the service to register.
//
// implementationInstance:
// The instance of the service.
//
// ๋ฐํ ๊ฐ:
// A reference to this instance after the operation has completed.
public static IServiceCollection AddSingleton(this IServiceCollection services, Type serviceType, object implementationInstance);
์ด ๋ฉ์๋๋ฅผ ์จ๋ณด์ธ์
Vincent
3์ 29, 2021, 3:53์ค์
3
AzureServiceBusService ๋ผ๋ ํด๋์ค๊ฐ
public AzureServiceBusService(
ILogger<AzureServiceBusService> logger,
IHostApplicationLifetime appLifetime,
IOptions<AzureServiceBusSettings> settings)
{
_logger = logger;
_appLifetime = appLifetime;
connString = settings.Value.Endpoint;
queueName = settings.Value.QueueName;
timerState = new TimerState { Counter = TimerStateEnum.Run };
popTimer = new Timer(
callback: new TimerCallback(PopData),
state: timerState,
dueTime: 0,
period: 1000);
}
์์ ๊ฐ์ ์์ฑ์๋ฅผ ์ง๋๊ณ ์์ต๋๋คโฆ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๋ง๋ค์๋, ํ๋ผ๋ฏธํฐ๋ฅผ ์๋น์ค ๋ฑ๋ก ์ ๋ฐ์ง ๋ชปํ๋ ์ ์ด ์์ต๋๋ค. ์ด๋ ๊ฒํ๋ฉด static์ผ๋ก settings๊ฐ๋ค์ ๋ฐ์์ ๋๊ฒจ์ค์ผํ ๊ฑฐ๊ฐ์๋ฐ, ๊ทธ๋ ๊ฒ ํ๋ ๋ฐฉ๋ฒ ๋ฐ์ ์์๊น์โฆ?
dimohy
3์ 29, 2021, 3:55์ค์
4
์โฆ ์ธ์คํด์ค๋ฅผ ๋๊ฒจ์ฃผ๋ ๋ฐฉ์์
๋๋ค. ์ํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ธ์๋ก ๋๊ฒจ์ฃผ๋ฉด ๋์ด์
์ ๋ ์ฃผ๋ง์ ํฌ๋ก ์์
์ด ํ์ํด Quartz.net ์ด๋ Hangfire ์์๋ณด๋ค ๊ฐ๋ฒผ์ด ์๋น์ค๊ฐ ํ์ํด์ Hangfire Cronos๋ณด๊ณ ๊ฐ๋จํ ์ฐ๊ณ ์์ต๋๋ค. ํ์ํ์ ๊ฒ ๋ง๋๊ฑด์ง๋ ๋ชจ๋ฅด๊ฒ ๋ค์.
IHostedService๋ฅผ ํตํด ์๋น์ค๋ฅผ singleton์ผ๋ก ์์ฑํ๊ณ Cron Expression์ ์ด์ฉํด ํ์ด๋จธ๋ฅผ ์กฐ์ํฉ๋๋ค.
์ผ๋ถ ์์ ํด์ ์ฌ์ฉํด๋ณด๋ ์ ์๋ํฉ๋๋ค.
2๊ฐ์ ์ข์์
Vincent
3์ 29, 2021, 5:12์ค์
6
์โฆQuartz.NET ์ด ๋ญ์ง ๋ชจ๋ฅด๊ฒ ๋๋ฐ ๋์ค์ ์ฐพ์๋ด์ผ๊ฒ ๊ตฐ์.
IHostedService์์ ํํธ๋ฅผ ์ป์ด์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋๋ ์ํ๋ ๋๋ก ๋์ํฉ๋๋ค!!
๊ฐ์ฌํฉ๋๋ค.
class Program
{
static async Task Main(string[] args)
{
using IHost host = CreateHostBuilder(args).Build();
await host.RunAsync();
}
static IHostBuilder CreateHostBuilder(string[] args)
{
IHostBuilder rtnValue = Host.CreateDefaultBuilder(args)
.UseConsoleLifetime()
.ConfigureAppConfiguration((hostingContext, configuration) =>
{
configuration.Sources.Clear();
IHostEnvironment env = hostingContext.HostingEnvironment;
configuration.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
//configuration.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
IConfigurationRoot configurationRoot = configuration.Build();
AzureServiceBusSettings settings = new();
configurationRoot.GetSection(nameof(AzureServiceBusSettings)).Bind(settings);
Console.WriteLine($"AzureServiceBus Key={settings.Endpoint}");
Console.WriteLine($"AzureSerivceBus QueueName={settings.QueueName}");
})
.ConfigureServices((context, services) =>
{
services.Configure<AzureServiceBusSettings>(context.Configuration.GetSection(AzureServiceBusSettings.Settings)); // ์ฌ๊ธฐ ์์ ํ์ต๋๋ค.
services.AddHostedService<AzureServiceBusService>(); // ์ฌ๊ธฐ ์์ ํ์ต๋๋ค. Singleton -> AddHostedService
});
return rtnValue;
}
}
2๊ฐ์ ์ข์์