Exemple #1
0
        public async Task <TResult> GetAsync <TResult>(string path, object moduleRequest) where TResult : class
        {
            var registration = _moduleRegistry.GetRequestRegistration(path);

            if (registration is null)
            {
                throw new InvalidOperationException($"No action has been defined for path: {path}");
            }

            var moduleRequestType = moduleRequest.GetType();

            _logger.LogTrace($"Getting data from path {path} using module request of type " +
                             $"{{{moduleRequestType.Name}}} from module {moduleRequestType.Assembly.FullName}...");

            var action          = registration.Action;
            var receiverRequest = TranslateType(moduleRequest, registration.ReceiverType);

            _logger.LogTrace($"Translated module request {moduleRequestType.Name} to {registration.ReceiverType.Name}");

            var result = await action(_serviceProvider, receiverRequest);

            var resultJson = JsonConvert.SerializeObject(result);

            _logger.LogTrace($"Returning data from path {path} of type {typeof(TResult).Name}");

            return(JsonConvert.DeserializeObject <TResult>(resultJson));
        }
Exemple #2
0
        public async Task <TResult> SendAsync <TResult>(string path, object request) where TResult : class
        {
            var registration = _moduleRegistry.GetRequestRegistration(path);

            if (registration is null)
            {
                throw new InvalidOperationException($"No action has been defined for path: '{path}'.");
            }

            var receiverRequest = TranslateType(request, registration.RequestType);
            var result          = await registration.Action(receiverRequest);

            return(result is null ? null : TranslateType <TResult>(result));
        }
Exemple #3
0
        public async Task <TResult> RequestAsync <TResult>(string path, object request) where TResult : class
        {
            var registration = _moduleRegistry.GetRequestRegistration(path);

            if (registration is null)
            {
                throw new InvalidOperationException($"No action has been defined for path: {path}");
            }

            if (request is IMessage message)
            {
                // A synchronous request
                message.Id = Guid.Empty;
            }

            var receiverRequest = TranslateType(request, registration.RequestType);
            var result          = await registration.Action(receiverRequest);

            return(_serializer.Deserialize <TResult>(_serializer.Serialize(result)));
        }