示例#1
0
        private void EnsureEndpoints(CommunicationType processingCommunicationType)
        {
            var allEndpointsAreValid = true;
            var errorMessage         = new StringBuilder("Some endpoints are not valid:").AppendLine();
            var endpointMessagesDict = new Dictionary <Endpoint, string>();

            _log.WriteInfo(nameof(CqrsEngine), nameof(EnsureEndpoints), $"Endpoins verification for {processingCommunicationType}");

            foreach (var routeMap in new List <RouteMap> {
                DefaultRouteMap
            }.Concat(Contexts))
            {
                foreach (var route in routeMap)
                {
                    foreach (var messageRoute in route.MessageRoutes)
                    {
                        var routingKey = messageRoute.Key;
                        if (routingKey.CommunicationType != processingCommunicationType)
                        {
                            continue;
                        }

                        var endpoint = messageRoute.Value;
                        endpointMessagesDict[endpoint] =
                            $"Context {routeMap.Name}: "
                            + (processingCommunicationType == CommunicationType.Publish
                                ? $"publishing '{routingKey.MessageType.Name}' to"
                                : $"subscribing '{routingKey.MessageType.Name}' on")
                            + $" {endpoint}\t{{0}}";
                    }
                }
            }

            var endpointsErrorsDict = MessagingEngine.VerifyEndpoints(
                processingCommunicationType == CommunicationType.Publish ? EndpointUsage.Publish : EndpointUsage.Subscribe,
                endpointMessagesDict.Keys,
                _createMissingEndpoints);

            foreach (var endpointError in endpointsErrorsDict)
            {
                string messagePattern = endpointMessagesDict[endpointError.Key];
                if (string.IsNullOrWhiteSpace(endpointError.Value))
                {
                    _log.WriteInfo(nameof(CqrsEngine), nameof(EnsureEndpoints), string.Format(messagePattern, "OK"));
                }
                else
                {
                    _log.WriteError(
                        nameof(CqrsEngine),
                        nameof(EnsureEndpoints),
                        new InvalidOperationException(string.Format(messagePattern, $"ERROR: {endpointError.Value}")));
                }
            }

            if (!allEndpointsAreValid)
            {
                throw new ApplicationException(errorMessage.ToString());
            }
        }