.NET Core Analysis

來源:互聯網
上載者:User

標籤:

.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?

  • Azure web apps
    In the project.json please include (appsettings.development.json, appsettings.staging.json, appsettings.production.json)
    {  "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


  • Azure cloud services
    One possible way would be to run prepublish or postpublic scripts/commands

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


 

.NET Core Analysis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.