public async Task StartAsync(CancellationToken cancellationToken)
        {
            var loggerRedirect = new NuGetRedirectLogger(loggerFactory.CreateLogger("NuGet"));

            string url = "https://unitynuget-registry.azurewebsites.net/";

            bool isDevelopment = hostEnvironment.IsDevelopment();

            if (isDevelopment)
            {
                var urls = configuration[WebHostDefaults.ServerUrlsKey];

                // Select HTTPS in production, HTTP in development
                url = urls.Split(';').FirstOrDefault(x => !x.StartsWith("https"));
                if (url == null)
                {
                    throw new InvalidOperationException($"Unable to find a proper server URL from `{urls}`. Expecting a `http://...` URL in development");
                }
            }

            // Get the current directory /home/site/unity_packages or binary folder in dev
            var currentDirectory   = isDevelopment ? Path.GetDirectoryName(typeof(Startup).Assembly.Location) : Directory.GetCurrentDirectory();
            var unityPackageFolder = Path.Combine(currentDirectory, "unity_packages");

            loggerRedirect.LogInformation($"Using Unity Package folder `{unityPackageFolder}`");

            // Build the cache synchronously because ConfigureServices doesn't support async Task
            var initialCache = new RegistryCache(unityPackageFolder, url, loggerRedirect);
            await initialCache.Build();

            // Add the cache accessible from the services
            registryCacheSingleton.Instance = initialCache;
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            var loggerRedirect = new NuGetRedirectLogger(loggerFactory.CreateLogger("NuGet"));

            Uri uri = registryOptions.RootHttpUrl !;

            bool isDevelopment = hostEnvironment.IsDevelopment();

            if (isDevelopment)
            {
                var urls = configuration[WebHostDefaults.ServerUrlsKey];

                // Select HTTPS in production, HTTP in development
                var url = urls.Split(';').FirstOrDefault(x => !x.StartsWith("https"));
                if (url == null)
                {
                    throw new InvalidOperationException($"Unable to find a proper server URL from `{urls}`. Expecting a `http://...` URL in development");
                }

                // https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#dotnet_running_in_container-and-dotnet_running_in_containers
                bool runningInContainer = configuration.GetValue <bool>("DOTNET_RUNNING_IN_CONTAINER");

                uri = new Uri(runningInContainer ? url.Replace("+", "localhost") : url);
            }

            // Get the current directory from registry options (prepend binary folder in dev)
            string unityPackageFolder;

            if (isDevelopment)
            {
                var currentDirectory = Path.GetDirectoryName(typeof(Startup).Assembly.Location) !;
                unityPackageFolder = Path.Combine(currentDirectory, new DirectoryInfo(registryOptions.RootPersistentFolder !).Name);
            }
            else
            {
                if (Path.IsPathRooted(registryOptions.RootPersistentFolder))
                {
                    unityPackageFolder = registryOptions.RootPersistentFolder;
                }
                else
                {
                    unityPackageFolder = Path.Combine(Directory.GetCurrentDirectory(), registryOptions.RootPersistentFolder !);
                }
            }
            loggerRedirect.LogInformation($"Using Unity Package folder `{unityPackageFolder}`");

            // Add the cache accessible from the services
            registryCacheSingleton.UnityPackageFolder  = unityPackageFolder;
            registryCacheSingleton.ServerUri           = uri;
            registryCacheSingleton.NuGetRedirectLogger = loggerRedirect;

            return(Task.CompletedTask);
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            var loggerRedirect = new NuGetRedirectLogger(loggerFactory.CreateLogger("NuGet"));

            Uri uri = registryOptions.RootHttpUrl;

            bool isDevelopment = hostEnvironment.IsDevelopment();

            if (isDevelopment)
            {
                var urls = configuration[WebHostDefaults.ServerUrlsKey];

                // Select HTTPS in production, HTTP in development
                var url = urls.Split(';').FirstOrDefault(x => !x.StartsWith("https"));
                if (url == null)
                {
                    throw new InvalidOperationException($"Unable to find a proper server URL from `{urls}`. Expecting a `http://...` URL in development");
                }

                uri = new Uri(url);
            }

            // Get the current directory from registry options (prepend binary folder in dev)
            string unityPackageFolder;

            if (isDevelopment)
            {
                var currentDirectory = Path.GetDirectoryName(typeof(Startup).Assembly.Location);
                unityPackageFolder = Path.Combine(currentDirectory, new DirectoryInfo(registryOptions.RootPersistentFolder).Name);
            }
            else
            {
                if (Path.IsPathRooted(registryOptions.RootPersistentFolder))
                {
                    unityPackageFolder = registryOptions.RootPersistentFolder;
                }
                else
                {
                    unityPackageFolder = Path.Combine(Directory.GetCurrentDirectory(), registryOptions.RootPersistentFolder);
                }
            }
            loggerRedirect.LogInformation($"Using Unity Package folder `{unityPackageFolder}`");

            // Add the cache accessible from the services
            registryCacheSingleton.UnityPackageFolder  = unityPackageFolder;
            registryCacheSingleton.ServerUri           = uri;
            registryCacheSingleton.NuGetRedirectLogger = loggerRedirect;

            return(Task.CompletedTask);
        }
Exemple #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var loggerRedirect = new NuGetRedirectLogger(LoggerFactory.CreateLogger("NuGet"));

            string url = "https://unitynuget-registry.azurewebsites.net/";

            bool isDevelopment = HostingEnvironment.IsDevelopment();

            if (isDevelopment)
            {
                var urls = Configuration[WebHostDefaults.ServerUrlsKey];

                // Select HTTPS in production, HTTP in development
                url = urls.Split(';').FirstOrDefault(x => !x.StartsWith("https"));
                if (url == null)
                {
                    throw new InvalidOperationException($"Unable to find a proper server URL from `{urls}`. Expecting a `http://...` URL in development");
                }
            }

            // Get the current directory /home/site/unity_packages or binary folder in dev
            var currentDirectory   = isDevelopment ? Path.GetDirectoryName(typeof(Startup).Assembly.Location) : Directory.GetCurrentDirectory();
            var unityPackageFolder = Path.Combine(currentDirectory, "unity_packages");

            loggerRedirect.LogInformation($"Using Unity Package folder `{unityPackageFolder}`");

            // Build the cache synchronously because ConfigureServices doesn't support async Task
            var initialCache = new RegistryCache(unityPackageFolder, url, loggerRedirect);

            initialCache.Build().GetAwaiter().GetResult();

            // Add the cache accessible from the services
            var singletonCache = new RegistryCacheSingleton(initialCache);

            services.AddSingleton(singletonCache);

            // Add the registry cache updater
            services.AddHostedService <RegistryCacheUpdater>();

            // Also enable NewtonsoftJson serialization
            services.AddControllers().AddNewtonsoftJson();
        }