コード例 #1
0
        public bool RegisterWaitHandle(Guid uuid, DataWaitHandle handle, ResponseStyle responseStyle)
        {
            lock (requests)
            {
                if (requests.ContainsKey(uuid))
                {
                    requests[uuid].SetHandleValue(handle);
                    requests[uuid].ResponseStyle = responseStyle;
                }
                else
                {
                    requests[uuid] = new RequestData(uuid, handle, responseStyle);
                }

                if (!requests[uuid].Set)
                {
                    return(false);
                }

                ProcessRemovals();
                return(true);
            }
        }
コード例 #2
0
        public void PublishAndWaitForResponse <TNeed, TSolution>(Message <TNeed, TSolution> message, HttpStatusCode successResponseCode, IHttpResponse response, ResponseStyle responseStyle = ResponseStyle.WholeSolution, Func <string, bool> solutionMatchFunction = null)
        {
            var busResponse = coreRequestStore.PublishAndWaitForResponse(message, responseStyle, solutionMatchFunction);

            if (string.IsNullOrWhiteSpace(busResponse.Solution) && string.IsNullOrWhiteSpace(busResponse.Errors))
            {
                response.SetStringContent("Failed to get a response from the bus");
                logger.Debug(
                    $"Operation=\"{nameof(RequestStore)}.{nameof(PublishAndWaitForResponse)}\" Event=\"Published message and received no response\" MessageId=\"{message.Uuid}\" MessageMethod=\"{message.Method}\" Message=\"{message}\"");
                response.HttpStatusCode = HttpStatusCode.RequestTimeout;
                return;
            }

            logger.Debug(
                $"Operation=\"{nameof(RequestStore)}.{nameof(PublishAndWaitForResponse)}\" Event=\"Published message and received response\" MessageId=\"{message.Uuid}\" MessageMethod=\"{message.Method}\"");

            List <ResponseError> errors = null;

            if (!string.IsNullOrWhiteSpace(busResponse.Errors))
            {
                errors = JsonConvert.DeserializeObject <List <ResponseError> >(busResponse.Errors);
            }

            SetResponse(successResponseCode, response, busResponse.Solution, errors);
        }
コード例 #3
0
 public RequestData(Guid uuid, DataWaitHandle handle, ResponseStyle responseStyle)
 {
     Uuid = uuid;
     SetHandleValue(handle);
     ResponseStyle = responseStyle;
 }
コード例 #4
0
        public JsonBusResponse PublishAndWaitForResponse <TNeed, TSolution>(Message <TNeed, TSolution> message, ResponseStyle responseStyle = ResponseStyle.WholeSolution,
                                                                            Func <string, bool> solutionMatchFunction = null)
        {
            Guid requestId = CreateUniqueId.Invoke();

            if (message.Uuid == Guid.Empty)
            {
                message.Uuid = requestId;
            }

            message.LastModifiedBy   = ServiceInfo.Name;
            message.LastModifiedTime = dateTimeProvider.UtcNow;

            if (!Methods.Contains(message.Method))
            {
                Methods.Add(message.Method);
                backplane.Send(new RequestMethodRegistration {
                    Method = message.Method
                });
            }

            logger.Trace($"{nameof(RequestStore)}.{nameof(PublishAndWaitForTypedResponse)}",
                         new LogItem("Event", "Preparing to publish message"),
                         new LogItem("MessageId", message.Uuid.ToString),
                         new LogItem("MessageMethod", message.Method));

            var response = new JsonBusResponse();

            using (DataWaitHandle dataWaitHandle = new DataWaitHandle(false, EventResetMode.AutoReset, solutionMatchFunction))
            {
                requestMatcher.RegisterWaitHandle(message.Uuid, dataWaitHandle, responseStyle);

                queueWrapper.PublishMessage(message, message.Uuid);

                if (dataWaitHandle.WaitOne(busTimeoutMilliseconds))
                {
                    List <ResponseError> errors = null;
                    if (dataWaitHandle.Errors != null)
                    {
                        errors = JsonConvert.DeserializeObject <List <ResponseError> >(dataWaitHandle.Errors);
                    }

                    logger.Trace($"{nameof(RequestStore)}.{nameof(PublishAndWaitForTypedResponse)}",
                                 new LogItem("Event", "Published message and received response"),
                                 new LogItem("MessageId", message.Uuid.ToString),
                                 new LogItem("MessageMethod", message.Method));

                    if (errors != null && errors.Any())
                    {
                        response.Errors = dataWaitHandle.Errors;
                    }

                    response.Solution = dataWaitHandle.Solution;
                }

                if (string.IsNullOrEmpty(response.Errors) && string.IsNullOrEmpty(response.Solution))
                {
                    logger.Trace($"{nameof(RequestStore)}.{nameof(PublishAndWaitForTypedResponse)}",
                                 new LogItem("Event", "Published message and received no response"),
                                 new LogItem("MessageId", message.Uuid.ToString),
                                 new LogItem("MessageMethod", message.Method));
                }

                return(response);
            }
        }
コード例 #5
0
        public BusResponse <TSolution> PublishAndWaitForTypedResponse <TNeed, TSolution>(Message <TNeed, TSolution> message, ResponseStyle responseStyle = ResponseStyle.WholeSolution, Func <string, bool> solutionMatchFunction = null,
                                                                                         JsonSerializerSettings jsonSerializerSettings = null)
        {
            var response = PublishAndWaitForResponse(message, responseStyle, solutionMatchFunction);

            var solution = default(IEnumerable <TSolution>);

            if (response.Solution != null)
            {
                solution = JsonConvert.DeserializeObject <IEnumerable <TSolution> >(response.Solution, jsonSerializerSettings);
            }

            return(new BusResponse <TSolution>
            {
                Solution = solution,
                Errors = response.Errors
            });
        }