public void AddServices(IServiceCollection services) { var option = new CapBusOptions(); configure(option); services.AddSingleton <CapBusOptions>(x => option); services.AddSingleton <IEasyCachingBus, DefaultCAPBus>(); services.AddSingleton <IConsumerServiceSelector, EasyCachingConsumerServiceSelector>(); }
public void AddServices(IServiceCollection services) { var option = new CapBusOptions(); configure(option); services.AddSingleton <CapBusOptions>(x => option); services.AddSingleton <IEasyCachingBus, DefaultCAPBus>(); services.AddSingleton(DynamicCreateSubscribeClass.GetCacheSubscribeType(option)); }
/// <summary> /// 获取缓存订阅者的Type /// </summary> /// <param name="options"></param> /// <returns></returns> public static Type GetCacheSubscribeType(CapBusOptions options) { var type = typeof(CacheSubscribe); var moduleName = type.GetTypeInfo().Module.Name; var assemblyName = new AssemblyName(Guid.NewGuid().ToString()); var module = AssemblyBuilder .DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run) .DefineDynamicModule(moduleName); var builder = module.DefineType("CacheSubscribe", TypeAttributes.Class, typeof(CacheSubscribe)); var met = type.GetMethods(); MethodBuilder methodBuilder = builder.DefineMethod("ReceiveMessage", MethodAttributes.Public, null, new Type[] { typeof(EasyCachingMessage) }); Type[] ctorParams = new Type[] { typeof(string), typeof(string) }; //获取构造器信息 ConstructorInfo classCtorInfo = typeof(EasyCachingSubscribeAttribute).GetConstructor(ctorParams); //动态创建CapSubscribeAttribute CustomAttributeBuilder myCABuilder = new CustomAttributeBuilder( classCtorInfo, new object[] { options.TopicName, options.QueuePrefixName + GetQueueSuffixName() }); //将上面动态创建的Attribute附加到(动态创建的)类型MyType methodBuilder.SetCustomAttribute(myCABuilder); //生成指令 ILGenerator numberGetIL = methodBuilder.GetILGenerator(); numberGetIL.Emit(OpCodes.Ldarg_0); numberGetIL.Emit(OpCodes.Ldarg_1); numberGetIL.Emit(OpCodes.Call, type.GetMethod("ReceiveMessage", new Type[] { typeof(EasyCachingMessage) })); numberGetIL.Emit(OpCodes.Ret); return(builder.CreateTypeInfo().AsType()); }
/// <summary> /// /// </summary> /// <param name="capBus"></param> /// <param name="optionsAccs"></param> public DefaultCAPBus(ICapPublisher capBus, CapBusOptions optionsAccs) { _capBus = capBus; _options = optionsAccs; }