.NET Core 1.0.1
1. MongoDB.Driver
There has a nuget package available 2.3.0
2. Configuration
e.g. appsettings.json
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } } }
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) { } }
Look very nice, a strong type configuration :)
3. Configuration per envionemnt (appsettings.development.json, appsettings.staging.json, appsettings.production.json)
public class HomeController : Controller { public HomeController(IHostingEnvironment env) { var environmentName = env.EnvironmentName; var isDevelopment = env.IsDevelopment(); var isStaging = env.IsStaging(); var isProduction = env.IsProduction(); } }
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"
I really want to thank Microsoft save a lot of my time (Switching a build configuration was a hell).
How are we going to do with the deployment?
{ "publishOptions": { "include": [ "wwwroot", "**/*.cshtml", "appsettings.json", "appsettings.Development.json", "appsettings.Staging.json", "appsettings.Production.json", "web.config" ] }
You can add a slot setting via Azure portal see the below screenshot
4. 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>(); services.AddMvc(); } }
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 whenConfigureServices
is run if you specify an instance there) and then every subsequent request will use the same instance. If your application requires singleton behavior, allowing the services container to manage the service’s lifetime is recommended instead of implementing the singleton design pattern and managing your object’s lifetime in the class yourself.
5. Replacing 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); } }
6. Newtonsoft.Json
If you are working with Mvc, Newtonsoft.Json has been included by default.
7. 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(); } }
8. NLog.RabbitMQ
Does not support .NET Core yet, in the Library [MethodImpl(MethodImplOptions.Synchronized)] and Delegate.CreateDelegate are not supported by .NET Core also.
9. NLog.Targets.ElasticSearch
I created one seem fine :)
10. Mandrill
Does not support .NET Core yet, we can run a small task without .NET Core.
11. Azure storage (Blob storages, Queues, ...)
There has a nuget package "WindowsAzure.Storage": "7.2.1"
12. Azure service bus
Does not support .NET Core yet
原文:http://www.cnblogs.com/zhangpengc/p/5934687.html