using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using FineUICore;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Microsoft.Extensions.FileProviders;
using System.IO;
using Model; namespace Som
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} // This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
services.AddDistributedMemoryCache();
services.AddSession(); // FineUI 和 MVC 服务
services.AddFineUI(Configuration);
services.AddMvc(options =>
{
// 自定义模型绑定(Newtonsoft.Json)
options.ModelBinderProviders.Insert(0, new JsonModelBinderProvider());
});
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";//将日期格式序列化
}); //添加dbcontext服务,可以实现依赖注入
services.AddDbContext<SomDbContext>(options =>
{
/*
[
"SomConfig":{
"DataBase": "部署库",
"Port": "8090"
},
"ConnectionStrings":{
"DefaultConnection":"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"
}
]
*/
var connStr=Configuration["ConnectionStrings:DefaultConnection"];//是写在Config.json文件里的 : 冒号表示 获取到第二层
var connstr2=Configuration.GetConnectionString("DefaultConnection");//另一种获取方法 自动去Config.json文件里找ConnectionStrings属性
optionsBuilder.UseSqlServer(connstr2);
}); //添加认证Cookie信息
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/login");
options.Cookie.HttpOnly = true;
}); services.AddAntiforgery(options =>
{
options.SuppressXFrameOptionsHeader = true;
}); services.AddSingleton<IWelcomeService,WelcomService>();//自定义的接口 和 实现该接口的类 Singleton单列 只生成一个
services.AddScoped<IEFManagerService<Student>,EFStrudentService>();//自定义的接口 和 实现该接口的类 Scoped 每个http请求生成一个实例 } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//跨域支持
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseSignalR(u => u.MapHub<Chat>("/chathub"));
// 静态资源中间件
app.UseStaticFiles(new StaticFileOptions
{ //设置不限制content-type
ServeUnknownFileTypes = true
});
app.UseSession();
//验证中间件
app.UseAuthentication(); // FineUI 和 MVC 中间件(确保 UseFineUI 位于 UseMvc 的前面)
app.UseFineUI(); if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Login/Error");
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "area",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}

  

下面是利用依赖注入 在构造函数里直接得到类的实例或实现了接口的实例

public class EFStrudentService
{
private readonly SomDbContext _efDbContext;
public EFManageStrudent(SomDbContext context)
{
//这里实现了依赖注入
this._efDbContext = context;
}
public void AddStudent(Student s)
{
_efDbContext.Add(s);
_efDbContext.save();
}
}

最新文章

  1. 解决问题:无法对 System程序集 添加Fakes程序集
  2. HTML-如何让自己的网页标题处可以显示网站的logo?
  3. The The Garbage-First (G1) collector since Oracle JDK 7 update 4 and later releases
  4. uC/OS-II配置文件
  5. CSS设置技巧
  6. html添加keyword,description帮助百度收录处理方法,jsp去除空白行方法
  7. MYSQL参数学习---------------- 张碧池
  8. iOS开发进阶-实现多线程的3种方法
  9. 省市区三级联动(jquery+ajax)(封装和不封装两种方式)-----2017-05-15
  10. 20165226 2017-2018-3 《Java程序设计》第5学习总结
  11. Jump跳板机的搭建和部署
  12. CF939D Love Rescue 并查集
  13. 2017-2018-2 20165327 实验四《Android程序设计》实验报告
  14. Postgresql ERROR: permission denied for relation app_info
  15. Java动态代理的两种实现方法
  16. 使用@SpringBootApplication注解
  17. at com.mysql.jdbc.SQLError.createSQLException
  18. Spark DataFrame vector 类型存储到Hive表
  19. 解题:POI 2007 Driving Exam
  20. 洛谷P2525 Uim的情人节礼物&#183;其之壱 [康托展开]

热门文章

  1. FreeMarker简单入门到使用
  2. javafx教程大全
  3. Linux命令(2):cat
  4. 2. Vim 概念扫盲
  5. 用python, PIL在图像上添加文字(可以控制,调节为水印等)
  6. pytong下安装安装SK-Learn
  7. 【CF1215F】 Radio Stations
  8. WinCE下的第二个窗口程序
  9. DLL和OCX注册
  10. websokect的原理