首页 > Web开发 > 详细

.NET Core Analysis

时间:2016-10-07 01:51:49      阅读:412      评论:0      收藏:0      [点我收藏+]

.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

原文:http://www.cnblogs.com/zhangpengc/p/5934687.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!