public TResponse Request <TResponse>(IQuery <TResponse> request) { var handlerType = typeof(IQueryHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse)); var wrapperType = typeof(QueryHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse)); var handler = _resolver.GetInstance(handlerType); if (handler == null) { throw new HandlerNotFoundException(request); } var wrapperHandler = (QueryHandler <TResponse>)Activator.CreateInstance(wrapperType, handler); return(wrapperHandler.Handle(request)); }
public async Task StartApplication() { MessageBrokerContext.SetEnvironment(Configuration.Environment); var azureConfiguration = new AzureServiceBusConfiguration { ConnectionString = File.ReadAllText($"{AppContext.BaseDirectory}\\ConnectionString.txt") }; var logInstance = new LoggerConfiguration() .MinimumLevel.Verbose() .Enrich.FromLogContext() .Enrich.WithProperty("Environment", "Testing") .WriteTo.Seq("http://localhost:5341") .CreateLogger(); _resolver = ConfigureDependencies.Initialise() .AddAzureServiceBusTransportProvider(azureConfiguration) .AddCoreDependencies() .ScanForHandlers(Assembly.GetExecutingAssembly()) .Build(); var configurationBuilder = _resolver.GetInstance <BusConfigurationBuilder>(); await configurationBuilder .SubscribeToQueue <TestQueue>() .SubscribeToEvent <TestEventV1>(Configuration.Subscription1) .SubscribeToEvent <MultipleSubscribersEventV1>(Configuration.Subscription1) .SubscribeToEvent <MultipleSubscribersEventV1>(Configuration.Subscription2) .PublishEvent <TestEventV1>() .PublishEvent <MultipleSubscribersEventV1>() .UseLogger(logInstance) .BuildAsync(); _monitor = _resolver.GetInstance <IMonitor>(); _monitor.Start(); Bus = _resolver.GetInstance <IBus>(); }
public async Task <HandlerResult> Handle <TMessage>(TMessage message) where TMessage : IMessage { var stopwatch = Stopwatch.StartNew(); var handler = _resolver.GetInstance <IHandle <TMessage> >(message, typeof(IHandle <>)); if (handler == null) { return(HandlerResult.ForHandlerNotFound(stopwatch.ElapsedMilliseconds)); } try { await handler.HandleAsync(message); return(HandlerResult.ForSuccess(stopwatch.ElapsedMilliseconds)); } catch (Exception exception) { return(HandlerResult.ForUnhandledError(exception, stopwatch.ElapsedMilliseconds)); } }
public TService GetInstance <TService>() { return(_resolver.GetInstance <TService>()); }