public async Task CanInvokeServiceTest() { var services = new ServiceCollection(); services.AddSingleton <ITestService, TestService2>(); services.AddSingleton <ITestService, TestService>(); var provider = services.BuildServiceProvider(); var invoker = InitDefaultConsumerInvoker(provider); var descriptor = new ConsumerExecutorDescriptor { ServiceTypeInfo = typeof(ITestService).GetTypeInfo(), ImplTypeInfo = typeof(TestService).GetTypeInfo(), MethodInfo = typeof(TestService).GetMethod("Index") }; descriptor.Attribute = descriptor.MethodInfo.GetCustomAttribute <TopicAttribute>(true); var context = new Internal.ConsumerContext(descriptor, _messageContext); var result = await invoker.InvokeAsync(context); Assert.NotNull(result); Assert.NotNull(result.Result); Assert.Equal("test", result.Result.ToString()); }
private static ConsumerExecutorDescriptor PrepareDescriptor <TIntegrationEventHandler, TIntegrationEvent>(DispatchMode broadcastMode, string groupName) where TIntegrationEventHandler : IEventHandler <TIntegrationEvent> where TIntegrationEvent : IntegrationEvent { var consExecDesc = new ConsumerExecutorDescriptor(); consExecDesc.ServiceTypeInfo = typeof(IEventHandler <TIntegrationEvent>).GetTypeInfo(); consExecDesc.MethodInfo = typeof(TIntegrationEventHandler).GetMethod("HandleAsync"); consExecDesc.ImplTypeInfo = typeof(TIntegrationEventHandler).GetTypeInfo(); consExecDesc.Attribute = new CapSubscribeAttribute(typeof(TIntegrationEvent).FullName); if (broadcastMode == DispatchMode.Broadcast && string.IsNullOrEmpty(groupName) == false) { consExecDesc.Attribute.Group = $"{typeof(TIntegrationEvent).FullName}-{groupName}"; } else { consExecDesc.Attribute.Group = typeof(TIntegrationEvent).FullName; } consExecDesc.MethodInfo.GetParameters() .Select(parameter => new ParameterDescriptor { Name = parameter.Name, ParameterType = parameter.ParameterType, }) .ToList() .ForEach(pDesc => consExecDesc.Parameters = new List <ParameterDescriptor> { pDesc }); return(consExecDesc); }
public async Task InvokeTest() { var descriptor = new ConsumerExecutorDescriptor() { Attribute = new CandidatesTopic("fake.output.withcancellation"), ServiceTypeInfo = typeof(FakeSubscriberWithCancellation).GetTypeInfo(), ImplTypeInfo = typeof(FakeSubscriberWithCancellation).GetTypeInfo(), MethodInfo = typeof(FakeSubscriberWithCancellation) .GetMethod(nameof(FakeSubscriberWithCancellation.CancellationTokenInjected)), Parameters = new List <ParameterDescriptor> { new ParameterDescriptor { ParameterType = typeof(CancellationToken), IsFromCap = true, Name = "cancellationToken" } } }; var header = new Dictionary <string, string>(); var message = new Message(header, null); var context = new ConsumerContext(descriptor, message); var cancellationToken = new CancellationToken(); var ret = await SubscribeInvoker.InvokeAsync(context, cancellationToken); Assert.Equal(cancellationToken, ret.Result); }
/// <summary> /// 替换参数 /// </summary> /// <param name="item">项</param> private void ReplaceMethodInfo(ConsumerExecutorDescriptor item) { var oldMethod = item.MethodInfo; var methodInfos = item.ImplTypeInfo.GetMethods(); var methodInfo = methodInfos.FirstOrDefault(x => x.ToString() == oldMethod.ToString()); item.MethodInfo = methodInfo ?? oldMethod; }
private ConsumerExecutorDescriptor InitDescriptor(TopicAttribute attr, MethodInfo methodInfo, TypeInfo implType) { var descriptor = new ConsumerExecutorDescriptor { Attribute = attr, MethodInfo = methodInfo, ImplTypeInfo = implType }; return(descriptor); }
/// <summary> /// Get a dictionary of specify topic candidates. /// The Key is Group name, the value is specify topic candidates. /// </summary> /// <param name="topicName">message topic name</param> public ConsumerExecutorDescriptor GetTopicExector(string queueName) { if (Entries == null) { throw new ArgumentNullException(nameof(Entries)); } ConsumerExecutorDescriptor groupd = null; Entries.TryGetValue(queueName, out groupd); return(groupd); }
private ConsumerExecutorDescriptor InitDescriptor(TopicAttribute attr, MethodInfo methodInfo, TypeInfo implHandler, TypeInfo serviceHandlerInfo, List <ParameterDescriptor> parameters) { var descriptor = new ConsumerExecutorDescriptor { Attribute = attr, MethodInfo = methodInfo, ImplTypeInfo = implHandler, ServiceTypeInfo = serviceHandlerInfo, Parameters = parameters }; return(descriptor); }
/// <summary> /// 替换上下文 /// </summary> /// <param name="item">消费者执行器定义者</param> /// <param name="provider">服务提供程序</param> private void ReplaceContext(ConsumerExecutorDescriptor item, IServiceProvider provider) { try { var aspectCoreType = provider.GetService(item.ServiceTypeInfo); item.ImplTypeInfo = aspectCoreType.GetType().GetTypeInfo(); ReplaceMethodInfo(item); Debug.WriteLine($"AspectCoreType: {aspectCoreType}"); Debug.WriteLine($"服务类型:{item.ServiceTypeInfo}, 实现类型: {item.ImplTypeInfo}, 方法: {item.MethodInfo}, 特性: {item.Attribute}"); } catch (InvalidOperationException e) { Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.sss}] Type: {item.ServiceTypeInfo}, Exception: {e}"); } }
public void CreateInvokerTest() { var methodInfo = typeof(Sample).GetRuntimeMethods() .Single(x => x.Name == nameof(Sample.ThrowException)); var description = new ConsumerExecutorDescriptor { MethodInfo = methodInfo, ImplTypeInfo = typeof(Sample).GetTypeInfo() }; var messageContext = new MessageContext(); var context = new ConsumerContext(description, messageContext); var invoker = consumerInvokerFactory.CreateInvoker(context); Assert.NotNull(invoker); }
/// <summary> /// Attempts to get the topic executor associated with the specified topic name and group name from the /// <see cref="Entries" />. /// </summary> /// <param name="topicName">The topic name of the value to get.</param> /// <param name="groupName">The group name of the value to get.</param> /// <param name="matchTopic">topic executor of the value.</param> /// <returns>true if the key was found, otherwise false. </returns> public bool TryGetTopicExecutor(string topicName, string groupName, out ConsumerExecutorDescriptor matchTopic) { if (Entries == null) { throw new ArgumentNullException(nameof(Entries)); } matchTopic = null; if (Entries.TryGetValue(groupName, out var groupMatchTopics)) { matchTopic = _selector.SelectBestCandidate(topicName, groupMatchTopics); return(matchTopic != null); } return(false); }
public async Task InvokeTest() { var descriptor = new ConsumerExecutorDescriptor() { Attribute = new CandidatesTopic("fake.output.integer"), ServiceTypeInfo = typeof(FakeSubscriber).GetTypeInfo(), ImplTypeInfo = typeof(FakeSubscriber).GetTypeInfo(), MethodInfo = typeof(FakeSubscriber).GetMethod(nameof(FakeSubscriber.OutputIntegerSub)), Parameters = new List <ParameterDescriptor>() }; var header = new Dictionary <string, string>(); var message = new Message(header, null); var context = new ConsumerContext(descriptor, message); var ret = await SubscribeInvoker.InvokeAsync(context); Assert.Equal(int.MaxValue, ret.Result); }
private ConsumerContext FackConsumerContext() { //Mock description var description = new ConsumerExecutorDescriptor { MethodInfo = GetType().GetMethod("WriteSubscriberInvokeAfterTest"), Attribute = new CapSubscribeAttribute("xxx"), ImplTypeInfo = GetType().GetTypeInfo() }; //Mock messageContext var messageContext = new MessageContext { Name = "Name", Group = "Group", Content = "Content" }; return(new ConsumerContext(description, messageContext)); }
public async void InvokeMethodTest(string methodName) { var methodInfo = typeof(Sample).GetRuntimeMethods() .Single(x => x.Name == methodName); var description = new ConsumerExecutorDescriptor { MethodInfo = methodInfo, ImplTypeInfo = typeof(Sample).GetTypeInfo() }; var messageContext = new MessageContext(); var context = new ConsumerContext(description, messageContext); var invoker = consumerInvokerFactory.CreateInvoker(context); await Assert.ThrowsAsync(typeof(Exception), async() => { await invoker.InvokeAsync(); }); }
private void RegisterMessageProcessor(ConsumerExecutorDescriptor descriptor) { _rabbitMqManager.OnMessageReceived += async(sender, transportMessage) => { try { _rabbitMqManager.CreateConnect(descriptor.Attribute.ExchangeName, descriptor.Attribute.QueueName); var message = new Message(transportMessage.Headers, Encoding.UTF8.GetString(transportMessage.Body)); var value = message.Value.ToString().ToObject <Message>(); var consumerContext = new ConsumerContext(descriptor, value); await Invoker.InvokeAsync(consumerContext, _cts.Token); _rabbitMqManager.Commit(sender); //_rabbitMQManager.Receive(descriptor.Attribute.ExchangeName, descriptor.Attribute.QueueName, (T) => Invoker.InvokeAsync(consumerContext, _cts.Token).GetAwaiter().GetResult()); //_rabbitMQManager.Commit(sender); } catch (Exception ex) { _rabbitMqManager.Commit(sender); //_rabbitMQManager.Reject(sender); } }; }
public void InvokeMethodTest(string methodName) { // Arrange var fixure = Create(); var methodInfo = typeof(Sample).GetRuntimeMethods() .Single(x => x.Name == methodName); var description = new ConsumerExecutorDescriptor { MethodInfo = methodInfo, ImplTypeInfo = typeof(Sample).GetTypeInfo() }; var messageContext = new MessageContext(); var context = new ConsumerContext(description, messageContext); var invoker = fixure.CreateInvoker(); Assert.Throws <Exception>(() => { invoker.InvokeAsync(context).GetAwaiter().GetResult(); }); }
/// <summary> /// create a new instance of <see cref="ConsumerContext" /> . /// </summary> /// <param name="descriptor">consumer method descriptor. </param> /// <param name="message"> received message.</param> public ConsumerContext(ConsumerExecutorDescriptor descriptor, MessageContext message) { ConsumerDescriptor = descriptor ?? throw new ArgumentNullException(nameof(descriptor)); DeliverMessage = message ?? throw new ArgumentNullException(nameof(message)); }
public void EnqueueToExecute(MediumMessage message, ConsumerExecutorDescriptor descriptor) { }