设为首页 收藏本站
查看: 1151|回复: 0

[经验分享] .NET Core Analysis

[复制链接]

尚未签到

发表于 2017-7-1 10:08:16 | 显示全部楼层 |阅读模式
  .NET Core 1.0.1

ModuleComponent.NET Core
MongoDBMongoDB.DriverThere has a nuget package available v2.3.0.
JsonNewtonsoft.Json  If you are working with Mvc, Newtonsoft.Json has been included by default.
DSC0000.png


LoggingLogging


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();
}
}

LoggingNLog.RabbitMQThere are no nuget packages available, and in the library [MethodImpl(MethodImplOptions.Synchronized)] and Delegate.CreateDelegate are not supported by .NET Core also.
LoggingNLog.Targets.ElasticSearchThere are no nuget packages available, but I created one myself.
MailingMandrillThere are no nuget packages availabe, but you can use SMTP or a small webjob without .NET Core.
Azure StorageWindowsAzure.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:


  • Microsoft.Data.OData
  • Microsoft.Data.Edm
  • System.Spatial

  Note: The ODataLib packages currently do not support "netstandard1.6" or "netcoreapp1.0" frameworks in projects depending on the current relase of Dotnet CoreCLR. Thus, you may encounter failures while trying to restore the ODataLib dependencies for one of the targeted frameworks mentioned above. Until the support is added, if you run into this, you can use the imports statement within the framework node of your project.json file to specify to NuGet that it can restore the packages targeting the framework within the "imports" statement as shown below:


  "imports": [
"dnxcore50",
"portable-net451+win8"
]
Azure ServiceBusWindowsAzure.ServiceBusThere are no nuget packages availabe.
IdentityMicrosoft.AspNet.Identity.CoreThere has a nuget package available.
IdentityMicrosoft.AspNet.Identity.OwinThere 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?
DSC0001.png

  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?


  • 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
DSC0002.png



  • Azure cloud services
    One possible way would be to run prepublish or postpublic scripts/commands
IoCDependency 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.
  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 testsMicrosoft.AspNetCore.TestHost  There has a nuget package available v1.0.0.

Integration testsMicrosoft.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)


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389943-1-1.html 上篇帖子: Machine Learning Library (MLlib) Guide, BOOKS 下篇帖子: 全世界云计算宕机和中断[2013年-2014年集锦]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表