簡體   English   中英

我的IoC容器配置放在Service Fabric Service中的哪里?

[英]Where to put my IoC Container configuration in Service Fabric Service?

我當時在考慮將IoC容器依賴項配置放在Service類中的RunAsync方法下,但我感到不好,這不是正確的選擇。

Azure Service Fabric服務中是否有任何約定可以放置這種類型的配置而不會引起任何類型的沖突?

另外,您將在哪里放置處置電話?

注意:我正在為此使用Simple Injector,但其他IoC容器的其他示例也應該這樣做。

您可以在program.cs的啟動代碼中創建IoC容器。 注冊服務時,可以向其傳遞通過ServiceContext傳遞的回調方法。 擁有ServiceContext ,就可以使用它來訪問應用程序配置中的連接字符串等。

這是我用來創建Ninject內核的一些代碼。

namespace AccountCommandService
{
    internal static class Program
    {
        private static void Main()
        {
            try
            {
                ServiceRuntime.RegisterServiceAsync("AccountCommandServiceType",
                    context =>
                    {
                        // Create IoC container
                        var kernel = new ServiceKernel(context);

                        // Create Service
                        return new AccountCommandService(context, 
                            kernel.Get<IAccountDataContextFactory>(), // Pull a DBContext factory from the IoC
                            );
                    }).GetAwaiter().GetResult();

                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                throw;
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM