gpt4 book ai didi

c# - Apache Ignite.NET 服务中的依赖注入(inject)

转载 作者:行者123 更新时间:2023-11-30 21:33:00 26 4
gpt4 key购买 nike

考虑提供服务网格的 Apache Ignite.NET 集群。

有一个简单的服务,可以在任何节点上运行:

public class ClientConnectionService : IClientConnectionService, IService
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

[InstanceResource] private IIgnite Ignite { get; set; }

public void Listen(string hostname, int port, uint username, string password,
ClientConnectionListenerOptions options = ClientConnectionListenerOptions.All)
{
Logger.Debug("Listen");
}

public void Init(IServiceContext context)
{
Logger.Debug("Initialized");
}

public void Execute(IServiceContext context)
{
Logger.Debug("Executed");
}

public void Cancel(IServiceContext context)
{
Logger.Debug("Canceled");
}
}

该应用程序使用 CaSTLe Windsor 作为控制容器的反转。

我想注入(inject)自定义依赖项,它不会被序列化并通过网络传输。

有什么办法可以实现吗?

注意在 Java 版本中,有一个 @SpringResource 注释,基本上可以做我想做的,但问题是关于 .NET,它只提供 [InstanceResource] 属性。

最佳答案

这是我最终得到的结果:

  1. 在描述了所有接口(interface)和契约的共享项目中,我引入了 IContainer
    public interface IContainer
{
T Resolve<T>();
}
  1. 在负责 Apache Ignite.NET 集成的项目中,我实现了简单的 Apache Ignite.NET 插件
    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)
{

}
}
  1. 在负责连接所有组件的主项目中,我实现了之前定义的 IContainer,并将其注册到 CaSTLe Windsor:
    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>()
);
}
}
  1. 在同一个项目中,我注册了 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()}
}))
);
}
}
  1. 最后,在应用程序的主要方法中:
    // 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();
}

就是这样。从现在开始,我可以像这样访问 Apache Ignite.NET 分布式服务中的 IContainer 实现:

    var plugin = Ignite.GetPlugin<DependencyInjectionPlugin>("DependencyInjection");
var whatever = plugin.Resolve<IWhatever>();

关于c# - Apache Ignite.NET 服务中的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51872358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com