我的IoC容器配置放在Service Fabric Service中的哪里? [英] Where to put my IoC Container configuration in Service Fabric Service?

查看:56
本文介绍了我的IoC容器配置放在Service Fabric Service中的哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在考虑将IoC容器依赖项配置放在Service类中的RunAsync方法下,但我感到不好,这不是正确的选择.

I was thinking in placing the IoC Container dependencies configuration under RunAsync method in the Service class but I have a bad feeling that's not the right place to go..

Azure Service Fabric服务中是否存在用于放置这种类型的配置而不引起任何类型冲突的约定?

Is there any convention in Azure Service Fabric Services to place this type of configurations without causing any sort of conflicts?

此外,您将在哪里放置处置电话?

Also, where would you place the dispose call?

注意:我正在为此使用Simple Injector,但其他IoC容器中的其他示例也应该这样做.

Note: I'm using Simple Injector for this but other examples from other IoC containers should do too.

推荐答案

您可以在program.cs的启动代码中创建IoC容器.注册服务时,可以向其传递通过ServiceContext传递的回调方法.一旦有了ServiceContext,就可以使用它来访问应用程序配置中的连接字符串等.

You can create your IoC container in the startup code in program.cs. When you register a service, you can pass it a callback method that is passed the ServiceContext. Once you have the ServiceContext, you can use it to gain access to your application configuration for connection strings and such.

这是我用来创建Ninject内核的一些代码.

Here's some code I use to create a Ninject kernel.

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;
            }
        }
    }
}

这篇关于我的IoC容器配置放在Service Fabric Service中的哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆