Exemplo n.º 1
0
 private static IHostBuilder CreateHostBuilder(string[] args) =>
 Host.CreateDefaultBuilder(args)
 .ConfigureWebHostDefaults(webBuilder =>
 {
     webBuilder.UseUrls(new string[] { "http://*:5000/", "https://*:5001/" });
     webBuilder.UseStartup <Startup>();
 })
 .ConfigureHostConfiguration(configHost => {
     configHost.SetBasePath(Directory.GetCurrentDirectory());
     configHost.AddEnvironmentVariables();
 })
 .ConfigureServices((hostContext, services) => {
     IConfiguration configuration      = hostContext.Configuration;
     ExchangeSettings exchangeSettings = configuration.GetSection("ExchangeSettings").Get <ExchangeSettings>();
     //if (!string.IsNullOrEmpty(configuration["COINBASE_API_KEY"]) && !string.IsNullOrEmpty(configuration["COINBASE_API_PASSPHRASE"]) && !string.IsNullOrEmpty(configuration["COINBASE_API_SECRET"]))
     //{
     //    exchangeSettings.APIKey = configuration["COINBASE_API_KEY"];
     //    exchangeSettings.PassPhrase = configuration["COINBASE_API_PASSPHRASE"];
     //    exchangeSettings.Secret = configuration["COINBASE_API_SECRET"];
     //}
     services.AddSingleton <IExchangeSettings>(exchangeSettings);
     //cross origin requests
     services.AddCors(options => options.AddPolicy(name: Startup.AllowSpecificOrigins, builder => {
         builder.WithOrigins($"http://*:9000/")
         .AllowAnyHeader().AllowAnyMethod().AllowCredentials();
     }));
     services.AddHttpClient <IConnectionAdapter, ConnectionAdapter>(httpClient =>
     {
         if (exchangeSettings.Uri != null)
         {
             httpClient.BaseAddress = new Uri(exchangeSettings.EndpointUrl);
         }
     });
     services.AddSignalR();
     //services.AddSingleton<IExchangeService, Coinbase>();
     services.AddSingleton <IExchangeService, Binance>();
     services.AddHostedService <Worker>();
 })
 .ConfigureAppConfiguration((hostContext, configApp) =>
 {
     string f = Directory.GetCurrentDirectory();
     configApp.SetBasePath(Directory.GetCurrentDirectory());
     configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
     configApp.AddCommandLine(args);
 })
 .ConfigureLogging((context, logging) => {
     //change configuration for logging
     //clearing out everyone listening to the logging event
     logging.ClearProviders();
     //add configuration with appsettings.json
     logging.AddConfiguration(context.Configuration.GetSection("Logging"));
     //add loggers (write to)
     logging.AddDebug();
     logging.AddConsole();
 });
Exemplo n.º 2
0
        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            return(Host.CreateDefaultBuilder(args)
                   .ConfigureLogging(loggingConfiguration => loggingConfiguration.ClearProviders())
                   .UseSerilog()
                   .ConfigureWebHostDefaults(webBuilder =>
            {
                //Create a Generic Host - HTTP workload
                //Read Configuration from appSettings
                string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
                if (string.IsNullOrEmpty(environment))
                {
                    environment = DefaultEnvironmentName;
                }
                IConfigurationRoot config = new ConfigurationBuilder()
                                            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                            .AddJsonFile($"appsettings.{environment}.json", true)
                                            .Build();
                string[] hostUrls = config.GetSection("HostUrls").Get <string[]>();
                Log.Information($"Host Url: {string.Join(",", hostUrls)}");
                webBuilder.UseUrls(hostUrls);
                webBuilder.UseStartup <Startup>();
            })
                   .ConfigureHostConfiguration(configHost =>
            {
                //The host configuration is also added to the app configuration.
                configHost.SetBasePath(Directory.GetCurrentDirectory());
                configHost.AddEnvironmentVariables();
            })
                   .ConfigureAppConfiguration((hostContext, configApp) =>
            {
                string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
                if (string.IsNullOrEmpty(environment))
                {
                    environment = DefaultEnvironmentName;
                }
                configApp.AddJsonFile($"appsettings.{environment}.json", true);
                configApp.AddCommandLine(args);
            })
                   .ConfigureServices((hostContext, services) =>
            {
                //Creates a Generic Host using non-HTTP workload.
                //The IHostedService implementation is added to the DI container
                IConfiguration configuration = hostContext.Configuration;
                hostContext.HostingEnvironment.ApplicationName = "Exchange.Service";
                string[] allowedOrigins = configuration.GetSection("AllowedOrigins").Get <string[]>();
                if (allowedOrigins != null)
                {
                    Log.Information($"Allowed Origins: {string.Join(",", allowedOrigins)}");
                    ExchangeSettings exchangeSettings =
                        configuration.GetSection("ExchangeSettings").Get <ExchangeSettings>();
                    if (exchangeSettings != null)
                    {
                        services.AddSingleton <IExchangeSettings>(exchangeSettings);
                    }
                    //cross origin requests
                    services.AddCors(options => options.AddPolicy(Startup.AllowSpecificOrigins, builder =>
                    {
                        builder.WithOrigins(allowedOrigins)
                        .AllowAnyHeader().AllowAnyMethod().AllowCredentials();
                    }));
                }
                else
                {
                    Log.Information($"Service CORS Origins: N/A");
                }

                services.AddSignalR();
                string directoryName = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
                if (string.IsNullOrEmpty(directoryName))
                {
                    return;
                }
                string pluginDirectory = Path.Combine(directoryName, "plugin");
                if (!Directory.Exists(pluginDirectory))
                {
                    Directory.CreateDirectory(pluginDirectory);
                }
                ExchangePluginService exchangePluginService = new ExchangePluginService();
                exchangePluginService.AddPluginFromFolder(pluginDirectory);
                services.AddSingleton <IExchangePluginService>(exchangePluginService);
                services.AddSingleton <IExchangeService, ExchangeService>();
                services.AddHostedService <Worker>();
            }));
        }