Exemplo n.º 1
0
        private void ToggleOption(DropdownMenuAction action)
        {
            ExampleOptions option;

            switch (action.name)
            {
            case "Koala":
                option = ExampleOptions.Koala;
                break;

            case "Kangaroo":
                option = ExampleOptions.Kangaroo;
                break;

            case "Platypus":
                option = ExampleOptions.Platypus;
                break;

            case "Wombat":
                option = ExampleOptions.Wombat;
                break;

            default:
                option = ExampleOptions.None;
                break;
            }

            options ^= option;
        }
Exemplo n.º 2
0
 private static void RunExample(ExampleOptions options)
 {
     using (var messageSerializer = new SingleFileJSONMessageSerializer(Console.OpenStandardOutput(), false, new UTF8Encoding(false)))
     {
         messageSerializer.Serialize(new Message
         {
             Exchange    = "example",
             Queue       = "example.queue",
             RoutingKey  = "example.routing.key",
             DeliveryTag = 42,
             Properties  = new MockBasicProperties
             {
                 ContentType  = "application/json",
                 DeliveryMode = 2,
                 Headers      = new Dictionary <string, object>
                 {
                     { "classType", Encoding.UTF8.GetBytes("Tapeti.Cmd.Example:Tapeti.Cmd") }
                 },
                 ReplyTo   = "reply.queue",
                 Timestamp = new AmqpTimestamp(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds())
             },
             Body = Encoding.UTF8.GetBytes("{ \"Hello\": \"world!\" }")
         });
     }
 }
 public OtherValuesController(
     IExampleService service,
     IOptions <ExampleOptions> options
     )
 {
     _service = service;
     _options = options.Value;
 }
Exemplo n.º 4
0
        private static int ShowExample(ExampleOptions options)
        {
            var assembly          = Assembly.GetExecutingAssembly();
            var exampleScriptName = assembly.GetManifestResourceNames()
                                    .First(x => x.Contains("HelloWorld.cs"));

            using (var stream = assembly.GetManifestResourceStream(exampleScriptName))
                using (var reader = new StreamReader(stream))
                {
                    var exampleScript = reader.ReadToEnd();
                    Console.WriteLine(exampleScript);
                }

            return((int)ExitCode.Success);
        }
Exemplo n.º 5
0
        private int HandleExampleOptions(ExampleOptions exampleOptions)
        {
            Reporter.ToLog(eLogLevel.DEBUG, "Running example options");

            switch (exampleOptions.verb)
            {
            case "all":
                ShowExamples();
                break;

            case "run":
                ShowExamples();
                break;

            default:
                Reporter.ToLog(eLogLevel.ERROR, "Unknown verb '" + exampleOptions.verb + "' ");
                return(1);
            }

            return(0);
        }
Exemplo n.º 6
0
        private async Task <int> HandleExampleOptions(ExampleOptions exampleOptions)
        {
            return(await Task.Run(() =>
            {
                Reporter.ToLog(eLogLevel.DEBUG, "Running example options");

                switch (exampleOptions.verb)
                {
                case "all":
                    ShowExamples();
                    break;

                case "run":
                    ShowExamples();
                    break;

                default:
                    Reporter.ToLog(eLogLevel.ERROR, "Unknown verb '" + exampleOptions.verb + "' ");
                    return 1;
                }

                return 0;
            }));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 根据命令行参数打印示例。
        /// </summary>
        /// <param name="options">命令行参数</param>
        public static void Print(ExampleOptions options)
        {
            var    uploaders   = NamedInterfaceLoader.Load(typeof(IUploader));
            var    dataSources = NamedInterfaceLoader.Load(typeof(IDataSource));
            string content     = string.Empty;

            if (string.IsNullOrEmpty(options.List) == false)
            {
                if (options.List == "datasource")
                {
                    content = string.Join('\n', dataSources.Keys);
                    Console.WriteLine("已支持以下数据源:");
                }
                else if (options.List == "uploader")
                {
                    content = string.Join('\n', uploaders.Keys);
                    Console.WriteLine("已支持以下上传类:");
                }
                else
                {
                    Console.WriteLine("只能列出 datasource 或 uploader");
                }
            }
            else if (string.IsNullOrEmpty(options.Uploader) == false)
            {
                if (uploaders.ContainsKey(options.Uploader))
                {
                    content = JsonConvert.SerializeObject((Activator.CreateInstance(uploaders[options.Uploader]) as IExampled).GetExample(),
                                                          Formatting.Indented,
                                                          new NameConverter());
                }
                else
                {
                    Console.WriteLine($"不存在uploader类: { options.Uploader }");
                }
            }
            else if (string.IsNullOrEmpty(options.DataSource) == false)
            {
                if (dataSources.ContainsKey(options.DataSource))
                {
                    content = JsonConvert.SerializeObject((Activator.CreateInstance(dataSources[options.DataSource]) as IExampled).GetExample(),
                                                          Formatting.Indented,
                                                          new NameConverter());
                }
                else
                {
                    Console.WriteLine($"不存在datasource类: { options.DataSource }");
                }
            }
            else
            {
                content = GetExampleConfig(uploaders, dataSources);
            }

            if (string.IsNullOrEmpty(content) == false)
            {
                Console.WriteLine(content);
                if (string.IsNullOrEmpty(options.Path) == false)
                {
                    //保存到文件。
                    using (StreamWriter stream = new StreamWriter(options.Path, false, Encoding.UTF8))
                    {
                        stream.Write(content);
                    }

                    Console.WriteLine("\n\n");
                    Log.Info(string.Format("文件已经保存到\"{0}\"。", options.Path));
                }
            }

#if !DEBUG
            Environment.Exit(0);
#endif
        }
Exemplo n.º 8
0
        //В классе Startup могут присутствовать методы вида Configure{EnvironmentName}Services и Configure{EnvironmentName}
        //которые будут вызываться в соответствующих средах
        //если такие методы не будут найдены, то будут использованы простые ConfigureServices и Configure

        /*public void ConfigureDevelopmentServices(IServiceCollection services) { }
         * public void ConfigureDevelopment(IApplicationBuilder app)
         * {
         *  app.Run(context=>context.Response.WriteAsync("<h1>This is Develoment!</h1>"));
         * }*/

        //Настройка конвейера обработки запросов
        //Use this method to configure the HTTP request pipeline
        //  IApplicationBuilder - обязательный параметр для метода Configure
        //  IWebHostEnvironment - НЕ обязательный параметр. Позволяет получить информацию о среде
        //  ILoggerFactory      - НЕ обязательный параметр.
        //  в качестве параметров можно передавать любой сервис, зарегистрированный в методе ConfigureServices
        //Выполняется один раз при создании объекта класса Startup
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IMessageSender sender, ServiceUsingExample senderService, ILogger <Startup> loger1, ILoggerFactory loggerFactory)
        {
            bool useRouteHandler = false;

            #region RouterMiddleware - система маршрутищации из пред. версий
            //Пример настройки роутера
            if (useRouteHandler)
            {
                var routeHandler = new RouteHandler(async context =>
                {
                    //получение данных маршрута
                    RouteData routeData = context.GetRouteData();

                    string s = "";
                    foreach (KeyValuePair <string, object> item in routeData.Values)
                    {
                        s += $" {item.Key}={item.Value};";
                    }

                    //получение данных маршрута по ключу
                    object id = context.GetRouteValue("id");

                    await context.Response.WriteAsync("Router middleware example " + s +
                                                      $" {(id == null ? string.Empty : id)}");
                });

                var routeBuilder = new RouteBuilder(app, routeHandler);

                //пример использования midleware
                routeBuilder.MapMiddlewareGet("middleware/{action}",
                                              middl => { middl.Run(async context => await context.Response.WriteAsync("middleware example")); });

                //пример использования произвольного метода
                routeBuilder.MapVerb("GET", "test/{action}/{id?}",
                                     async(request, response, route) => { await response.WriteAsync("MapVerbExample"); });

                //пример использвания расширения для Post метода
                routeBuilder.MapPost("test/{action}",
                                     async context => { await context.Response.WriteAsync("POST: test/"); });

                //Пример именованого маршрута (отработает routeHandler)
                routeBuilder.MapRoute("default",
                                      @"{controller:regex(^H.*)=home}/{action:alpha:minlength(3)=index}/{id:regex(\d+)?}");
                //routeBuilder.MapRoute("default",
                //    "{controller}/{action}/{id?}",
                //    new { controller = "home", action = "index" },                                            //пример комбинации ограничений
                //    new { httpMethod = new HttpMethodRouteConstraint("GET"), controller = "^H.*", id = @"\d+", action = new CompositeRouteConstraint(new IRouteConstraint[]{
                //        new AlphaRouteConstraint(),
                //        new MinLengthRouteConstraint(3)
                //})});
                //Microsoft.AspNetCore.Routing.Constraints.* - дополнительные ограничния

                app.UseRouter(routeBuilder.Build());

                app.Run(async context => { await context.Response.WriteAsync("Route miss"); });
            }

            #endregion

            app.UsePerformanceTimer();

            //Logging example
            //ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
            //{
            //    //вывод информации на консоль
            //    //builder.AddConsole();
            //    //вывод информации в окне Output
            //    builder.AddDebug();
            //    //логгирование в лог ETW (Event Tracing for Windows)
            //    //builder.AddEventSourceLogger();
            //    //записывает в Windows Event Log
            //    //builder.AddEventLog();
            //});
            //ILogger<ExampleOptions> testLogger = loggerFactory.CreateLogger<ExampleOptions>();

            //loggerFactory.AddFile(Path.Combine(Directory.GetCurrentDirectory(), "log.txt"));
            loggerFactory.AddProvider(new ColorConsoleLoggerProvider(new ColorConsoleLoggerConfiguration
            {
                Color    = ConsoleColor.DarkCyan,
                LogLevel = LogLevel.Warning
            }));

            ILogger fileLogger = loggerFactory.CreateLogger("FileLogger");

            app.Use((context, next) =>
            {
                string path = context.Request.Path;
                string id   = context.Request.Query["id"];

                using (loger1.BeginScope("String format"))
                {
                    loger1.LogTrace(100, "Trace from loger1");
                    loger1.LogDebug("Debug from loger1 {path} id={id}", path, id);
                    loger1.LogInformation("Info from loger1");
                    loger1.LogWarning("Warning from loger1");
                    loger1.LogError(new Exception("Test exception for logger1"), "Error from loger1");
                    loger1.LogCritical(new EventId(123, "eventID_123"), "Critical from loger1");
                }
                //testLogger.LogInformation("Hello from test logger");
                fileLogger.LogInformation("[{time}] Example log", DateTime.Now);

                //performance example
                using (loger1.ScopeExample(123))
                {
                    loger1.WithoutParameters();
                    loger1.OneParameter(100);
                    loger1.TwoParameters("Foo-string", 777);
                    loger1.WithException(333, new Exception("Test exception"));
                }

                return(next());
            });

            //если проиложение в разработке
            if (env.IsDevelopment())
            {
                //выводить специальную страницу с описанием ошибки
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //обработчик ошибок, используется для production окружения
                //можно передать путь к обработчику ошибок
                //!Обработчик не производит редирект, т.е. путь и статус остаются теже
                app.UseExceptionHandler("/errorHandler");

                //Добавляет заголовки HSTS. Настраивается в ConfigureServices
                //app.UseHsts();
            }

            //Производит переадресацию на https. Параметры можно настроить в ConfigureServices
            app.UseHttpsRedirection();

            //Стандартный обработчик для Http ошибок (коды 400-599)
            //Можно заменить выводимое сообщение, вместо {0} будек статусный код
            app.UseStatusCodePages(/*"text/plain", "Code: {0}"*/);
            ////другой обработчик производит переадресацию на другую страницу (302 / Found). Тоже можно передать код ошибки через {0}
            //app.UseStatusCodePagesWithRedirects("/env{0}");
            //еще вариант - рендер ответа по тому же пути
            ////тут указывается путь к обработчику и строка с параметрами, где {0} статусный код
            //app.UseStatusCodePagesWithReExecute("/env", "?id={0}");

            //Объединяет вызов UseDefaultFiles, UseDirectoryBrowser и UseStaticFiles
            app.UseFileServer(new FileServerOptions
            {
                StaticFileOptions =
                {
                    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
                    RequestPath  = string.Empty
                },

                //EnableDirectoryBrowsing = true,
                //DirectoryBrowserOptions =
                //{
                //    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "content")),
                //    RequestPath = "/admin/contentFolder"
                //},

                EnableDefaultFiles  = true,
                DefaultFilesOptions =
                {
                    DefaultFileNames = new List <string> {
                        "index.html"
                    }
                }
            });

            ////позволяет пользователям просматривать содержимое каталога wwwroot
            ////также можно переопределить на какую папку будет указывать DirectoryBrowser
            ////и по какому url DirectoryBrowser будет "открываться"
            //app.UseDirectoryBrowser(new DirectoryBrowserOptions
            //{
            //    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"content")),
            //    RequestPath = "/admin/contentFolder"
            //});
            ////при обращении к корню ищет статические файлы: default.htm, default.html, index.htm, index.html
            ////файлы по-умолчанию можно переопределить
            //app.UseDefaultFiles(new DefaultFilesOptions
            //{
            //    DefaultFileNames = new List<string> { "index.html" }
            //});
            //кстати UseDefaultFiles не прерывает конвеер, а только подставляет нужный path
            //app.Run(context=>context.Response.WriteAsync(context.Request.Path));

            //middleware для использования статичных файлов
            //app.UseStaticFiles();

            //включаем в конвеер свой компонент middleware
            //app.UseMiddleware<TokenMiddleware>();
            //либо через метод расширения
            //app.UseToken("1234");

            //Сессия
            app.UseSession();

            //включаем возможность маршрутизации, например - использование метода UseEndpoints
            app.UseRouting();

            //! Компоненты middleware создаются один раз и живут в течение всего жизненного цикла приложения
            //т.е. значение x не будет сбрасываться между запросами
            int x = 1;

            //установка маршрутов
            app.UseEndpoints(endpoints =>
            {
                //обработка Get запроса по адресу "/"
                endpoints.MapGet("/", async context =>
                {
                    x += 7;
                    //отправка ответа прямо в контекст
                    await context.Response.WriteAsync($"Hello World! x={x}");
                });

                endpoints.MapGet("/env", async context =>
                {
                    bool rootExists = env.ContentRootFileProvider.GetDirectoryContents("/").Exists;
                    string result   = $"<h1>IWebHostEnvironment</h1><p>ApplicationName: {env.ApplicationName}<br>EnvironmentName: {env.EnvironmentName}<br>WebRootPath: {env.WebRootPath}<br>ContentRootPath: {env.ContentRootPath}<br>Is root exists: {rootExists}</p>";
                    await context.Response.WriteAsync(result);
                });

                endpoints.MapGet("/err", context => throw new Exception("This is test exception"));

                endpoints.MapGet("/errorHandler", context => context.Response.WriteAsync("<h1>Oops!</h1><h2>Some error happened!<h2>"));
                endpoints.MapGet("/listServices", ListAllServices);

                //Конфигурация. Пример операций чтения/изменения
                endpoints.MapGet("/getConfig", context =>
                {
                    string username  = _exampleConfig["username"];
                    string[] configs = _exampleConfig.GetChildren().Select(x => $"Path={x.Path}; {x.Key}={x.Value}").ToArray();

                    //Примеры привязки конфигурации к объекту                //По-умолчанию привязываются только публичные переменные
                    var bindedOptions = new ExampleOptions();                //если установить BindNonPublicProperties = true, то будут привязываться все переменные, за исключением readonly
                    _exampleConfig.Bind(bindedOptions, options => options.BindNonPublicProperties = true);
                    var bindedOptionsAlt  = _exampleConfig.Get <ExampleOptions>();
                    var bindedOptionsAlt2 = _exampleConfig.Get <ExampleOptions>(options => options.BindNonPublicProperties = true);

                    return(context.Response.WriteAsync($"username: {username}\n\nAll configs:\n{string.Join("\n", configs)}"));
                });
                endpoints.MapGet("/getConfigAll", context =>
                {
                    string[] configs = _appConfig.GetChildren().Select(x => $"{x.Path}={x.Value}").ToArray();

                    return(context.Response.WriteAsync($"All configs:\n\n{string.Join("\n", configs)}"));
                });
                endpoints.MapGet("/updateConfig", context =>
                {
                    string name  = context.Request.Query["name"];
                    string value = context.Request.Query["value"];

                    bool isRecordExists = !string.IsNullOrEmpty(_exampleConfig[name]);

                    _exampleConfig[name] = value;

                    return(context.Response.WriteAsync($"Record {(isRecordExists ? "updated" : "added")}  {name}={value}"));
                });
                endpoints.MapGet("/deleteConfig", context =>
                {
                    string name = context.Request.Query["name"];

                    bool isRecordExists = !string.IsNullOrEmpty(_exampleConfig[name]);

                    if (isRecordExists)
                    {
                        _exampleConfig[name] = null;
                    }

                    return(context.Response.WriteAsync($"Record {(isRecordExists ? "deleted" : "not exists")}"));
                });

                //Cookies
                endpoints.MapGet("/getCookie", context =>
                {
                    int cookieCount   = context.Request.Cookies.Count;
                    string cookieKeys = string.Join(", ", context.Request.Cookies.Keys);
                    string myCookie   = context.Request.Cookies["myCookie"];

                    return(context.Response.WriteAsync($"Total cookies: {cookieCount}\nKeys: {cookieKeys}\nMyCookie: {myCookie}"));
                });
                endpoints.MapGet("/setCookie", context =>
                {
                    string name  = context.Request.Query["name"];
                    string value = context.Request.Query["value"];

                    context.Response.Cookies.Append(name, value);

                    return(context.Response.WriteAsync($"Set cookie: {name}={value}"));
                });
                endpoints.MapGet("/setCookie2", context =>
                {
                    string name  = context.Request.Query["name"];
                    string value = context.Request.Query["value"];

                    var cookieOptions = new CookieOptions
                    {
                        Path        = "/",
                        MaxAge      = TimeSpan.FromDays(2),
                        Domain      = "localhost",
                        Expires     = DateTimeOffset.Now.AddDays(1),
                        HttpOnly    = true,
                        IsEssential = true,
                        SameSite    = SameSiteMode.Lax,
                        Secure      = true
                    };

                    context.Response.Cookies.Append(name, value, cookieOptions);

                    return(context.Response.WriteAsync($"Set cookie: {name}={value}"));
                });

                //Session
                endpoints.MapGet("/getSession", context =>
                {
                    bool isSessionAvailable = context.Session.IsAvailable;
                    string sessionKeys      = string.Join(", ", context.Session.Keys);
                    string sessionID        = context.Session.Id;
                    string foo = context.Session.GetString("foo");

                    return(context.Response.WriteAsync($"SessionAvailable: {isSessionAvailable}\nKeys: {sessionKeys}\nID: {sessionID}\nfoo: {foo}"));
                });
                endpoints.MapGet("/setSession", context =>
                {
                    string value = context.Request.Query["value"];
                    context.Session.SetString("foo", value);

                    return(context.Response.WriteAsync($"Session set"));
                });

                //Замечание при работе с разным жизненным циклом:
                //Transient:
                //  в /send и /send2 значение не будет менятся между запросами, но у каждого будет своё значение
                //  это из-за того, что Configure вызывается один раз при построении приложения и переданные объекты сервисов остаются одни и теже
                //  в /send3 и /send4 каждый раз будет меняться значение, т.к. каждый раз запрашивается новый сервис
                //Scoped:
                //  в /send и /send2 значение будет одно и тоже, т.е. одинаковое у обоих причем даже в разных запросах
                //  это тоже из-за того, что Configure вызывается один раз, но в этот раз объект сервиса одинаковый в рамках запроса, т.е. senderService._sender == sender
                //  в /send3 каждый раз будет меняться значение
                //  в /send4 вернется ошибка. В чем причина - сложно сказать. Скорее всего дело в том, что объект сервиса привязывается к запросу.
                //      А т.к. тут вызывается ApplicationServices, созданный в первом запросе, то после завершения запроса объект удаляется сборщиком мусора и ссылка на сервис теряется.
                //      По идее тогда этот путь должен отработать первым запросом? Идея не сработала =(
                //Singleton:
                //  во всех случаях выводится одно и то же значение
                endpoints.MapGet("/send", context => context.Response.WriteAsync(sender.Send()));
                endpoints.MapGet("/send2", context =>
                {
                    string result = senderService.Send();
                    return(context.Response.WriteAsync(result));
                });
                endpoints.MapGet("/send3", context =>
                {
                    var sender    = context.RequestServices.GetService <IMessageSender>();
                    string result = sender.Send();
                    return(context.Response.WriteAsync(result));
                });
                endpoints.MapGet("/send4", context =>
                {
                    var sender    = app.ApplicationServices.GetService <IMessageSender>();
                    string result = sender.Send();
                    return(context.Response.WriteAsync(result));
                });
            });

            app.Use(async(context, next) =>
            {
                x -= 3;
                await next.Invoke();
                x -= 1;
            });

            app.Map("/index", builder =>
            {
                builder.Run(context => context.Response.WriteAsync("This is Index"));
            });

            app.MapWhen(context => context.Request.Path.Value.Contains("foo"), builder =>
            {
                builder.Run(context => context.Response.WriteAsync("<h1>BAR!!</h1>"));
            });

            //Для создания компонентов middleware используется делегат RequestDelegate
            //Он выполняет некоторое действие и принимает контекст запроса
            //RequestDelegate delegateExample = context => context.Response.WriteAsync($"Not Found! x={x}");
            //app.Run(delegateExample);

            //app.Use(async (context, next) => await next());
        }