HTML5技术

.NET跨平台之旅:升级至ASP.NET 5 RC1,Linux上访问SQL Server数据库 - 博客园团队

字号+ 作者:H5之家 来源:H5之家 2015-11-20 10:47 我要评论( )

今天微软正式发布了ASP.NET 5 RC1(详见Announcing ASP.NET 5 Release Candidate 1),.NET跨平台迈出了关键一步。 紧跟这次RC1的发布,我们成功地将运行在Linux上的示例站点()升级到了ASP.NET 5 RC1,并且增加了数据库访问功能基于Entity Framework 7 RC1

今天微软正式发布了ASP.NET 5 RC1(详见Announcing ASP.NET 5 Release Candidate 1),.NET跨平台迈出了关键一步。

紧跟这次RC1的发布,我们成功地将运行在Linux上的示例站点()升级到了ASP.NET 5 RC1,并且增加了数据库访问功能——基于Entity Framework 7 RC1访问SQL Server数据库。

示例站点页面左侧的导航是从数据库读取数据动态加载的,数据库服务器用的是阿里云RDS(注:创建数据库时需要将支持的字符集设置为SQL_Latin1_General_CP1_CS_AS,这是针对SqlClient中一个bug的临时解决方法)。数据库表是通过EF迁移功能生成的,所用命令如下:

dnx ef migrations add FirstMigration dnx ef database update

数据库连接字符串是从config.json中读取的。

后端Web服务器用的是kestrel,前端Web服务器用的是阿里云负载均衡,使用中发现一个很奇怪的问题:浏览器直接访问kestrel,速度飞快;而访问阿里云负载均衡,页面虽然显示出来,但页面一直牌加载状态,长达1分钟。

怀疑是阿里云负载均衡与kestrel在TCP通信上存在某些问题,这个问题暂时没有找到解决方法。

SQL Server数据库终于能跨平台访问了,接下来就看kestrel的稳定性了。如果kestrel稳定,我们就开始将一些实际使用的小站点迁移至ASP.NET 5,并部署在Linux服务器上。

下面分享一下这个示例ASP.NET 5站点的主要代码。

文件结构:

. ├── config.json ├── Controllers │   ├── AboutController.cs │   └── HomeController.cs ├── Data │   ├── EfDbContext.cs │   ├── ITabNavRepository.cs │   └── TabNavRepository.cs ├── Extensions │   └── HtmlHelperExtensions.cs ├── Models │   └── TabNav.cs ├── project.json ├── project.lock.json ├── Startup.cs ├── Views │   ├── About │   │   ├── Ad.cshtml │   │   ├── Contact.cshtml │   │   ├── Intro.cshtml │   │   └── Job.cshtml │   ├── Shared │   │   └── _Layout.cshtml │   └── _ViewStart.cshtml └── wwwroot

project.json文件的内容:

{ "webroot": "wwwroot", "exclude": ["wwwroot"], "commands":{ "kestrel": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls *:8001", "ef": "EntityFramework.Commands" }, "dependencies":{ "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", "Microsoft.AspNet.Mvc": "6.0.0-*", "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final", "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", "EntityFramework.Commands": "7.0.0-rc1-final", "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final", "System.Runtime.Serialization.Primitives": "4.0.10-*", "System.Net.Security":"4.0.0-*" }, "frameworks":{ "dnxcore50": {}, } }

Startup.cs中的代码:

using System; using System.Linq; using Microsoft.AspNet.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Data.Entity; using CNBlogs.AboutUs.Data; using Microsoft.Dnx.Runtime; using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.Configuration; using System.Data.SqlClient; using Microsoft.Extensions.Logging; namespace CNBlogs.AboutUs.Web { public class Startup { public Startup(IApplicationEnvironment appEnv) { IConfigurationBuilder builder = new ConfigurationBuilder() .SetBasePath(appEnv.ApplicationBasePath) .AddJsonFile(, false); Configuration = builder.Build(); } public IConfiguration Configuration { get; set; } public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); app.UseDeveloperExceptionPage(); app.UseMvcWithDefaultRoute(); app.UseStaticFiles(); app.UseRuntimeInfoPage(); } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddEntityFramework() .AddSqlServer() .AddDbContext<EfDbContext>(options => { options.UseSqlServer(Configuration[]); }); services.AddTransient<ITabNavRepository, TabNavRepository>(); } } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Dora.Interception: 一个为.NET Core度身定制的AOP框架 - Artech

    Dora.Interception: 一个为.NET Core度身定制的AOP框架 - Artech

    2017-05-02 11:00

  • 如何在 ASP.NET Core 中发送邮件 - Savorboard

    如何在 ASP.NET Core 中发送邮件 - Savorboard

    2017-05-02 08:02

  • 十二个 ASP.NET Core 例子 - Savorboard

    十二个 ASP.NET Core 例子 - Savorboard

    2017-04-27 16:01

  • ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    2017-04-24 08:04

网友点评
s