首选的依赖引导方式

时间:2018-02-04 11:44:42

标签: .net-core inversion-of-control mqtt dryioc

所以,这是我要一起使用的事情列表。

  • Aspnetcore 2.0网络应用
  • DryIoC
  • MqttServerNet

Startup.cs

public IServiceProvider ConfigureServices(IServiceCollection services)
{
   //cut
   return IoCBootstrapper.Init(services);
}

IocBootsrapper

public static IServiceProvider Init(IServiceCollection services)
{
                var container = new Container();  
                container.WithDependencyInjectionAdapter(services,
                    throwIfUnresolved: type => type.Name.EndsWith("Controller"))
                    .ConfigureServiceProvider<IoCCompositionRoot>();          

                return container;

}

好的,非常简单的设置,IoCCompositionRoot只是IRegistrator我在整个应用中使用的服务注册。

现在,当我尝试将MqttServer配置到我的应用程序时问题就开始了。

ConfigureServices方法中,我应该像这样注册我的服务器。

var mqttServerOptions = new MqttServerOptions();
services.AddHostedMqttServer(mqttServerOptions);

这个香草设置工作得很好,但是......这就是我需要的东西。

var mqttServerOptions = new MqttServerOptions();
mqttServerOptions.SubscriptionInterceptor = context =>
{
      //instance of IUserService
      //check if user is allowed to subscribe
};
services.AddHostedMqttServer(mqttServerOptions);

现在问题变得明显了。 AddHostedMqttServerIServiceCollection上的一种扩展方法,我传递给IoCBootstrapper创建新的Container并执行WithDependencyInjectionAdapter(),以便注册DryIoC实施IServiceProvider等等,意味着我无法使用IUserService的此实例来配置我的MqttServerOptions,因为它们尚未配置。

我显然采取了错误的做法。

我确实提出了 hacky 这样做的方法,但必须有更好的选择。

有什么建议吗?

0 个答案:

没有答案
相关问题