Esempio n. 1
0
        private static async Task Main()
        {
            const string culture = "en-US";

            var host = new HostBuilder()
                       .ConfigureAppConfiguration((hostContext, configApp) =>
            {
                configApp.SetBasePath(Directory.GetCurrentDirectory());
                configApp.AddJsonFile(ConfigurationConstants.ClusterServerPath, optional: false);
                configApp.AddJsonFile(ConfigurationConstants.DatabasePath, optional: false);
            })
                       .ConfigureServices((hostContext, services) =>
            {
                services.AddOptions();
                services.AddMemoryCache();

                services.Configure <ClusterConfiguration>(hostContext.Configuration.GetSection(ConfigurationConstants.ClusterServer));
                services.Configure <CoreConfiguration>(hostContext.Configuration.GetSection(ConfigurationConstants.CoreServer));

                services.AddDatabase(hostContext.Configuration);
                services.AddHandlers();
                services.AddGameResources();

                // Core client configuration
                services.AddSingleton <IClusterCoreClient, ClusterCoreClient>();
                services.AddSingleton <ICorePacketFactory, CorePacketFactory>();
                services.AddSingleton <IHostedService, ClusterCoreClientService>();

                // Cluster server configuration
                services.AddSingleton <IClusterServer, ClusterServer>();
                services.AddSingleton <IClusterPacketFactory, ClusterPacketFactory>();
                services.AddSingleton <IHostedService, ClusterServerService>();
            })
                       .ConfigureLogging(builder =>
            {
                builder.AddFilter("Microsoft", LogLevel.Warning);
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddNLog(new NLogProviderOptions
                {
                    CaptureMessageTemplates  = true,
                    CaptureMessageProperties = true
                });
            })
                       .SetConsoleCulture(culture)
                       .UseConsoleLifetime()
                       .Build();

            await host
            .AddHandlerParameterTransformer <INetPacketStream, IPacketDeserializer>((source, dest) =>
            {
                dest?.Deserialize(source);
                return(dest);
            })
            .RunAsync();
        }