// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <RabbitMQSettings>(options => Configuration.GetSection("RabbitMQSettings").Bind(options));
            services.Configure <BlockchainSettings>(options => Configuration.GetSection("BlockchainSettings").Bind(options));

            //services.Configure<RawRabbitConfiguration>(options => Configuration.GetSection("RawRabbitConfiguration").Bind(options));

            // add the configuration object
            services.AddSingleton <IConfiguration>(Configuration);

            IServiceProvider sp            = services.BuildServiceProvider();
            ILoggerFactory   loggerFactory = sp.GetRequiredService <ILoggerFactory>();

            //loggerFactory.AddDebug();
            loggerFactory.AddSerilog();
            //loggerFactory.AddFile("Logs/FDBC_Main-{Date}.txt", isJson: false);

            ILogger <Web3GethService> web3_logger = loggerFactory.CreateLogger <Web3GethService>();

            //Web3GethService = new Web3GethService(Configuration);
            Web3GethService = new Web3GethService(Configuration, web3_logger);
            services.AddSingleton <IWeb3GethService>(Web3GethService);
            //services.AddSingleton<IWeb3GethService, Web3GethService>((ctx) =>
            //{
            //  ILoggerFactory factory = ctx.GetRequiredService<ILoggerFactory>();
            //  ILogger<Web3GethService> logger = factory.CreateLogger<Web3GethService>();
            //  return new Web3GethService(Configuration, logger);
            //});
            //services.AddSingleton<Web3GethService>();

            ILogger <EasyNetQService> rabbitmq_logger = loggerFactory.CreateLogger <EasyNetQService>();

            //services.AddRawRabbit();
            //services.AddSingleton(new RawRabbitService(Configuration));

            services.AddSingleton(new EasyNetQService(Configuration, Web3GethService, rabbitmq_logger));
            //services.AddSingleton<EasyNetQService>((ctx) =>
            //{
            //  IWeb3GethService web3geth_service = ctx.GetRequiredService<IWeb3GethService>();
            //  return new EasyNetQService(Configuration, web3geth_service);
            //});
            //services.AddSingleton<EasyNetQService>();
            //services.AddSingleton<EasyNetQService>((ctx) => {
            //  IWeb3GethService web3geth_svc = ctx.GetRequiredService<IWeb3GethService>();
            //  return new EasyNetQService(Configuration, web3geth_svc);
            //});

            // Add framework services.
            //services.AddMvc();
        }
Example #2
0
        public EasyNetQService(IConfiguration configuration, IWeb3GethService web3geth_service, ILogger <EasyNetQService> logger)
        //public EasyNetQService(IConfiguration configuration, IWeb3GethService web3geth_service)
        {
            _rabbitmq_settings   = configuration.GetSection("RabbitMQSettings").Get <RabbitMQSettings>();
            _blockchain_settings = configuration.GetSection("BlockchainSettings").Get <BlockchainSettings>();

            //_bus = RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQConnectionString"));

            //_bus = RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQConnectionString"),
            //  serviceRegister => serviceRegister.Register<IConsumerErrorStrategy, DeadLetterStrategy>());

            _bus = RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQConnectionString"),
                                         serviceRegister => serviceRegister.Register <IConsumerErrorStrategy, AlwaysRequeueErrorStrategy>());

            //_bus.Receive<string>(queue: QueueNameFormatting("intermediate2blockchain"), onMessage: message => TestString(message));

            //_advanced_client = RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQConnectionString")).Advanced;

            _queue_intermediate2blockchain = _bus.Advanced.QueueDeclare(name: QueueNameFormatting("intermediate2blockchain"));

            _queue_blockchain2intermdiate = _bus.Advanced.QueueDeclare(name: QueueNameFormatting("blockchain2intermediate"));

            _web3geth_service = web3geth_service;

            _invlid_block_hash = _web3geth_service.BlockchainManager.INVALID_BLOCK_HASH;

            _logger = logger;

            var ASPNETCORE_ENVIRONMENT = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            _logger.LogInformation("ASPNETCORE_ENVIRONMENT = {ASPNETCORE_ENVIRONMENT}", ASPNETCORE_ENVIRONMENT);

            _logger.LogDebug("Initialized: EasyNetQService");

            // TEST Flight.Create
            foreach (var i in Enumerable.Range(0, 20))
            {
                var request = new I2B_Request
                {
                    task_uuid = $"task_uuid_{i}",
                    task      = new I2B_Request_Task()
                    {
                        name    = "createNewBlockchainFlight",
                        payload = "{\"pid\":28,\"ufid\":72,\"flight_code\":\"CX564\",\"fs_flight_code\":\"CX564\",\"departure_airport\":\"HKG\",\"arrival_airport\":\"TPE\",\"departure_utc_offset_hours\":8,\"arrival_utc_offset_hours\":8,\"scheduled_departure_date\":20170719,\"scheduled_departure_date_time\":\"2017-07-19T05:10:00Z\",\"scheduled_departure_date_time_local\":\"2017-07-19T13:10:00+08:00\",\"scheduled_arrival_date_time\":\"2017-07-19T07:10:00Z\",\"scheduled_arrival_date_time_local\":\"2017-07-19T15:10:00+08:00\",\"hash\":\"0xad037ad2f98401ea9b02b8fa4373e444858836e1acbddf2cea73c126dca40083\"}"
                    }
                };

                SendI2B_Request(request).Wait();
            }

            //// TEST Flight.SetFlightAttribute
            //foreach (var i in Enumerable.Range(0, 1))
            //{
            //  var request = new I2B_Request
            //  {
            //    task_uuid = $"deleteBlockchainFlight_test",
            //    task = new I2B_Request_Task()
            //    {
            //      name = "deleteBlockchainFlight",
            //      payload = "{\"flight_id\":80,\"pid\":29,\"ufid\":79,\"flight_code\":\"CI172\",\"fs_flight_code\":\"CI172\",\"departure_utc_offset_hours\":8,\"arrival_utc_offset_hours\":9,\"departure_airport\":\"TPE\",\"arrival_airport\":\"KIX\",\"status\":null,\"scheduled_departure_date\":\"20170720\",\"scheduled_departure_date_time\":\"2017-07-20T06:20:00.000Z\",\"scheduled_departure_date_time_local\":\"2017-07-20T14:20:00+08:00\",\"actual_departure_date_time\":null,\"actual_departure_date_time_local\":null,\"scheduled_arrival_date_time\":\"2017-07-20T09:05:00.000Z\",\"scheduled_arrival_date_time_local\":\"2017-07-20T18:05:00+09:00\",\"actual_arrival_date_time\":null,\"actual_arrival_date_time_local\":null,\"cancel_date_time\":null,\"cancel_date_time_local\":null,\"hash\":\"0xde52a4b7fe8b7d69c68c37ce60639b989e928a457dace6dda065569b4138060b\",\"contract_address\":\"0x4f82d90edeecf2abc5880e8c41aa288312ce2981\",\"flight_status_source\":null,\"flight_status_fed\":false,\"flight_status_confirmed_txhash\":null,\"delay_notification_date_time\":null,\"deleted\":false,\"creation_txhash\":null,\"created_at\":\"2017-07-20T05:37:02.000Z\",\"version\":0}"
            //    }
            //  };

            //  DeleteFlight delete_flight = JsonConvert.DeserializeObject<DeleteFlight>(request.task.payload);

            //  Tuple<string, string> result = _web3geth_service.Flight.SetFlightAllAttributes(
            //    contract_address: delete_flight.contract_address,
            //    task_uuid: request.task_uuid,
            //    status: delete_flight.status,
            //    actual_departure_date_time: delete_flight.actual_departure_date_time,
            //    actual_departure_date_time_local: delete_flight.actual_departure_date_time_local,
            //    actual_arrival_date_time: delete_flight.actual_arrival_date_time,
            //    actual_arrival_date_time_local: delete_flight.actual_arrival_date_time_local,
            //    cancel_date_time: delete_flight.cancel_date_time,
            //    cancel_date_time_local: delete_flight.cancel_date_time_local,
            //    deleted: delete_flight.deleted.ToString(),
            //    flight_status_source: delete_flight.flight_status_source,
            //    flight_status_fed: delete_flight.flight_status_fed.ToString(),
            //    delay_notification_date_time: delete_flight.delay_notification_date_time
            //    ).Result;

            //// TEST Policy.Create
            //foreach (var i in Enumerable.Range(0, 10))
            //{
            //  var request = new I2B_Request
            //  {
            //    task_uuid = $"createNewBlockchainPolicy_test{i}",
            //    task = new I2B_Request_Task()
            //    {
            //      name = "createNewBlockchainPolicy",
            //      payload = "{\"status\":\"activated\",\"deleted\":false,\"version\":0,\"pid\":31,\"psn\":\"000033\",\"tenant_id\":1,\"start_date_time\":\"2017-07-20T08:00:00.000Z\",\"end_date_time\":\"2017-07-21T08:00:00.000Z\",\"start_date_time_local\":\"201707200800\",\"end_date_time_local\":\"201707210800\",\"created_at\":\"2017-07-20T05:37:51.601Z\"}"
            //    }
            //  };

            //  SendI2B_Request(request).Wait();
            //}

            ////TODO contract_address "0xdec46ce3bc57e48fa986173e223c176a26336922"
            //// TEST Policy.SetFlightAttribute
            //foreach (var i in Enumerable.Range(0, 1))
            //{
            //  var request = new I2B_Request
            //  {
            //    task_uuid = $"deleteBlockchainFlight_test",
            //    task = new I2B_Request_Task()
            //    {
            //      name = "deleteBlockchainFlight",
            //      payload = "{\"pid\":23,\"psn\":\"000024\",\"tenant_id\":1,\"start_date_time\":\"2017-07-19T08:00:00.000Z\",\"end_date_time\":\"2017-07-20T08:00:00.000Z\",\"start_date_time_local\":\"201707190800\",\"end_date_time_local\":\"201707200800\",\"status\":\"activated\",\"contract_address\":\"0xdec46ce3bc57e48fa986173e223c176a26336922\",\"deleted\":false,\"creation_txhash\":null,\"created_at\":\"2017-07-19T07:12:42.000Z\",\"version\":0}"
            //    }
            //  };

            //  //DeleteBlockchainPolicy(request).Wait();

            //  SendI2B_Request(request).Wait();
            //}

            // 需要 Node.js 那邊配合在 Message Propertoes 加入 type: FDBC_Shared.DTO.I2B_Request:FDBC_Shared
            // 目前測試過 PrefetchCount = 1 可以穩定處理所有message, N大於1都會在最後一批有 noacked N-1 卡住, 要關掉 blockchain module 才會 release
            ushort prefetch_count = 1;

            _bus.Advanced.Consume <I2B_Request>(
                _queue_intermediate2blockchain,
                (msg, info) => OnReceiving_I2B_Request(msg, info),
                configure => configure.WithPrefetchCount(prefetch_count)
                );

            //_client.Receive<I2B_Request>(queue: QueueNameFormatting("intermediate2blockchain"), onMessage: message =>
            //  EasyNetQActions.OnReceiving_I2B_Request(message)
            //);
        }