コード例 #1
0
        /// <summary>
        /// Enables the NAME self host server.
        /// </summary>
        /// <param name="nameConfigBuilder">The name configuration builder.</param>
        /// <returns>
        /// Returns an <see cref="IDisposable" /> proxy object containing the Parsed Dependencies and a way to stop the server.
        /// </returns>
        /// <exception cref="NAMEException">Error parsing the dependencies file.</exception>
        /// <exception cref="DependenciesCheckException">Happens when <see cref="NAMEBaseConfiguration.ThrowOnDependenciesFail" /> is set to true and dependencies check fail.</exception>
        public static SelfHostResult EnableName(Action <NAMEHttpListenerConfiguration> nameConfigBuilder)
        {
            var nameConfiguration = new NAMEHttpListenerConfiguration();

            nameConfigBuilder?.Invoke(nameConfiguration);

            var pathMapper = new StaticFilePathMapper();

            var server             = new HttpListenerServer(nameConfiguration, pathMapper);
            var parsedDependencies = SelfHostInitializer.Initialize(server, pathMapper, nameConfiguration);

            return(new SelfHostResult(parsedDependencies, server));
        }
コード例 #2
0
        private async Task GetManifest(HttpListenerContext context, NAMEHttpListenerConfiguration configuration)
        {
            context.Response.ContentType = "application/json";

            int currentHopCount = 0;
            var hopCountHeaders = context.Request.Headers.GetValues(Constants.HOP_COUNT_HEADER_NAME);

            if (hopCountHeaders?.Length > 0)
            {
                if (!int.TryParse(hopCountHeaders.First(), out currentHopCount))
                {
                    currentHopCount = 0;
                    LogWarning($"The received hop count header it not a valid int value ({hopCountHeaders.First()}), defaulting to 0.", this.nameConfiguration.LogHealthCheckToConsole);
                }
            }

            currentHopCount++;

            var nameContext = new NAMEContext()
            {
                ServiceDependencyCurrentNumberOfHops = currentHopCount
            };

            var dependencies = DependenciesReader.ReadDependencies(configuration.DependenciesFilePath, this.pathMapper, this.settings, nameContext);

            if (currentHopCount == this.settings.ServiceDependencyMaxHops)
            {
                context.Response.StatusCode = Constants.SERVICE_HOPS_ERROR_STATUS_CODE;
                return;
            }

            string manifest = await ManifestGenerator.GenerateJson(configuration.APIName, configuration.APIVersion, dependencies).ConfigureAwait(false);

            context.Response.ContentType = "application/json";

            var manifestBytes = Encoding.UTF8.GetBytes(manifest);

            context.Response.OutputStream.Write(manifestBytes, 0, manifestBytes.Length);
        }
コード例 #3
0
 public HttpListenerServer(NAMEHttpListenerConfiguration nameConfiguration, IFilePathMapper pathMapper)
 {
     this.pathMapper        = pathMapper;
     this.nameConfiguration = nameConfiguration;
 }