public void Throws_MissingArgumentCraneException_when_required_parameter_not_given(CommandArgParser commandArgParser, Exception result)
        {
            "Given I have a commandArgParser"
            ._(() => commandArgParser = new CommandArgParser());

            "When I dont pass in the Bar parameter"
            ._(() => result = Throws.Exception(() => commandArgParser.Parse(typeof(FooCommand), "FooCommand")));

            "Then a MissingArgumentCraneException is thrown"
            ._(() => result.Should().BeOfType <MissingArgumentCraneException>());
        }
        public void Parses_all_args_when_all_passed_by_switch(CommandArgParser commandArgParser, FooCommand result)
        {
            "Given I have a commandArgParser"
            ._(() => commandArgParser = new CommandArgParser());

            "When I parse the command 'FooCommand -Bar Hello -Baz World'"
            ._(() => result = commandArgParser.Parse(typeof(FooCommand), "FooCommand", "-Bar", "Hello", "-Baz", "World") as FooCommand);

            "Then the result should have Bar set to Hello"
            ._(() => result.Bar.Should().Be("Hello"));

            "And the result should have Baz set to World"
            ._(() => result.Baz.Should().Be("World"));
        }
        public void Parses_args_when_passed_by_position(CommandArgParser commandArgParser, FooCommand result)
        {
            "Given I have a commandArgParser"
            ._(() => commandArgParser = new CommandArgParser());

            "When I parse the command 'FooCommand Hello'"
            ._(() => result = commandArgParser.Parse(typeof(FooCommand), new[] { "FooCommand", "Hello" }) as FooCommand);

            "Then the result should have Bar set to Hello"
            ._(() => result.Bar.Should().Be("Hello"));

            "And the result should not have Baz set"
            ._(() => result.Baz.Should().BeNull());
        }
Exemple #4
0
        static void Main(string[] args)
        {
            if (args.Length > 1 && args[0].EndsWith(".pfx"))
            {
                var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(args[0], args[1]);
                Console.WriteLine(cert.GetCertHashString());
                return;
            }
            //固定当前工作目录
            System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

            var builder = new ConfigurationBuilder();

            builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            var configuration = builder.Build();

            var port = configuration.GetValue <int>("Port");
            CommandArgParser cmdArg = new CommandArgParser(args);

            port = cmdArg.TryGetValue <int>("port", port);

            var datafolder = configuration.GetValue <string>("DataFolder");

            if (!System.IO.Directory.Exists(datafolder))
            {
                System.IO.Directory.CreateDirectory(datafolder);
            }
            datafolder = cmdArg.TryGetValue <string>("DataFolder", datafolder);

            ServiceCollection services = new ServiceCollection();

            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
                loggingBuilder.AddConsole(); // 将日志输出到控制台
            });
            services.AddSingleton <IConfiguration>(configuration);
            services.AddSingleton <GatewayRefereeClient>();
            services.AddSingleton <IRequestReception, RequestReception>();
            services.AddSingleton <ICommandHandlerManager, CommandHandlerManager>();
            services.AddSingleton <Gateway>();
            services.AddSingleton <LockKeyManager>();
            services.AddTransient <IMicroServiceReception, MicroServiceReception>();
            services.AddSingleton <TransactionIdBuilder>();
            services.AddSingleton <FileChangeWatcher>();
            services.AddTransient <ListenFileChangeReception>();

            var assembly = Assembly.Load(configuration.GetValue <string>("ServiceProviderAllocator:Assembly"));
            var serviceProviderAllocatorType = assembly.GetType(configuration.GetValue <string>("ServiceProviderAllocator:FullName"));
            var serviceProviderAllocator     = (IServiceProviderAllocator)Activator.CreateInstance(serviceProviderAllocatorType);


            services.AddSingleton <IServiceProviderAllocator>(serviceProviderAllocator);
            var serviceProvider = services.BuildServiceProvider();

            serviceProvider.GetService <LockKeyManager>();
            serviceProvider.GetService <FileChangeWatcher>();

            //启动GatewayRefereeClient,申请成为主网关
            serviceProvider.GetService <GatewayRefereeClient>();

            var gateway = serviceProvider.GetService <Gateway>();

            //SSL
            var certPath = configuration.GetValue <string>("SSL:Cert");

            if (!string.IsNullOrEmpty(certPath))
            {
                gateway.ServerCert     = new System.Security.Cryptography.X509Certificates.X509Certificate2(certPath, configuration.GetValue <string>("SSL:Password"));
                gateway.AcceptCertHash = configuration.GetSection("SSL:AcceptCertHash").Get <string[]>();
            }

            gateway.ServiceProvider = serviceProvider;
            gateway.Run(port);
        }