ASP.NET Core 中 Redis 中频道的订阅方法

我尝试使用 StackExchange.Redis 作为消息代理。它工作得很好,但我现在有一个缺点。我需要 MQ 来通信 2 个 ASP.NET Core 项目。 ConnectionMultiplexer有一个Subscriber方法将相同的 Action 委托订阅到 MQ。有什么办法可以不通过Startup课堂来做到这一点吗?


首先,我尝试借助属性类来实现这一点,代码如下


public class FireAndForgetAttribute<T> : Attribute

{

    public FireAndForgetAttribute(IConnectionMultiplexer plexer, string channelName, Action<T> reaction)

    {

            var subscriber = plexer.GetSubscriber();


            subscriber.Subscribe(channelName, (channel, message) => 

            {

                var data = JsonConvert.Deserialize<T>(message);


                reaction.Invoke(data);

            }, flags: CommandFlags.FireAndForget);

    }

}

但这不是一个好方法,因为 C# 中仍然没有通用属性(只是在路线图中标记为讨论的,没有其他),而且我也无法在这里传递Action<T>具体对象。


PPS 抱歉,我忘了改变我的问题的主题。现在是正确的了。


精慕HU
浏览 125回答 1
1回答

子衿沉夜

我这样做了,那是很久以前的事了,使用 Nancy、EasyQ 和 DryIOC它不是基于属性而是基于类将注册所有具体类的辅助方法IPipelineRegisterMultipleTypesToContainer(container, typeof(IPipeline));我想这就是 Scutor 所做的,但当时没有 scutor,所以必须使用 Reflection,然后订阅......private void RegisterMultipleTypesToContainer(IContainer container, Type type){&nbsp; var allTypes = AppDomain.CurrentDomain.GetAssemblies()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Where(x =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x.FullName.StartsWith("MyAssembly", StringComparison.Ordinal))SelectMany(x => x.GetTypes());&nbsp; &nbsp;var types = allTypes.Where(x => type.IsAssignableFrom(x) && x.IsClass && !x.IsAbstract).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var concreteTypes in types)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var genericTypes = concreteTypes.GetInterfaces().Where(x => x != type).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var genericType in genericTypes)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.Register(genericType, concreteTypes, Reuse.Singleton);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var pipeline = container.Resolve(genericType) as IPipeline;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pipeline.Subscribe();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;public class HelloPipeline :&nbsp; &nbsp; &nbsp; &nbsp; BasePipeline<HelloPipeline.Message>&nbsp; &nbsp; {&nbsp; &nbsp; IBusService _bus;&nbsp; &nbsp; HelloPipeline(IBusService bus){&nbsp; &nbsp; &nbsp;_bus = bus;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; private Task ExecuteAsync(String channel, ) { }&nbsp; &nbsp; &nbsp; &nbsp;public void Subscribe()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _bus.SubscribeAsync<T>(this.GetType().Name, this.ExecuteAsync);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }刚刚检查了旧代码,但仍然有效,希望它能给您一个想法,但我认为您想要实现的目标与我所做的相同,但使用其他工具
打开App,查看更多内容
随时随地看视频慕课网APP