├── .gitignore ├── LICENSE ├── README.md └── src ├── 1.Options └── App │ ├── App.csproj │ ├── Controllers │ └── ValuesController.cs │ ├── Options │ └── AppOptions.cs │ ├── Program.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 10.Nancy └── App │ ├── App.csproj │ ├── Modules │ └── HomeModule.cs │ ├── Program.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 11.Docker └── App │ ├── App.csproj │ ├── Controllers │ └── ValuesController.cs │ ├── Dockerfile │ ├── Program.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 12.Nginx ├── App │ ├── App.csproj │ ├── Controllers │ │ └── ValuesController.cs │ ├── Program.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json └── nginx_config.txt ├── 2.Middleware └── App │ ├── App.csproj │ ├── Controllers │ └── ValuesController.cs │ ├── CustomMiddleware.cs │ ├── Extensions.cs │ ├── Program.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 3.Filters └── App │ ├── App.csproj │ ├── Controllers │ └── ValuesController.cs │ ├── Filters │ └── ExceptionHandlerAttribute.cs │ ├── Program.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 4.Autofac └── App │ ├── App.csproj │ ├── Controllers │ └── ValuesController.cs │ ├── Program.cs │ ├── Services │ ├── IValueService.cs │ └── ValueService.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 5.Tests ├── App.Tests │ ├── App.Tests.csproj │ ├── Controllers │ │ └── UsersControllerTests.cs │ └── Services │ │ └── UserServiceTests.cs └── App │ ├── App.csproj │ ├── Commands │ └── CreateUser.cs │ ├── Controllers │ └── UsersController.cs │ ├── Models │ └── User.cs │ ├── Program.cs │ ├── Repositories │ ├── IUserRepository.cs │ └── UserRepository.cs │ ├── Services │ ├── IUserService.cs │ └── UserService.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 6.SQLServer └── App │ ├── App.csproj │ ├── Controllers │ └── SqlController.cs │ ├── Program.cs │ ├── Services │ ├── ISqlService.cs │ └── SqlService.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 7.MongoDB └── App │ ├── App.csproj │ ├── Controllers │ └── MongoController.cs │ ├── Models │ └── User.cs │ ├── MongoConfigurator.cs │ ├── Program.cs │ ├── Services │ ├── IMongoService.cs │ └── MongoService.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── 8.Redis └── App │ ├── App.csproj │ ├── Controllers │ └── RedisController.cs │ ├── Program.cs │ ├── Services │ ├── IRedisService.cs │ └── RedisService.cs │ ├── Startup.cs │ ├── appsettings.Development.json │ └── appsettings.json └── 9.RabbitMQ └── App ├── App.csproj ├── Options └── RabbitMqOptions.cs ├── Program.cs ├── Startup.cs ├── appsettings.Development.json └── appsettings.json /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # Visual Studio code coverage results 114 | *.coverage 115 | *.coveragexml 116 | 117 | # NCrunch 118 | _NCrunch_* 119 | .*crunch*.local.xml 120 | nCrunchTemp_* 121 | 122 | # MightyMoose 123 | *.mm.* 124 | AutoTest.Net/ 125 | 126 | # Web workbench (sass) 127 | .sass-cache/ 128 | 129 | # Installshield output folder 130 | [Ee]xpress/ 131 | 132 | # DocProject is a documentation generator add-in 133 | DocProject/buildhelp/ 134 | DocProject/Help/*.HxT 135 | DocProject/Help/*.HxC 136 | DocProject/Help/*.hhc 137 | DocProject/Help/*.hhk 138 | DocProject/Help/*.hhp 139 | DocProject/Help/Html2 140 | DocProject/Help/html 141 | 142 | # Click-Once directory 143 | publish/ 144 | 145 | # Publish Web Output 146 | *.[Pp]ublish.xml 147 | *.azurePubxml 148 | # TODO: Comment the next line if you want to checkin your web deploy settings 149 | # but database connection strings (with potential passwords) will be unencrypted 150 | *.pubxml 151 | *.publishproj 152 | 153 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 154 | # checkin your Azure Web App publish settings, but sensitive information contained 155 | # in these scripts will be unencrypted 156 | PublishScripts/ 157 | 158 | # NuGet Packages 159 | *.nupkg 160 | # The packages folder can be ignored because of Package Restore 161 | **/packages/* 162 | # except build/, which is used as an MSBuild target. 163 | !**/packages/build/ 164 | # Uncomment if necessary however generally it will be regenerated when needed 165 | #!**/packages/repositories.config 166 | # NuGet v3's project.json files produces more ignoreable files 167 | *.nuget.props 168 | *.nuget.targets 169 | 170 | # Microsoft Azure Build Output 171 | csx/ 172 | *.build.csdef 173 | 174 | # Microsoft Azure Emulator 175 | ecf/ 176 | rcf/ 177 | 178 | # Windows Store app package directories and files 179 | AppPackages/ 180 | BundleArtifacts/ 181 | Package.StoreAssociation.xml 182 | _pkginfo.txt 183 | 184 | # Visual Studio cache files 185 | # files ending in .cache can be ignored 186 | *.[Cc]ache 187 | # but keep track of directories ending in .cache 188 | !*.[Cc]ache/ 189 | 190 | # Others 191 | ClientBin/ 192 | ~$* 193 | *~ 194 | *.dbmdl 195 | *.dbproj.schemaview 196 | *.jfm 197 | *.pfx 198 | *.publishsettings 199 | node_modules/ 200 | orleans.codegen.cs 201 | 202 | # Since there are multiple workflows, uncomment next line to ignore bower_components 203 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 204 | #bower_components/ 205 | 206 | # RIA/Silverlight projects 207 | Generated_Code/ 208 | 209 | # Backup & report files from converting an old project file 210 | # to a newer Visual Studio version. Backup files are not needed, 211 | # because we have git ;-) 212 | _UpgradeReport_Files/ 213 | Backup*/ 214 | UpgradeLog*.XML 215 | UpgradeLog*.htm 216 | 217 | # SQL Server files 218 | *.mdf 219 | *.ldf 220 | 221 | # Business Intelligence projects 222 | *.rdl.data 223 | *.bim.layout 224 | *.bim_*.settings 225 | 226 | # Microsoft Fakes 227 | FakesAssemblies/ 228 | 229 | # GhostDoc plugin setting file 230 | *.GhostDoc.xml 231 | 232 | # Node.js Tools for Visual Studio 233 | .ntvs_analysis.dat 234 | 235 | # Visual Studio 6 build log 236 | *.plg 237 | 238 | # Visual Studio 6 workspace options file 239 | *.opt 240 | 241 | # Visual Studio LightSwitch build output 242 | **/*.HTMLClient/GeneratedArtifacts 243 | **/*.DesktopClient/GeneratedArtifacts 244 | **/*.DesktopClient/ModelManifest.xml 245 | **/*.Server/GeneratedArtifacts 246 | **/*.Server/ModelManifest.xml 247 | _Pvt_Extensions 248 | 249 | # Paket dependency manager 250 | .paket/paket.exe 251 | paket-files/ 252 | 253 | # FAKE - F# Make 254 | .fake/ 255 | 256 | # JetBrains Rider 257 | .idea/ 258 | *.sln.iml 259 | 260 | # CodeRush 261 | .cr/ 262 | 263 | # Python Tools for Visual Studio (PTVS) 264 | __pycache__/ 265 | *.pyc 266 | 267 | .vscode/* 268 | !.vscode/settings.json 269 | !.vscode/tasks.json 270 | !.vscode/launch.json 271 | 272 | appsettings.local.json 273 | .vscode/ 274 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Piotr Gankiewicz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASP.NET Core samples 2 | 3 | 4 | For more information take a look at my [blog post](http://piotrgankiewicz.com/2017/04/17/asp-net-core-12-samples/). 5 | 6 | ### 1. Options 7 | ### 2. Middleware 8 | ### 3. Filters 9 | ### 4. Autofac 10 | ### 5. Tests 11 | ### 6. SQL Server 12 | ### 7. MongoDB 13 | ### 8. Redis 14 | ### 9. RabbitMQ 15 | ### 10. Nancy 16 | ### 11. Docker 17 | ### 12. Nginx -------------------------------------------------------------------------------- /src/1.Options/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/1.Options/App/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Microsoft.AspNetCore.Mvc; 3 | using Microsoft.Extensions.Options; 4 | 5 | namespace App.Controllers 6 | { 7 | [Route("api/[controller]")] 8 | public class ValuesController : Controller 9 | { 10 | private readonly IOptions _appOptions; 11 | 12 | public ValuesController(IOptions appOptions) 13 | { 14 | _appOptions = appOptions; 15 | } 16 | 17 | // GET api/values 18 | [HttpGet] 19 | public IEnumerable Get() 20 | { 21 | return new string[] { "value1", "value2" }; 22 | } 23 | 24 | // GET api/values/5 25 | [HttpGet("{id}")] 26 | public string Get(int id) 27 | { 28 | return "value"; 29 | } 30 | 31 | // POST api/values 32 | [HttpPost] 33 | public void Post([FromBody]string value) 34 | { 35 | } 36 | 37 | // PUT api/values/5 38 | [HttpPut("{id}")] 39 | public void Put(int id, [FromBody]string value) 40 | { 41 | } 42 | 43 | // DELETE api/values/5 44 | [HttpDelete("{id}")] 45 | public void Delete(int id) 46 | { 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/1.Options/App/Options/AppOptions.cs: -------------------------------------------------------------------------------- 1 | namespace App 2 | { 3 | public class AppOptions 4 | { 5 | public string Name { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/1.Options/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | 5 | namespace App 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | var host = new WebHostBuilder() 12 | .UseKestrel() 13 | .UseContentRoot(Directory.GetCurrentDirectory()) 14 | .UseIISIntegration() 15 | .UseStartup() 16 | .Build(); 17 | 18 | host.Run(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/1.Options/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.Logging; 6 | 7 | namespace App 8 | { 9 | public class Startup 10 | { 11 | public Startup(IHostingEnvironment env) 12 | { 13 | var builder = new ConfigurationBuilder() 14 | .SetBasePath(env.ContentRootPath) 15 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 16 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 17 | .AddEnvironmentVariables(); 18 | Configuration = builder.Build(); 19 | } 20 | 21 | public IConfigurationRoot Configuration { get; } 22 | 23 | // This method gets called by the runtime. Use this method to add services to the container. 24 | public void ConfigureServices(IServiceCollection services) 25 | { 26 | // Add framework services. 27 | services.AddOptions(); 28 | services.AddMvc(); 29 | services.Configure(Configuration.GetSection("app")); 30 | } 31 | 32 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 33 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 34 | { 35 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 36 | loggerFactory.AddDebug(); 37 | 38 | app.UseMvc(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/1.Options/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/1.Options/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | }, 8 | "app": { 9 | "name": "Czwartki z .NET" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/10.Nancy/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/10.Nancy/App/Modules/HomeModule.cs: -------------------------------------------------------------------------------- 1 | using Nancy; 2 | 3 | namespace App.Modules 4 | { 5 | public class HomeModule : NancyModule 6 | { 7 | public HomeModule() 8 | { 9 | Get("", args => "Nancy says hello!"); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /src/10.Nancy/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace App 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/10.Nancy/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.Logging; 6 | using Nancy.Owin; 7 | 8 | namespace App 9 | { 10 | public class Startup 11 | { 12 | public Startup(IHostingEnvironment env) 13 | { 14 | var builder = new ConfigurationBuilder() 15 | .SetBasePath(env.ContentRootPath) 16 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 17 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 18 | .AddEnvironmentVariables(); 19 | Configuration = builder.Build(); 20 | } 21 | 22 | public IConfigurationRoot Configuration { get; } 23 | 24 | // This method gets called by the runtime. Use this method to add services to the container. 25 | public void ConfigureServices(IServiceCollection services) 26 | { 27 | // Add framework services. 28 | services.AddMvc(); 29 | } 30 | 31 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 32 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 33 | { 34 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 35 | loggerFactory.AddDebug(); 36 | 37 | app.UseOwin().UseNancy(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/10.Nancy/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/10.Nancy/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/11.Docker/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/11.Docker/App/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace App.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/11.Docker/App/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM microsoft/dotnet:latest 2 | COPY . /app 3 | WORKDIR /app 4 | 5 | RUN ["dotnet", "restore", "--source", "https://api.nuget.org/v3/index.json", "--no-cache"] 6 | RUN ["dotnet", "build"] 7 | 8 | EXPOSE 5000/tcp 9 | ENV ASPNETCORE_URLS http://*:5000 10 | ENV ASPNETCORE_ENVIRONMENT docker 11 | 12 | ENTRYPOINT ["dotnet", "run"] 13 | -------------------------------------------------------------------------------- /src/11.Docker/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace App 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/11.Docker/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace App 12 | { 13 | public class Startup 14 | { 15 | public Startup(IHostingEnvironment env) 16 | { 17 | var builder = new ConfigurationBuilder() 18 | .SetBasePath(env.ContentRootPath) 19 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 20 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 21 | .AddEnvironmentVariables(); 22 | Configuration = builder.Build(); 23 | } 24 | 25 | public IConfigurationRoot Configuration { get; } 26 | 27 | // This method gets called by the runtime. Use this method to add services to the container. 28 | public void ConfigureServices(IServiceCollection services) 29 | { 30 | // Add framework services. 31 | services.AddMvc(); 32 | } 33 | 34 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 35 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 36 | { 37 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 38 | loggerFactory.AddDebug(); 39 | 40 | app.UseMvc(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/11.Docker/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/11.Docker/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/12.Nginx/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/12.Nginx/App/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace App.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/12.Nginx/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace App 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/12.Nginx/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace App 12 | { 13 | public class Startup 14 | { 15 | public Startup(IHostingEnvironment env) 16 | { 17 | var builder = new ConfigurationBuilder() 18 | .SetBasePath(env.ContentRootPath) 19 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 20 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 21 | .AddEnvironmentVariables(); 22 | Configuration = builder.Build(); 23 | } 24 | 25 | public IConfigurationRoot Configuration { get; } 26 | 27 | // This method gets called by the runtime. Use this method to add services to the container. 28 | public void ConfigureServices(IServiceCollection services) 29 | { 30 | // Add framework services. 31 | services.AddMvc(); 32 | } 33 | 34 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 35 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 36 | { 37 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 38 | loggerFactory.AddDebug(); 39 | 40 | app.UseMvc(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/12.Nginx/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/12.Nginx/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/12.Nginx/nginx_config.txt: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | 4 | location / { 5 | proxy_pass http://localhost:5000; 6 | proxy_set_header Upgrade $http_upgrade; 7 | proxy_set_header Connection 'upgrade'; 8 | proxy_set_header Host $host; 9 | proxy_cache_bypass $http_upgrade; 10 | } 11 | } -------------------------------------------------------------------------------- /src/2.Middleware/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/2.Middleware/App/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace App.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/2.Middleware/App/CustomMiddleware.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Microsoft.AspNetCore.Http; 4 | 5 | namespace App 6 | { 7 | public class CustomMiddleware 8 | { 9 | private readonly RequestDelegate _next; 10 | 11 | public CustomMiddleware(RequestDelegate next) 12 | { 13 | _next = next; 14 | } 15 | 16 | public async Task Invoke(HttpContext httpContext) 17 | { 18 | Console.WriteLine($"Invoking custom middleware, path: {httpContext.Request.Path}."); 19 | await _next.Invoke(httpContext); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/2.Middleware/App/Extensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | 3 | namespace App 4 | { 5 | public static class Extensions 6 | { 7 | public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder) 8 | => builder.UseMiddleware(); 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /src/2.Middleware/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | 5 | namespace App 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | var host = new WebHostBuilder() 12 | .UseKestrel() 13 | .UseContentRoot(Directory.GetCurrentDirectory()) 14 | .UseIISIntegration() 15 | .UseStartup() 16 | .Build(); 17 | 18 | host.Run(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/2.Middleware/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.Logging; 6 | 7 | namespace App 8 | { 9 | public class Startup 10 | { 11 | public Startup(IHostingEnvironment env) 12 | { 13 | var builder = new ConfigurationBuilder() 14 | .SetBasePath(env.ContentRootPath) 15 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 16 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 17 | .AddEnvironmentVariables(); 18 | Configuration = builder.Build(); 19 | } 20 | 21 | public IConfigurationRoot Configuration { get; } 22 | 23 | // This method gets called by the runtime. Use this method to add services to the container. 24 | public void ConfigureServices(IServiceCollection services) 25 | { 26 | // Add framework services. 27 | services.AddMvc(); 28 | } 29 | 30 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 31 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 32 | { 33 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 34 | loggerFactory.AddDebug(); 35 | 36 | app.UseCustomMiddleware(); 37 | app.UseMvc(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/2.Middleware/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/2.Middleware/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/3.Filters/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/3.Filters/App/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using App.Filters; 4 | using Microsoft.AspNetCore.Mvc; 5 | 6 | namespace App.Controllers 7 | { 8 | [Route("api/[controller]")] 9 | [ExceptionHandler] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | throw new Exception("Something wrong has just happened."); 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/3.Filters/App/Filters/ExceptionHandlerAttribute.cs: -------------------------------------------------------------------------------- 1 | using System.Net; 2 | using Microsoft.AspNetCore.Mvc; 3 | using Microsoft.AspNetCore.Mvc.Filters; 4 | 5 | namespace App.Filters 6 | { 7 | public class ExceptionHandlerAttribute : ExceptionFilterAttribute 8 | { 9 | public override void OnException(ExceptionContext context) 10 | { 11 | var exception = context.Exception; 12 | var error = new 13 | { 14 | message = exception.Message 15 | }; 16 | var result = new JsonResult(error); 17 | result.StatusCode = (int)HttpStatusCode.BadRequest; 18 | context.Result = result; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/3.Filters/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace App 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/3.Filters/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace App 12 | { 13 | public class Startup 14 | { 15 | public Startup(IHostingEnvironment env) 16 | { 17 | var builder = new ConfigurationBuilder() 18 | .SetBasePath(env.ContentRootPath) 19 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 20 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 21 | .AddEnvironmentVariables(); 22 | Configuration = builder.Build(); 23 | } 24 | 25 | public IConfigurationRoot Configuration { get; } 26 | 27 | // This method gets called by the runtime. Use this method to add services to the container. 28 | public void ConfigureServices(IServiceCollection services) 29 | { 30 | // Add framework services. 31 | services.AddMvc(); 32 | } 33 | 34 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 35 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 36 | { 37 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 38 | loggerFactory.AddDebug(); 39 | 40 | app.UseMvc(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/3.Filters/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/3.Filters/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/4.Autofac/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/4.Autofac/App/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using App.Services; 6 | using Microsoft.AspNetCore.Mvc; 7 | 8 | namespace App.Controllers 9 | { 10 | [Route("api/[controller]")] 11 | public class ValuesController : Controller 12 | { 13 | private readonly IValueService _valueService; 14 | 15 | public ValuesController(IValueService valueService) 16 | { 17 | _valueService = valueService; 18 | } 19 | 20 | // GET api/values 21 | [HttpGet] 22 | public IEnumerable Get() 23 | { 24 | return new string[] { "value1", "value2" }; 25 | } 26 | 27 | // GET api/values/5 28 | [HttpGet("{id}")] 29 | public string Get(int id) => _valueService.Get(id); 30 | 31 | // POST api/values 32 | [HttpPost] 33 | public void Post([FromBody]string value) 34 | { 35 | } 36 | 37 | // PUT api/values/5 38 | [HttpPut("{id}")] 39 | public void Put(int id, [FromBody]string value) 40 | { 41 | } 42 | 43 | // DELETE api/values/5 44 | [HttpDelete("{id}")] 45 | public void Delete(int id) 46 | { 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/4.Autofac/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | 5 | namespace App 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | var host = new WebHostBuilder() 12 | .UseKestrel() 13 | .UseContentRoot(Directory.GetCurrentDirectory()) 14 | .UseIISIntegration() 15 | .UseStartup() 16 | .Build(); 17 | 18 | host.Run(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/4.Autofac/App/Services/IValueService.cs: -------------------------------------------------------------------------------- 1 | namespace App.Services 2 | { 3 | public interface IValueService 4 | { 5 | string Get(int id); 6 | } 7 | } -------------------------------------------------------------------------------- /src/4.Autofac/App/Services/ValueService.cs: -------------------------------------------------------------------------------- 1 | namespace App.Services 2 | { 3 | public class ValueService : IValueService 4 | { 5 | public string Get(int id) => $"Value {id}"; 6 | } 7 | } -------------------------------------------------------------------------------- /src/4.Autofac/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using App.Services; 3 | using Autofac; 4 | using Autofac.Extensions.DependencyInjection; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace App 12 | { 13 | public class Startup 14 | { 15 | public IConfigurationRoot Configuration { get; } 16 | public IContainer ApplicationContainer { get; private set; } 17 | 18 | public Startup(IHostingEnvironment env) 19 | { 20 | var builder = new ConfigurationBuilder() 21 | .SetBasePath(env.ContentRootPath) 22 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 23 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 24 | .AddEnvironmentVariables(); 25 | Configuration = builder.Build(); 26 | } 27 | 28 | 29 | // This method gets called by the runtime. Use this method to add services to the container. 30 | public IServiceProvider ConfigureServices(IServiceCollection services) 31 | { 32 | // Add framework services. 33 | services.AddMvc(); 34 | 35 | var builder = new ContainerBuilder(); 36 | builder.RegisterType().As(); 37 | builder.Populate(services); 38 | this.ApplicationContainer = builder.Build(); 39 | 40 | // Create the IServiceProvider based on the container. 41 | return new AutofacServiceProvider(this.ApplicationContainer); 42 | } 43 | 44 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 45 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, 46 | ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) 47 | { 48 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 49 | loggerFactory.AddDebug(); 50 | 51 | app.UseMvc(); 52 | appLifetime.ApplicationStopped.Register(() => this.ApplicationContainer.Dispose()); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/4.Autofac/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/4.Autofac/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/5.Tests/App.Tests/App.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/5.Tests/App.Tests/Controllers/UsersControllerTests.cs: -------------------------------------------------------------------------------- 1 | using System.Net; 2 | using System.Net.Http; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using App.Commands; 6 | using App.Models; 7 | using FluentAssertions; 8 | using Microsoft.AspNetCore.Hosting; 9 | using Microsoft.AspNetCore.TestHost; 10 | using Newtonsoft.Json; 11 | using Xunit; 12 | 13 | namespace App.Tests.Controllers 14 | { 15 | public class UsersControllerTests 16 | { 17 | protected readonly TestServer Server; 18 | protected readonly HttpClient Client; 19 | 20 | public UsersControllerTests() 21 | { 22 | Server = new TestServer(new WebHostBuilder() 23 | .UseStartup()); 24 | Client = Server.CreateClient(); 25 | } 26 | 27 | [Fact] 28 | public async Task given_valid_email_user_should_exist() 29 | { 30 | var email = "user1@email.com"; 31 | var user = await GetUserAsync(email); 32 | user.Email.ShouldBeEquivalentTo(email); 33 | } 34 | 35 | [Fact] 36 | public async Task given_invalid_email_user_should_not_exist() 37 | { 38 | var email = "user1000@email.com"; 39 | var response = await Client.GetAsync($"users/{email}"); 40 | response.StatusCode.ShouldBeEquivalentTo(HttpStatusCode.NotFound); 41 | } 42 | 43 | [Fact] 44 | public async Task given_unique_email_user_should_be_created() 45 | { 46 | var command = new CreateUser 47 | { 48 | Email = "test@email.com", 49 | Password = "secret" 50 | }; 51 | var payload = GetPayload(command); 52 | var response = await Client.PostAsync("users", payload); 53 | response.StatusCode.ShouldBeEquivalentTo(HttpStatusCode.Created); 54 | response.Headers.Location.ToString().ShouldBeEquivalentTo($"users/{command.Email}"); 55 | 56 | var user = await GetUserAsync(command.Email); 57 | user.Email.ShouldBeEquivalentTo(command.Email); 58 | } 59 | 60 | private async Task GetUserAsync(string email) 61 | { 62 | var response = await Client.GetAsync($"users/{email}"); 63 | var responseString = await response.Content.ReadAsStringAsync(); 64 | 65 | return JsonConvert.DeserializeObject(responseString); 66 | } 67 | 68 | protected static StringContent GetPayload(object data) 69 | { 70 | var json = JsonConvert.SerializeObject(data); 71 | 72 | return new StringContent(json, Encoding.UTF8, "application/json"); 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /src/5.Tests/App.Tests/Services/UserServiceTests.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using App.Models; 3 | using App.Repositories; 4 | using App.Services; 5 | using Moq; 6 | using Xunit; 7 | 8 | namespace App.Tests.Services 9 | { 10 | public class UserServiceTests 11 | { 12 | [Fact] 13 | public async Task create_async_should_invoke_add_async_on_repository() 14 | { 15 | //Arrange 16 | var userRepositoryMock = new Mock(); 17 | var userService = new UserService(userRepositoryMock.Object); 18 | 19 | //Act 20 | await userService.CreateAsync("user@test.com", "secret"); 21 | 22 | //Assert 23 | userRepositoryMock.Verify(x => x.AddAsync(It.IsAny()), Times.Once); 24 | } 25 | 26 | [Fact] 27 | public async Task when_calling_get_async_and_user_exists_it_should_invoke_user_repository_get_async() 28 | { 29 | //Arrange 30 | var userRepositoryMock = new Mock(); 31 | var userService = new UserService(userRepositoryMock.Object); 32 | var email = "user@test.com"; 33 | var user = new User(email, "secret"); 34 | userRepositoryMock.Setup(x => x.GetAsync(It.IsAny())) 35 | .ReturnsAsync(user); 36 | 37 | //Act 38 | await userService.GetAsync(email); 39 | 40 | //Assert 41 | userRepositoryMock.Verify(x => x.GetAsync(It.IsAny()), Times.Once()); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /src/5.Tests/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/5.Tests/App/Commands/CreateUser.cs: -------------------------------------------------------------------------------- 1 | namespace App.Commands 2 | { 3 | public class CreateUser 4 | { 5 | public string Email { get; set; } 6 | public string Password { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /src/5.Tests/App/Controllers/UsersController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using App.Commands; 3 | using App.Services; 4 | using Microsoft.AspNetCore.Mvc; 5 | 6 | namespace App.Controllers 7 | { 8 | [Route("[controller]")] 9 | public class UsersController : Controller 10 | { 11 | private readonly IUserService _userService; 12 | 13 | public UsersController(IUserService userService) 14 | { 15 | _userService = userService; 16 | } 17 | 18 | [HttpGet("{email}")] 19 | public async Task Get(string email) 20 | { 21 | var user = await _userService.GetAsync(email); 22 | if(user == null) 23 | { 24 | return NotFound(); 25 | } 26 | 27 | return Json(user); 28 | } 29 | 30 | [HttpPost] 31 | public async Task Post([FromBody]CreateUser command) 32 | { 33 | await _userService.CreateAsync(command.Email, command.Password); 34 | 35 | return Created($"users/{command.Email}", new object()); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/5.Tests/App/Models/User.cs: -------------------------------------------------------------------------------- 1 | namespace App.Models 2 | { 3 | public class User 4 | { 5 | public string Email { get; set; } 6 | public string Password { get; set; } 7 | 8 | protected User() 9 | { 10 | } 11 | 12 | public User(string email, string password) 13 | { 14 | Email = email; 15 | Password = password; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/5.Tests/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | 5 | namespace App 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | var host = new WebHostBuilder() 12 | .UseKestrel() 13 | .UseContentRoot(Directory.GetCurrentDirectory()) 14 | .UseIISIntegration() 15 | .UseStartup() 16 | .Build(); 17 | 18 | host.Run(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/5.Tests/App/Repositories/IUserRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using App.Models; 3 | 4 | namespace App.Repositories 5 | { 6 | public interface IUserRepository 7 | { 8 | Task GetAsync(string email); 9 | Task AddAsync(User user); 10 | } 11 | } -------------------------------------------------------------------------------- /src/5.Tests/App/Repositories/UserRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using App.Models; 5 | 6 | namespace App.Repositories 7 | { 8 | public class UserRepository : IUserRepository 9 | { 10 | private static ISet _users = new HashSet 11 | { 12 | new User("user1@email.com", "secret"), 13 | new User("user2@email.com", "secret"), 14 | new User("user3@email.com", "secret") 15 | }; 16 | 17 | public async Task GetAsync(string email) 18 | => await Task.FromResult(_users.SingleOrDefault(x => x.Email == email)); 19 | 20 | public async Task AddAsync(User user) 21 | { 22 | _users.Add(user); 23 | await Task.CompletedTask; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/5.Tests/App/Services/IUserService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using App.Models; 3 | 4 | namespace App.Services 5 | { 6 | public interface IUserService 7 | { 8 | Task GetAsync(string email); 9 | Task CreateAsync(string email, string password); 10 | } 11 | } -------------------------------------------------------------------------------- /src/5.Tests/App/Services/UserService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using App.Models; 4 | using App.Repositories; 5 | 6 | namespace App.Services 7 | { 8 | public class UserService : IUserService 9 | { 10 | private readonly IUserRepository _userRepository; 11 | 12 | public UserService(IUserRepository userRepository) 13 | { 14 | _userRepository = userRepository; 15 | } 16 | 17 | public async Task GetAsync(string email) 18 | => await _userRepository.GetAsync(email); 19 | 20 | public async Task CreateAsync(string email, string password) 21 | { 22 | var user = await _userRepository.GetAsync(email); 23 | if(user != null) 24 | { 25 | throw new Exception($"User with email: '{email}' already exists."); 26 | } 27 | 28 | user = new User(email, password); 29 | await _userRepository.AddAsync(user); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/5.Tests/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using App.Repositories; 2 | using App.Services; 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Logging; 8 | 9 | namespace App 10 | { 11 | public class Startup 12 | { 13 | public Startup(IHostingEnvironment env) 14 | { 15 | var builder = new ConfigurationBuilder() 16 | .SetBasePath(env.ContentRootPath) 17 | .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 18 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 19 | .AddEnvironmentVariables(); 20 | Configuration = builder.Build(); 21 | } 22 | 23 | public IConfigurationRoot Configuration { get; } 24 | 25 | // This method gets called by the runtime. Use this method to add services to the container. 26 | public void ConfigureServices(IServiceCollection services) 27 | { 28 | // Add framework services. 29 | services.AddScoped(); 30 | services.AddScoped(); 31 | services.AddMvc(); 32 | } 33 | 34 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 35 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 36 | { 37 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 38 | loggerFactory.AddDebug(); 39 | 40 | app.UseMvc(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/5.Tests/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/5.Tests/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/6.SQLServer/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/6.SQLServer/App/Controllers/SqlController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using App.Services; 4 | using Microsoft.AspNetCore.Mvc; 5 | 6 | namespace App.Controllers 7 | { 8 | [Route("sql")] 9 | public class SqlController : Controller 10 | { 11 | private readonly ISqlService _sqlService; 12 | 13 | public SqlController(ISqlService sqlService) 14 | { 15 | _sqlService = sqlService; 16 | } 17 | 18 | [HttpGet] 19 | public async Task> Get() 20 | => await _sqlService.GetDatabasesAsync(); 21 | } 22 | } -------------------------------------------------------------------------------- /src/6.SQLServer/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace App 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/6.SQLServer/App/Services/ISqlService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace App.Services 5 | { 6 | public interface ISqlService 7 | { 8 | Task> GetDatabasesAsync(); 9 | } 10 | } -------------------------------------------------------------------------------- /src/6.SQLServer/App/Services/SqlService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Data.SqlClient; 3 | using System.Threading.Tasks; 4 | using Dapper; 5 | 6 | namespace App.Services 7 | { 8 | public class SqlService : ISqlService 9 | { 10 | public async Task> GetDatabasesAsync() 11 | { 12 | using(var connection = new SqlConnection("Server=localhost;User Id=SA; Password=secret;")) 13 | { 14 | var names = await connection 15 | .QueryAsync("select name from sys.Databases;"); 16 | 17 | return names; 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/6.SQLServer/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using App.Services; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Logging; 7 | 8 | namespace App 9 | { 10 | public class Startup 11 | { 12 | public Startup(IHostingEnvironment env) 13 | { 14 | var builder = new ConfigurationBuilder() 15 | .SetBasePath(env.ContentRootPath) 16 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 17 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 18 | .AddEnvironmentVariables(); 19 | Configuration = builder.Build(); 20 | } 21 | 22 | public IConfigurationRoot Configuration { get; } 23 | 24 | // This method gets called by the runtime. Use this method to add services to the container. 25 | public void ConfigureServices(IServiceCollection services) 26 | { 27 | // Add framework services. 28 | services.AddScoped(); 29 | services.AddMvc(); 30 | } 31 | 32 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 33 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 34 | { 35 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 36 | loggerFactory.AddDebug(); 37 | 38 | app.UseMvc(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/6.SQLServer/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/6.SQLServer/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/7.MongoDB/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/7.MongoDB/App/Controllers/MongoController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using App.Models; 4 | using App.Services; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace App.Controllers 8 | { 9 | [Route("mongo")] 10 | public class MongoController : Controller 11 | { 12 | private readonly IMongoService _mongoService; 13 | 14 | public MongoController(IMongoService mongoService) 15 | { 16 | _mongoService = mongoService; 17 | } 18 | 19 | [HttpGet] 20 | public async Task> Get() 21 | => await _mongoService.GetUsersAsync(); 22 | } 23 | } -------------------------------------------------------------------------------- /src/7.MongoDB/App/Models/User.cs: -------------------------------------------------------------------------------- 1 | namespace App.Models 2 | { 3 | public class User 4 | { 5 | public string Name { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/7.MongoDB/App/MongoConfigurator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using MongoDB.Bson; 3 | using MongoDB.Bson.Serialization.Conventions; 4 | 5 | namespace App 6 | { 7 | public static class MongoConfigurator 8 | { 9 | private static bool _initialized; 10 | public static void Initialize() 11 | { 12 | if (_initialized) 13 | return; 14 | 15 | RegisterConventions(); 16 | } 17 | 18 | private static void RegisterConventions() 19 | { 20 | ConventionRegistry.Register("MyConventions", new MongoConvention(), x => true); 21 | _initialized = true; 22 | } 23 | 24 | private class MongoConvention : IConventionPack 25 | { 26 | public IEnumerable Conventions => new List 27 | { 28 | new IgnoreExtraElementsConvention(true), 29 | new EnumRepresentationConvention(BsonType.String), 30 | new CamelCaseElementNameConvention() 31 | }; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/7.MongoDB/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace App 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/7.MongoDB/App/Services/IMongoService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using App.Models; 4 | 5 | namespace App.Services 6 | { 7 | public interface IMongoService 8 | { 9 | Task> GetUsersAsync(); 10 | } 11 | } -------------------------------------------------------------------------------- /src/7.MongoDB/App/Services/MongoService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using App.Models; 4 | using MongoDB.Driver; 5 | 6 | namespace App.Services 7 | { 8 | public class MongoService : IMongoService 9 | { 10 | private readonly IMongoDatabase _database; 11 | 12 | public MongoService(IMongoDatabase database) 13 | { 14 | _database = database; 15 | } 16 | 17 | public async Task> GetUsersAsync() 18 | { 19 | var users = await _database.GetCollection("Users") 20 | .AsQueryable() 21 | .ToListAsync(); 22 | 23 | return users; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/7.MongoDB/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using App.Services; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Logging; 7 | using MongoDB.Driver; 8 | 9 | namespace App 10 | { 11 | public class Startup 12 | { 13 | public Startup(IHostingEnvironment env) 14 | { 15 | var builder = new ConfigurationBuilder() 16 | .SetBasePath(env.ContentRootPath) 17 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 18 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 19 | .AddEnvironmentVariables(); 20 | Configuration = builder.Build(); 21 | } 22 | 23 | public IConfigurationRoot Configuration { get; } 24 | 25 | // This method gets called by the runtime. Use this method to add services to the container. 26 | public void ConfigureServices(IServiceCollection services) 27 | { 28 | // Add framework services. 29 | services.AddMvc(); 30 | services.AddScoped(); 31 | 32 | var mongoClient = new MongoClient("mongodb://localhost:27017"); 33 | var database = mongoClient.GetDatabase("asp-net-core-demo"); 34 | services.AddScoped(_ => database); 35 | } 36 | 37 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 38 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 39 | { 40 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 41 | loggerFactory.AddDebug(); 42 | 43 | app.UseMvc(); 44 | MongoConfigurator.Initialize(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/7.MongoDB/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/7.MongoDB/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/8.Redis/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/8.Redis/App/Controllers/RedisController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using App.Services; 3 | using Microsoft.AspNetCore.Mvc; 4 | 5 | namespace App.Controllers 6 | { 7 | [Route("redis")] 8 | public class RedisController : Controller 9 | { 10 | private readonly IRedisService _redisService; 11 | 12 | public RedisController(IRedisService redisService) 13 | { 14 | _redisService = redisService; 15 | } 16 | 17 | [HttpGet("{key}")] 18 | public async Task Get(string key) 19 | => await _redisService.GetAsync(key); 20 | } 21 | } -------------------------------------------------------------------------------- /src/8.Redis/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace App 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/8.Redis/App/Services/IRedisService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace App.Services 4 | { 5 | public interface IRedisService 6 | { 7 | Task GetAsync(string key); 8 | } 9 | } -------------------------------------------------------------------------------- /src/8.Redis/App/Services/RedisService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using StackExchange.Redis; 3 | 4 | namespace App.Services 5 | { 6 | public class RedisService : IRedisService 7 | { 8 | private readonly IDatabase _database; 9 | 10 | public RedisService(IDatabase database) 11 | { 12 | _database = database; 13 | } 14 | 15 | public async Task GetAsync(string key) 16 | => await _database.StringGetAsync(key); 17 | } 18 | } -------------------------------------------------------------------------------- /src/8.Redis/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using App.Services; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Logging; 7 | using StackExchange.Redis; 8 | 9 | namespace App 10 | { 11 | public class Startup 12 | { 13 | public Startup(IHostingEnvironment env) 14 | { 15 | var builder = new ConfigurationBuilder() 16 | .SetBasePath(env.ContentRootPath) 17 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 18 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 19 | .AddEnvironmentVariables(); 20 | Configuration = builder.Build(); 21 | } 22 | 23 | public IConfigurationRoot Configuration { get; } 24 | 25 | // This method gets called by the runtime. Use this method to add services to the container. 26 | public void ConfigureServices(IServiceCollection services) 27 | { 28 | // Add framework services. 29 | services.AddScoped(); 30 | services.AddMvc(); 31 | 32 | var connectionMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1"); 33 | var database = connectionMultiplexer.GetDatabase(0); 34 | services.AddScoped(_ => database); 35 | } 36 | 37 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 38 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 39 | { 40 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 41 | loggerFactory.AddDebug(); 42 | 43 | app.UseMvc(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/8.Redis/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/8.Redis/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/9.RabbitMQ/App/App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/9.RabbitMQ/App/Options/RabbitMqOptions.cs: -------------------------------------------------------------------------------- 1 | using RawRabbit.Configuration; 2 | 3 | namespace App.Options 4 | { 5 | public class RabbitMqOptions : RawRabbitConfiguration 6 | { 7 | } 8 | } -------------------------------------------------------------------------------- /src/9.RabbitMQ/App/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | 5 | namespace App 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | var host = new WebHostBuilder() 12 | .UseKestrel() 13 | .UseContentRoot(Directory.GetCurrentDirectory()) 14 | .UseIISIntegration() 15 | .UseStartup() 16 | .Build(); 17 | 18 | host.Run(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/9.RabbitMQ/App/Startup.cs: -------------------------------------------------------------------------------- 1 | using App.Options; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Logging; 7 | using RawRabbit; 8 | using RawRabbit.vNext; 9 | 10 | namespace App 11 | { 12 | public class Startup 13 | { 14 | public Startup(IHostingEnvironment env) 15 | { 16 | var builder = new ConfigurationBuilder() 17 | .SetBasePath(env.ContentRootPath) 18 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 19 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 20 | .AddEnvironmentVariables(); 21 | Configuration = builder.Build(); 22 | } 23 | 24 | public IConfigurationRoot Configuration { get; } 25 | 26 | // This method gets called by the runtime. Use this method to add services to the container. 27 | public void ConfigureServices(IServiceCollection services) 28 | { 29 | // Add framework services. 30 | services.AddMvc(); 31 | services.AddOptions(); 32 | 33 | var rabbitMqOptions = new RabbitMqOptions(); 34 | var rabbitMqOptionsSection = Configuration.GetSection("rabbitmq"); 35 | rabbitMqOptionsSection.Bind(rabbitMqOptions); 36 | var rabbitMqClient = BusClientFactory.CreateDefault(rabbitMqOptions); 37 | 38 | services.Configure(rabbitMqOptionsSection); 39 | services.AddScoped(_ => rabbitMqClient); 40 | } 41 | 42 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 43 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 44 | { 45 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 46 | loggerFactory.AddDebug(); 47 | 48 | app.UseMvc(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/9.RabbitMQ/App/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/9.RabbitMQ/App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | }, 8 | "rabbitmq": { 9 | "Username": "guest", 10 | "Password": "guest", 11 | "VirtualHost": "/", 12 | "Port": 5672, 13 | "Hostnames": [ "localhost" ], 14 | "RequestTimeout": "00:00:10", 15 | "PublishConfirmTimeout": "00:00:01", 16 | "RecoveryInterval": "00:00:10", 17 | "PersistentDeliveryMode": true, 18 | "AutoCloseConnection": true, 19 | "AutomaticRecovery": true, 20 | "TopologyRecovery": true, 21 | "Exchange": { 22 | "Durable": true, 23 | "AutoDelete": true, 24 | "Type": "Topic" 25 | }, 26 | "Queue": { 27 | "AutoDelete": true, 28 | "Durable": true, 29 | "Exclusive": true 30 | } 31 | } 32 | } 33 | --------------------------------------------------------------------------------