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

Startup里面的一些用法的更多相关文章

  1. Solr学习总结(五)SolrNet的基本用法及CURD

    上一篇已经讲到了Solr 查询的相关的参数.这里在讲讲C#是如何通过客户端请求和接受solr服务器的数据, 这里推荐使用SolrNet,主要是:SolrNet使用非常方便,而且用户众多,一直都在更新, ...

  2. 为什么需要main函数,及其参数的用法

    首先,需要明确main函数是什么? 答:main函数是C语言约定的入口函数 C99标准里面是这样描述的: Program startup The function called at program ...

  3. oradmin相关用法

    [转]oradmin相关用法 创建例程: -NEW -SID sid | -SRVC 服务 [-INTPWD 口令] [-MAXUSERS 数量] [-STARTMODE a|m] [-PFILE 文 ...

  4. WPF中log4net的用法

    WPF中如何使用log4nethttp://www.cnblogs.com/C-Sharp2/archive/2013/04/12/WPF-LOG4NET.html Apache log4net Ma ...

  5. JDK之jstat的用法

    http://www.51testing.com/html/92/77492-203728.html jstat的用法 用以判断JVM是否存在内存问题呢?如何判断JVM垃圾回收是否正常?一般的top指 ...

  6. MongoDB高级查询用法大全

    转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...

  7. Shell脚本中让进程休眠的方法(sleep用法)

    有时候写Shell的脚本,用于顺序执行一系列的程序. 有些程序在停止之后并没能立即退出,就例如有一个 tomcat 挂了,就算是用 kill -9 命令也还没瞬间就结束掉. 这么如果 shell 还没 ...

  8. 关于Google Chrome 浏览器的一些命令及用法

    http://blog.csdn.net/zyz511919766/article/details/7356306 一些Chrome的地址栏命令(这些命令会不停的变动,所有不一定都是好用的) 在Chr ...

  9. printk的用法

    printk的用法 内核通过 printk() 输出的信息具有日志级别,日志级别是通过在 printk() 输出的字符串前加一个带尖括号的整数来控制的,如 printk("<6> ...

随机推荐

  1. Download Kali Linux

    https://www.kali.org/downloads/

  2. Android下载Android源码

    使用Git,命令是:git clone http://android.googlesource.com/platform/frameworks/base.git

  3. 字符串匹配dp+bitset,滚动数组优化——hdu5745(经典)

    bitset的经典优化,即把可行性01数组的转移代价降低 bitset的适用情况,当内层状态只和外层状态的上一个状态相关,并且内层状态的相关距离是一个固定的数,可用bitset,换言之,能用滚动数组是 ...

  4. NX二次开发-设置对象名称函数UF_OBJ_set_name

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize( ...

  5. ionic-CSS:ionic 单选框

    ylbtech-ionic-CSS:ionic 单选框 1.返回顶部 1. ionic 单选框 ionic 单选按钮与标准 type="radio" 的 input元素类似.以下展 ...

  6. 洛谷P3916 图的遍历

    题目链接:https://www.luogu.org/problemnew/show/P3916 题目大意 略. 分析 以终为始,逆向思维. 代码如下 #include <bits/stdc++ ...

  7. windows下装LINUX后,进不了系统

    在网上找了一款叫"DisckGenius"的软件,运行后选“硬盘”/“重建主引导记录(MBR)”,然后重启,就正常了. 还有系统盘最好是FAT32格式的.

  8. Java学习之JVM、JRE、JDK联系与区别

    JVM,全称是Java Virtual Machine,翻译为Java虚拟机: JRE,全称是Java Runtime Environment,翻译为Java运行时环境: JDK,全称是Java De ...

  9. 哈理工赛 H-小乐乐学数学 /// 筛法得素数表+树状数组

    题目大意: 给定n个数 m个询问 询问l r区间内的孤独数的个数 孤独数的定义为在该区间内与其他所有数互质的数 看注释 #include <bits/stdc++.h> using nam ...

  10. Hadoop的局限与不足