面向云技术架构 - 痴者工良

  • 首页
  • 电子书
    • Istio 入门教程
    • kubernetes 教程
    • 从 C# 入门 Kafka
    • 多线程和异步
    • 动态编程-反射、特性、AOP
    • 表达式树
  • 文章导航
  • 隐私政策
愿有人陪你颠沛流离
遇到能让你付出的事物或者人,都是一种运气。
能遇到,就该珍惜。或许你们最终没能在一起,但你会切实地感受到力量。
正因为这样,那段相遇才变得有价值,才没有辜负这世间的每一段相遇。
  1. 首页
  2. .NET
  3. 正文

SignalR 实现自动注入客户端方法

2023年9月25日 364点热度 3人点赞 1条评论
内容纲要

C# 编写 SignalR 客户端时需要手动注入客户端方法:

            connection.On<string, string>("ReceiveMessage", (user, message) =>
            {
                this.Dispatcher.Invoke(() =>
                {
                   var newMessage = $"{user}: {message}";
                   messagesList.Items.Add(newMessage);
                });
            });

这样处理起来比较麻烦,因此需要实现自动扫描类型的实例方法自动注册。

定义一个保存实例方法信息模型类:

    public class RegisterMethod
    {
        public MethodInfo MethodInfo { get; init; }

        public bool IsAsync { get; init; }

        public Type[] Types { get; init; }
    }

定义特性注解,在需要绑定的实例方法中标记。

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class SignalRAttribute : Attribute
    {
        public string Name { get; private set; }
        public SignalRAttribute(string name)
        {
            Name = name;
        }
    }

然后实现扫码和自动注册:

    public static class SignalRHelper<T> where T : class
    {
        private static Dictionary<string, RegisterMethod> MethodCache = new();
        static SignalRHelper()
        {
            var methods = typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (var item in methods)
            {
                var attr = item.GetCustomAttribute<SignalRAttribute>();
                if (attr == null) continue;
                MethodCache.Add(attr.Name, new RegisterMethod
                {
                    MethodInfo = item,
                    IsAsync = item.ReturnType == typeof(Task),
                    Types = item.GetParameters().Select(x => x.ParameterType).ToArray()
                });
            }
        }

        /// <summary>
        /// 绑定方法到 HubConnection 中
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="targetObj"></param>
        public static void Bind(HubConnection connection, T targetObj)
        {
            foreach (var item in MethodCache)
            {
                MethodInfo? onMethod;

                // 有泛型参数
                if (item.Value.Types.Length > 0)
                {
                    var ms = typeof(HubConnectionExtensions).GetMethods(BindingFlags.Static | BindingFlags.Public)
                       .Where(x => x.Name == "On" &&
                       x.GetGenericArguments().Length == item.Value.Types.Length &&
                       x.GetParameters().Length == 3
                       );

                    // 异步方法 (HubConnection,methodName,Func<....,Task>)
                    if (item.Value.IsAsync)
                    {
                        onMethod = ms.FirstOrDefault(x => x.GetParameters()[2].ParameterType!.Name!.Contains("Func"));
                    }
                    // 异步方法 (HubConnection,methodName,Action<....>)
                    else
                    {
                        onMethod = ms.FirstOrDefault(x => x.GetParameters()[2].ParameterType!.Name!.Contains("Action"));
                    }
                    if (onMethod != null) onMethod = onMethod.MakeGenericMethod(item.Value.Types);
                }
                else
                {
                    if (item.Value.IsAsync)
                    {
                        onMethod = typeof(HubConnectionExtensions).GetMethod("On", BindingFlags.Static | BindingFlags.Public, new Type[]
                        {
                            typeof(HubConnection),
                            typeof(string),
                            typeof(Func<Task>)
                        });
                    }
                    else
                    {
                        onMethod = typeof(HubConnectionExtensions).GetMethod("On", BindingFlags.Static | BindingFlags.Public, new Type[]
                        {
                            typeof(HubConnection),
                            typeof(string),
                            typeof(Action)
                        });
                    }
                }

                if (onMethod == null) continue;
                var del = Delegate.CreateDelegate(onMethod.GetParameters()[2].ParameterType, targetObj, item.Value.MethodInfo);
                onMethod.Invoke(null, new object[]
                {
                    connection,
                    item.Key,
                    del
                });
            }
        }
    }

只需要在实例方法中标记即可:

        [SignalR("A")]
        public async Task A(string a, string b)
        {
            await Task.CompletedTask;
        }

使用:

MyService a = ....
SignalRHelper<MyService>.Bind(_connection, a);
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: signalr 客户端 方法 注入 自动
最后更新:2023年9月25日

痴者工良

高级程序员劝退师

点赞
< 上一篇
下一篇 >

文章评论

  • 迷恋自留地

    不明觉历

    2023年10月11日
    回复
  • razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
    取消回复

    标签聚合
    c net kubernetes asp 浅入 入门 core 多线程

    COPYRIGHT © 2023 whuanle.cn. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang

    粤ICP备18051778号

    粤公网安备 44030902003257号