📜 ⬆️ ⬇️

Deploying dependencies into the Apache Ignite.NET service

Developing various applications that use the popular Castle Windsor library for dependency injection and Apache Ignite.NET as the “key” that opens the door to cloud computing, I was faced with a slight inconvenience: I had no opportunity to implement the dependency into the service launched via the so-called service grid.

The reason why this happens is pretty banal. Apache Ignite.NET serializes the service, sends it to one of the available servers, where it is deserialized and launched. Since this process has no idea about Castle Windsor in any way, we get what we get.

To solve this problem, we need to create a plug-in for Apache Ignite.NET, which will receive the container responsible for the implementation of dependencies and allow the service to access it in order to receive this or that object.
')
First of all, we will introduce an additional level of abstraction for a container that provides dependency injection, so that in the future we can easily change its implementation to another one:

public interface IContainer { T Resolve<T>(); } 

 public class DependencyInjectionContainer : IContainer { protected IKernel Kernel { get; set; } public DependencyInjectionContainer(IKernel kernel) { Kernel = kernel; } public T Resolve<T>() { return Kernel.Resolve<T>(); } } 

 public class DependencyInjectionInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register( Component .For<IContainer>() .ImplementedBy<DependencyInjectionContainer>() ); } } 

To create a plugin, we need to create 3 classes: the class responsible for the configuration of the plugin, the provider of the plugin and, directly, the plugin itself.

 public class DependencyInjectionPlugin { public IContainer Container { get; set; } public T Resolve<T>() { return Container.Resolve<T>(); } } 

 [PluginProviderType(typeof(DependencyInjectionPluginProvider))] public class DependencyInjectionPluginConfiguration : IPluginConfiguration { public void WriteBinary(IBinaryRawWriter writer) { // No-op } public int? PluginConfigurationClosureFactoryId { get; } = null; // No Java part } 

 public class DependencyInjectionPluginProvider : IPluginProvider<DependencyInjectionPluginConfiguration> { public string Name { get; } = "DependencyInjection"; public string Copyright { get; } = "MIT"; protected DependencyInjectionPlugin DependencyInjectionPlugin { get; set; } public T GetPlugin<T>() where T : class { return DependencyInjectionPlugin as T; } public void Start(IPluginContext<DependencyInjectionPluginConfiguration> context) { DependencyInjectionPlugin = new DependencyInjectionPlugin(); } public void Stop(bool cancel) { } public void OnIgniteStart() { } public void OnIgniteStop(bool cancel) { } } 

Great, it remains to download the plugin in Apache Ignite.NET.

 public class IgniteInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register( Component .For<IIgnite>() .UsingFactoryMethod(() => Ignition.Start(new IgniteConfiguration { PluginConfigurations = new[] {new DependencyInjectionPluginConfiguration()} })) ); } } 

When the application starts, we, as usual, initialize the dependency injection container, but now we are ready to transfer it to the plugin we just wrote:

 var Done = new ManualResetEventSlim(false); // Build Windsor container using (var container = new WindsorContainer()) { // Install DI abstraction layer container.Install(new DependencyInjectionInstaller()); // Install cluster abstraction layer container.Install(new IgniteInstaller()); // Attach DI container to cluster plugin container .Resolve<IIgnite>() .GetPlugin<DependencyInjectionPlugin>("DependencyInjection") .Container = container.Resolve<IContainer>(); // Wait Done.Wait(); } 

Congratulations, if you have read to the end, then you can get any dependency from your container inside the service. It looks like this:

 public class ClientConnectionService : IClientConnectionService, IService { private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); [InstanceResource] private IIgnite Ignite { get; set; } public void Init(IServiceContext context) { Logger.Debug("Initialized"); } public void Execute(IServiceContext context) { var plugin = Ignite.GetPlugin<DependencyInjectionPlugin>("DependencyInjection"); var whatever = plugin.Resolve<IWhatever>(); whatever.DoSomething(); } public void Cancel(IServiceContext context) { Logger.Debug("Canceled"); } } 

Source: https://habr.com/ru/post/420479/


All Articles