Exemplo n.º 1
0
        public async Task HandleAsync(
            IServiceDiscoveryRequest request,
            IClientConnection sourceConnection,
            ITransportChannel sourceChannel)
        {
            IReadOnlyCollection <(IConsumedMethod Consumed, IProvidedMethod Provided)> methodMatches;

            if (request.ConsumedService.HasValue)
            {
                methodMatches = _registryService.GetMethodMatches(
                    sourceConnection.Info.ApplicationId,
                    request.ConsumedService.Value);
            }
            else
            {
                methodMatches = _registryService.GetMethodMatches(sourceConnection.Info.ApplicationId);
            }
            IEnumerable <IGrouping <(IConsumedService ConsumedService, IProvidedService ProvidedService, Maybe <UniqueId> ConnectionId), IProvidedMethod> > groupedMethods;

            if (request.DiscoveryMode == DiscoveryMode.Offline)
            {
                groupedMethods = methodMatches
                                 .GroupBy(
                    x => (
                        x.Consumed.ConsumedService,
                        x.Provided.ProvidedService,
                        ConnectionId: Maybe <UniqueId> .Nothing),
                    x => x.Provided);
            }
            else
            {
                var onlineConnections = _connectionTracker.GetOnlineConnections();
                groupedMethods = methodMatches
                                 .Join(onlineConnections, x => x.Provided.ProvidedService.Application.Id, y => y.Info.ApplicationId, (x, y) => (Match: x, ConnectionId: y.Id))
                                 .GroupBy(
                    x => (
                        x.Match.Consumed.ConsumedService,
                        x.Match.Provided.ProvidedService,
                        new Maybe <UniqueId>(x.ConnectionId)),
                    x => x.Match.Provided);
            }
            var discoveredServices =
                from s in groupedMethods
                let consumedService = s.Key.ConsumedService
                                      let providedService = s.Key.ProvidedService
                                                            let connectionId = s.Key.ConnectionId
                                                                               select _protocol.MessageFactory.CreateDiscoveredService(
                    _protocol.MessageFactory.CreateConsumedServiceReference(
                        consumedService.Service.Id,
                        consumedService.Alias),
                    _protocol.MessageFactory.CreateProvidedServiceReference(
                        providedService.Service.Id,
                        providedService.Alias,
                        providedService.Application.Id,
                        connectionId),
                    s.Key.ProvidedService.Title,
                    s.Select(m =>
                             _protocol.MessageFactory.CreateDiscoveredServiceMethod(
                                 m.Method.Name,
                                 m.Title,
                                 m.Method.InputMessage.Id,
                                 m.Method.OutputMessage.Id,
                                 Convert(m.Method.Type))).ToList());

            using (var response = _protocol.MessageFactory.CreateServiceDiscoveryResponse(discoveredServices.ToList()))
            {
                Log.Info("Completed service discovery request {{{0}}} from {{{1}}}: {2}", request, sourceConnection, response);
                var serializedResponse = _protocol.Serializer.Serialize(response);
                try
                {
                    await sourceChannel.Out
                    .WriteAsync(new TransportMessageFrame(serializedResponse))
                    .ConfigureAwait(false);
                }
                catch
                {
                    serializedResponse.Dispose();
                    throw;
                }
            }
        }