.NET Core Analysis
2016-10-13 21:44 zhangpengc 阅读(509) 评论(0) 编辑 收藏 举报.NET Core 1.0.1
Module | Component | .NET Core |
MongoDB | MongoDB.Driver | There has a nuget package available v2.3.0. |
Json | Newtonsoft.Json |
If you are working with Mvc, Newtonsoft.Json has been included by default. |
Logging | Logging |
public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<HomeController>(); } public IActionResult Index() { _logger.LogInformation("Index has been called"); return View(); } }
|
Logging | NLog.RabbitMQ | There are no nuget packages available, and in the library [MethodImpl(MethodImplOptions.Synchronized)] and Delegate.CreateDelegate are not supported by .NET Core also. |
Logging | NLog.Targets.ElasticSearch | There are no nuget packages available, but I created one myself. |
Mailing | Mandrill | There are no nuget packages availabe, but you can use SMTP or a small webjob without .NET Core. |
Azure Storage | WindowsAzure.Storage |
There has a nuget package available v7.2.1. BUT... https://github.com/Azure/azure-storage-net/blob/master/README.md#odata This version depends on three libraries (collectively referred to as ODataLib), which are resolved through the ODataLib (version 5.6.4) packages available through NuGet and not the WCF Data Services installer which currently contains 5.0.0 versions. The ODataLib libraries can be downloaded directly or referenced by your code project through NuGet. The specific ODataLib packages are:
|
Azure ServiceBus | WindowsAzure.ServiceBus | There are no nuget packages availabe. |
Identity | Microsoft.AspNet.Identity.Core | There has a nuget package available. |
Identity | Microsoft.AspNet.Identity.Owin | There has a nuget package available. |
Configuration (It's a big improvement for unit testing.) | Configuration |
appsettings.json { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } } } C# public class LoggingConfig { public bool IncludeScopes { get; set; } public LogLevelConfig LogLevel { get; set; } } public LogLevelConfig { public string Default { get; set; } public string System { get; set; } public string Microsoft { get; set; } } public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.Configure<LoggingConfig>(Configuration.GetSection("Logging")); services.AddMvc(); } } public class HomeController : Controller { public HomeController(IOptions<LoggingConfig> loggingConfig) { } } |
Configuration (Switch build configuration was a hell but not an ymore.) |
Configuration per environment |
You can copy appsettings.json per environment, e.g. appsettings.development.json, appsettings.staging.json, appsettings.production.json The default code template already support this see the below code: .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) Based on IHostingEnvironment you can do all the magic public class HomeController : Controller { public HomeController(IHostingEnvironment env) { var environmentName = env.EnvironmentName; var isDevelopment = env.IsDevelopment(); var isStaging = env.IsStaging(); var isProduction = env.IsProduction(); } } How to switch the environment? In the end the environment variables will be saved into launchSettings.json Based on the below command you can switch the environment easily dotnet run --environment "Staging"
How are we going to do with the automatically deployment?
You can add a slot setting via Azure portal see the below screenshot
|
IoC | Dependency injection |
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddTransient<ITransientService, TransientService>(); services.AddScoped<IScopedService, ScopedService>(); services.AddSingleton<ISingletonService, SingletonService>(); TransientTransient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services. ScopedScoped lifetime services are created once per request. SingletonSingleton lifetime services are created the first time they are requested (or when How to replace the default services container? public class Startup { // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType<TransientService>().As<ITransientService>().InstancePerDependency(); containerBuilder.RegisterType<ScopedService>().As<IScopedService>().InstancePerRequest(); containerBuilder.RegisterType<SingletonService>().As<ISingletonService>().SingleInstance(); containerBuilder.Populate(services); var container = containerBuilder.Build(); return new AutofacServiceProvider(container); } } |
Unit Tests | MSTest |
"MSTest.TestFramework": "1.0.5-preview" "dotnet-test-mstest": "1.1.1-preview" does not support .NET Core 1.0.1 yet |
Unit Tests | xUnit |
project.json { "version": "1.0.0-*", "testRunner": "xunit", "dependencies": { "xunit": "2.2.0-beta3-build3402", "dotnet-test-xunit": "2.2.0-preview2-build1029" }, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.1", "type": "platform" } } } } } |
Integration tests | Microsoft.AspNetCore.TestHost |
There has a nuget package available v1.0.0. |
Integration tests | Microsoft.AspNet.WebApi.Client |
There has a nuget package available v5.2.3. |
Globalization and localization |
https://docs.asp.net/en/latest/fundamentals/localization.html http://andrewlock.net/adding-localisation-to-an-asp-net-core-application (Very interesting even with a localized view) |