Пример #1
0
        /// <summary>
        /// Start the application context
        /// </summary>
        public bool Start()
        {
            this.m_tracer.TraceInfo("Starting mini-context");

            String scheme = this.m_configuration.UseTls ? "https" : "http",
                   host   = $"{scheme}://{this.m_configuration.RealmId}:{this.m_configuration.Port}/";

            this.m_tracer.TraceInfo("Contacting {0}", host);
            try
            {
                // Options on AMI
                var optionDescription = new AdminClientDescription()
                {
                    Binding = new ServiceClientBindingDescription()
                    {
                        Security = new SecurityConfigurationDescription()
                    }
                };

                if (!String.IsNullOrEmpty(this.m_configuration.Proxy))
                {
                    this.m_tracer.TraceVerbose("Setting proxy to : {0}", this.m_configuration.Proxy);
                    WebRequest.DefaultWebProxy = new WebProxy(this.m_configuration.Proxy);
                }

                this.m_tracer.TraceVerbose("Setting up endpoint : {0}/ami", host);

                optionDescription.Endpoint.Add(new AdminClientEndpointDescription($"{host}/ami"));
                var amiServiceClient = new AmiServiceClient(new RestClient(optionDescription));

                // get options
                var amiOptions = amiServiceClient.Options();

                // Server version
                if (new Version(amiOptions.InterfaceVersion.Substring(0, amiOptions.InterfaceVersion.LastIndexOf(".")) + ".0") > typeof(AmiServiceClient).Assembly.GetName().Version)
                {
                    throw new InvalidOperationException($"Server version of AMI is too new for this version of console. Expected {typeof(AmiServiceClient).Assembly.GetName().Version} got {amiOptions.InterfaceVersion}");
                }

                foreach (var itm in amiOptions.Endpoints)
                {
                    this.m_tracer.TraceInfo("Server supports {0} at {1}", itm.ServiceType, String.Join(",", itm.BaseUrl).Replace("0.0.0.0", this.m_configuration.RealmId));

                    var config = new AdminClientDescription()
                    {
                        Binding = new ServiceClientBindingDescription()
                    };
                    if (itm.Capabilities.HasFlag(ServiceEndpointCapabilities.Compression))
                    {
                        config.Binding.Optimize = true;
                    }


                    if (itm.Capabilities.HasFlag(ServiceEndpointCapabilities.BearerAuth))
                    {
                        config.Binding.Security = new SecurityConfigurationDescription()
                        {
                            CredentialProvider = new TokenCredentialProvider(),
                            Mode = Core.Http.Description.SecurityScheme.Bearer,
                            PreemptiveAuthentication = true
                        }
                    }
                    ;
                    else if (itm.Capabilities.HasFlag(ServiceEndpointCapabilities.BasicAuth))
                    {
                        if (itm.ServiceType == ServiceEndpointType.AuthenticationService)
                        {
                            config.Binding.Security = new SecurityConfigurationDescription()
                            {
                                CredentialProvider = new OAuth2CredentialProvider(),
                                Mode = Core.Http.Description.SecurityScheme.Basic,
                                PreemptiveAuthentication = true
                            }
                        }
                        ;
                        else
                        {
                            config.Binding.Security = new SecurityConfigurationDescription()
                            {
                                CredentialProvider = new HttpBasicTokenCredentialProvider(),
                                Mode = Core.Http.Description.SecurityScheme.Basic,
                                PreemptiveAuthentication = true
                            }
                        };
                    }

                    config.Endpoint.AddRange(itm.BaseUrl.Select(o => new AdminClientEndpointDescription(o.Replace("0.0.0.0", this.m_configuration.RealmId))));

                    // Add client
                    this.m_restClients.Add(itm.ServiceType, new RestClient(
                                               config
                                               ));
                }

                // Attempt to get server time from clinical interface which should challenge
                this.GetRestClient(ServiceEndpointType.ImmunizationIntegrationService)?.Get("/time");
                return(true);
            }
            catch (Exception ex)
            {
#if DEBUG
                this.m_tracer.TraceError("Cannot start services: {0}", ex);
#else
                this.m_tracer.TraceError("Cannot start services: {0}", ex);
#endif
                return(false);
            }
        }