示例#1
0
        private void InitRegistrations(IEnumerable <IRegistration> registrations)
        {
            foreach (var registration in registrations)
            {
                registration.Create(this);
            }

            foreach (var registration in registrations)
            {
                registration.Process(this);
            }

            foreach (var routeMap in new List <RouteMap> {
                DefaultRouteMap
            }.Concat(Contexts))
            {
                foreach (var route in routeMap)
                {
                    MessagingEngine.AddProcessingGroup(route.ProcessingGroupName, route.ProcessingGroup);
                }

                routeMap.ResolveRoutes(_endpointProvider);
            }
        }
示例#2
0
        private void EnsureEndpoints()
        {
            var allEndpointsAreValid = true;
            var errorMessage         = new StringBuilder("Some endpoints are not valid:").AppendLine();

            Log.WriteInfo(nameof(CqrsEngine), nameof(EnsureEndpoints), "Endpoints verification");

            foreach (var context in new [] { DefaultRouteMap }.Concat(Contexts))
            {
                foreach (var route in context)
                {
                    MessagingEngine.AddProcessingGroup(route.ProcessingGroupName, route.ProcessingGroup);
                }
            }

            foreach (var routeMap in (new[] { DefaultRouteMap }).Concat(Contexts))
            {
                Log.WriteInfo(nameof(CqrsEngine), nameof(EnsureEndpoints), $"Context '{routeMap.Name}':");

                routeMap.ResolveRoutes(_endpointProvider);
                foreach (var route in routeMap)
                {
                    Log.WriteInfo(nameof(CqrsEngine), nameof(EnsureEndpoints), $"\t{route.Type} route '{route.Name}':");
                    foreach (var messageRoute in route.MessageRoutes)
                    {
                        string error;
                        var    routingKey      = messageRoute.Key;
                        var    endpoint        = messageRoute.Value;
                        var    messageTypeName = route.Type.ToString().ToLower().TrimEnd('s');
                        bool   result          = true;

                        if (routingKey.CommunicationType == CommunicationType.Publish)
                        {
                            if (!MessagingEngine.VerifyEndpoint(endpoint, EndpointUsage.Publish, _createMissingEndpoints, out error))
                            {
                                errorMessage
                                .AppendFormat(
                                    "Route '{1}' within bounded context '{0}' for {2} type '{3}' has resolved endpoint {4} that is not properly configured for publishing: {5}.",
                                    routeMap.Name,
                                    route.Name,
                                    messageTypeName,
                                    routingKey.MessageType.Name,
                                    endpoint, error)
                                .AppendLine();
                                result = false;
                            }

                            Log.WriteInfo(
                                nameof(CqrsEngine),
                                nameof(EnsureEndpoints),
                                $"\t\tPublishing  '{routingKey.MessageType.Name}' to {endpoint}\t{(result ? "OK" : "ERROR:" + error)}");
                        }

                        if (routingKey.CommunicationType == CommunicationType.Subscribe)
                        {
                            if (!MessagingEngine.VerifyEndpoint(endpoint, EndpointUsage.Subscribe, _createMissingEndpoints, out error))
                            {
                                errorMessage
                                .AppendFormat(
                                    "Route '{1}' within bounded context '{0}' for {2} type '{3}' has resolved endpoint {4} that is not properly configured for subscription: {5}.",
                                    routeMap.Name,
                                    route.Name,
                                    messageTypeName,
                                    routingKey.MessageType.Name,
                                    endpoint,
                                    error)
                                .AppendLine();
                                result = false;
                            }

                            Log.WriteInfo(
                                nameof(CqrsEngine),
                                nameof(EnsureEndpoints),
                                $"\t\tSubscribing '{routingKey.MessageType.Name}' on {endpoint}\t{(result ? "OK" : "ERROR:" + error)}");
                        }
                        allEndpointsAreValid = allEndpointsAreValid && result;
                    }
                }
            }

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