示例#1
0
        /// <summary>
        /// Sets up NAME.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>Returns the <see cref="ParsedDependencies"/>.</returns>
        public static ParsedDependencies UseNAME(
            this IApplicationBuilder app,
            NAMEAspNetCoreConfiguration config)
        {
            var env            = app.ApplicationServices.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment;
            var filePathMapper = new AspNetCoreFilePathMapper(env);
            var dependencies   = ReadAndLogDependencies(config, false, filePathMapper, out NAMESettings settings);

            if (settings.RunningMode == SupportedNAMEBehaviours.NAMEDisabled)
            {
                LogInfo("Not starting NAME since it is disabled in the dependencies file.", false);
                return(dependencies);
            }

            if (settings.RunningMode < SupportedNAMEBehaviours.BootstrapDisabled)
            {
                var register = new Registration.Register();

                //TODO: NAME-32

                register.RegisterInstance(
                    filePathMapper,
                    config.APIName,
                    config.APIVersion,
                    config.DependenciesFilePath,
                    settings,
                    Environment.MachineName,
                    null,
                    typeof(ApplicationBuilderExtensions).GetTypeInfo().Assembly.GetName().Version.ToString(),
                    (config.ManifestUriPrefix?.TrimEnd('/') ?? string.Empty) + Constants.MANIFEST_ENDPOINT,
                    Constants.REGISTRY_SUPPORTED_PROTOCOL_VERSIONS);
            }

            if (settings.RunningMode == SupportedNAMEBehaviours.NAMEDisabled)
            {
                LogInfo("Not starting NAME since it is disabled in the Registry.", false);
                return(dependencies);
            }

            app.UseMiddleware <NAMEMiddleware>(config, settings, filePathMapper);

            return(dependencies);
        }
示例#2
0
        /// <summary>
        /// Enables the NAME middleware.
        /// </summary>
        /// <param name="httpConfig">The HTTP configuration.</param>
        /// <param name="configure">The configuration action.</param>
        /// <returns>Returns the dependencies parsed from the dependencies file.</returns>
        public static ParsedDependencies EnableNAME(this HttpConfiguration httpConfig, Action <NAMEConfiguration> configure)
        {
            var config = new NAMEConfiguration();

            configure?.Invoke(config);

            var pathMapper = new WebApiFilePathMapper();

            var dependencies = ReadAndLogDependencies(config, false, pathMapper, out NAMESettings settings);

            if (settings.RunningMode == SupportedNAMEBehaviours.NAMEDisabled)
            {
                LogInfo("Not starting NAME since it is disabled in the dependencies file.", false);
                return(dependencies);
            }

            var routeTemplate = string.IsNullOrEmpty(config.ManifestUriPrefix) ? Constants.MANIFEST_ENDPOINT.TrimStart('/') : config.ManifestUriPrefix.TrimEnd('/') + Constants.MANIFEST_ENDPOINT;

            var nameEndpoint = $"/{routeTemplate}";

            if (!string.IsNullOrEmpty(httpConfig.VirtualPathRoot))
            {
                nameEndpoint = httpConfig.VirtualPathRoot.TrimEnd('/') + nameEndpoint;
            }

            // Register in the registry

            if (settings.RunningMode < SupportedNAMEBehaviours.BootstrapDisabled)
            {
                var register = new Registration.Register();

                register.RegisterInstance(
                    pathMapper,
                    config.APIName,
                    config.APIVersion,
                    config.DependenciesFilePath,
                    settings,
                    Environment.MachineName,
                    null,
                    typeof(NAMEHandler).Assembly.GetName().Version.ToString(),
                    nameEndpoint,
                    Constants.REGISTRY_SUPPORTED_PROTOCOL_VERSIONS);
            }

            httpConfig.Routes.MapHttpRoute(
                "NAME_Manifest",
                routeTemplate,
                null,
                null,
                new NAMEHandler(config.APIName, config.APIVersion, config.DependenciesFilePath, pathMapper, settings));

            httpConfig.Routes.MapHttpRoute(
                "NAME_Ui",
                routeTemplate + "/ui",
                null,
                null,
                new NAMEUiHandler(settings));

            // This is hacky and should be revised.
            // been revised, still the same
            NAMEEndpointHttpModule.NameEndpoint = nameEndpoint;
            HttpApplication.RegisterModule(typeof(NAMEEndpointHttpModule));

            return(dependencies);
        }
示例#3
0
        public static ParsedDependencies Initialize(ISelfHostServer server, IFilePathMapper pathMapper, NAMESelfHostConfiguration configuration)
        {
            NAMESettings settings;

            ParsedDependencies dependencies = ReadAndLogDependencies(configuration, configuration.LogHealthCheckToConsole, pathMapper, out settings);

            if (settings.RunningMode == SupportedNAMEBehaviours.NAMEDisabled)
            {
                LogInfo("Not starting NAME since it is disabled in the dependencies file.", configuration.LogHealthCheckToConsole);
                return(dependencies);
            }

            int portNumber = settings.SelfHostPortRangeFirst;

            try
            {
                bool result = false;
                LogInfo($"Starting the server", configuration.LogHealthCheckToConsole);
                while (portNumber <= settings.SelfHostPortRangeLast)
                {
                    result = server.Start(portNumber, settings);
                    if (result)
                    {
                        break;
                    }
                    portNumber++;
                }
                if (result == false)
                {
                    throw new NAMEException("Tried all ports, without success.");
                }

                Console.WriteLine($"Succesfully started the server. Listening on {portNumber}.");
            }
            catch (Exception ex)
            {
                LogWarning($"Could not bind to the SelfHost address: {ex.Message}.", configuration.LogHealthCheckToConsole);
                if (configuration.ThrowOnDependenciesFail)
                {
                    if (ex is NAMEException)
                    {
                        throw;
                    }
                    else
                    {
                        throw new NAMEException("Could not bind to the SelfHost address.", ex);
                    }
                }
                return(dependencies);
            }

            if (settings.RunningMode < SupportedNAMEBehaviours.BootstrapDisabled)
            {
                var register = new Registration.Register();

                register.RegisterInstance(
                    pathMapper,
                    configuration.APIName,
                    configuration.APIVersion,
                    configuration.DependenciesFilePath,
                    settings,
                    Environment.MachineName,
                    (uint)portNumber,
                    typeof(SelfHostInitializer).GetTypeInfo().Assembly.GetName().Version.ToString(),
                    (configuration.ManifestUriPrefix?.TrimEnd('/') ?? string.Empty) + Constants.MANIFEST_ENDPOINT,
                    Constants.REGISTRY_SUPPORTED_PROTOCOL_VERSIONS);
            }

            if (settings.RunningMode == SupportedNAMEBehaviours.NAMEDisabled)
            {
                LogInfo("Stopping the NAME self host server since it is disabled in the Registry.", configuration.LogHealthCheckToConsole);
                server.Dispose();
                return(dependencies);
            }

            return(dependencies);
        }
示例#4
0
        /// <summary>
        /// Sets up NAME.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>Returns the <see cref="ParsedDependencies"/>.</returns>
        public static ParsedDependencies UseNAME(
            this IApplicationBuilder app,
            NAMEAspNetCoreConfiguration config)
        {
            IConnectionStringProvider connectionStringProviderOverride(IJsonNode node)
            {
                if (node["locator"]?.Value == null)
                {
                    return(null);
                }

                if (!node["locator"].Value.Equals("IConfiguration", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }

                if (config.Configuration == null)
                {
                    throw new NAMEException("To use the 'IConfiguration' locator you must add the IConfiguration in the configuration.", NAMEStatusLevel.Warn);
                }

                var key = node["key"]?.Value;

                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentException("key", "The key must be specified with the 'IConfiguration' locator.");
                }

                return(new AspNetCoreConfigurationConnectionStringProvider(config.Configuration, key));
            }

            var env            = app.ApplicationServices.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment;
            var filePathMapper = new AspNetCoreFilePathMapper(env);
            var dependencies   = ReadAndLogDependencies(config, false, filePathMapper, out NAMESettings settings, connectionStringProviderOverride);

            if (settings.RunningMode == SupportedNAMEBehaviours.NAMEDisabled)
            {
                LogInfo("Not starting NAME since it is disabled in the dependencies file.", false);
                return(dependencies);
            }

            if (settings.RunningMode < SupportedNAMEBehaviours.BootstrapDisabled)
            {
                var register = new Registration.Register();

                //TODO: NAME-32

                register.RegisterInstance(
                    filePathMapper,
                    config.APIName,
                    config.APIVersion,
                    config.DependenciesFilePath,
                    settings,
                    Environment.MachineName,
                    null,
                    typeof(ApplicationBuilderExtensions).GetTypeInfo().Assembly.GetName().Version.ToString(),
                    (config.ManifestUriPrefix?.TrimEnd('/') ?? string.Empty) + Constants.MANIFEST_ENDPOINT,
                    Constants.REGISTRY_SUPPORTED_PROTOCOL_VERSIONS);
            }

            if (settings.RunningMode == SupportedNAMEBehaviours.NAMEDisabled)
            {
                LogInfo("Not starting NAME since it is disabled in the Registry.", false);
                return(dependencies);
            }

            app.UseMiddleware <NAMEMiddleware>(config, settings, filePathMapper);

            return(dependencies);
        }