Exemplo n.º 1
0
        private async ValueTask <IAppConnection> ResolveTargetConnectionAsync(
            IProvidedMethodReference methodReference,
            IAppConnection source,
            ITransportChannel sourceChannel,
            IContextLinkageOptions contextLinkageOptions)
        {
            if (methodReference.ProvidedService.ConnectionId.HasValue)
            {
                var connectionId = methodReference.ProvidedService.ConnectionId.Value;
                if (!_appLifecycleManager.TryGetOnlineConnection(connectionId, out var connection))
                {
                    throw new InvalidOperationException($"The requested connection {connectionId} is not online");
                }
                return(connection);
            }

            var appId = methodReference.ProvidedService.ApplicationId;

            if (methodReference.ProvidedService.ApplicationInstanceId.HasValue)
            {
                var appInstanceId = methodReference.ProvidedService.ApplicationInstanceId.Value;
                if (appId.HasValue && _appLifecycleManager.TryGetConnectionInProgress(appInstanceId, appId.Value, out var connectionInProgress))
                {
                    return(await connectionInProgress);
                }
                var connections = _appLifecycleManager.GetAppInstanceConnections(appInstanceId).ToList();
                if (connections.Count == 0)
                {
                    throw new InvalidOperationException($"App instance {appInstanceId} is doesn't have online connections");
                }

                if (appId.HasValue)
                {
                    var connection = connections.FirstOrDefault(c => c.Info.ApplicationId.Equals(appId.Value));
                    if (connection == null)
                    {
                        throw new InvalidOperationException($"App instance {appInstanceId} is doesn't have connection with {appId.Value} application id");
                    }
                    return(connection);
                }

                if (connections.Count == 1)
                {
                    return(connections.Single());
                }

                throw new InvalidOperationException($"App instance {appInstanceId} has several connections, you need to specify ApplicationId to make call to specific connection");
            }

            if (!appId.HasValue)
            {
                throw new InvalidOperationException($"AppId is required to resolve target connection for {methodReference} provided method reference");
            }
            var appIdValue = appId.Value;

            var method     = _registryService.GetProvidedMethod(methodReference);
            var launchMode = GetLaunchMode(method);

            Task <ResolvedConnection> resolveTask;

            lock (_resolveConnectionSync)
            {
                if (launchMode != LaunchMode.MultiInstance)
                {
                    var onlineConnections = _appLifecycleManager
                                            .GetOnlineConnections()
                                            .Where(x => x.Info.ApplicationId.Equals(appIdValue) &&
                                                   !x.Id.Equals(source.Id)).ToArray();

                    if (_contextLinkageManager.IsContextShouldBeConsidered(contextLinkageOptions, source))
                    {
                        onlineConnections = _contextLinkageManager
                                            .GetAppsInContexts(contextLinkageOptions, source, true)
                                            .Join(onlineConnections, x => x.ConnectionId.Value, y => y.Id, (x, y) => y)
                                            .ToArray();
                    }

                    if (onlineConnections.Any())
                    {
                        return(onlineConnections.First());
                    }
                }

                if (launchMode == LaunchMode.None || !_appLifecycleManager.CanBeLaunched(appIdValue))
                {
                    throw new InvalidOperationException(
                              $"The requested app {appIdValue} is not online and cannot be launched");
                }

                var resolveMode = ConvertToResolveMode(launchMode);

                resolveTask = _appLifecycleManager.LaunchAndConnectAsync(appIdValue, resolveMode, source.Info);
            }
            var resolvedConnection = await resolveTask.ConfigureAwait(false);

            return(resolvedConnection.AppConnection);
        }