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. VS 2019企业版激活码

    Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DF

  2. 兼容火狐浏览器的select下拉框样式

    经常遇到这样的问题,在使用bootstrap框架的时候select选择框的样式在火狐浏览器上继承的是浏览器本身的样式,跟谷歌等其他的浏览器样式对比很难看,并且很难调整,但是! 好东西来了,现在能调成跟 ...

  3. prop不同数据类型设置默认值

    vue prop 会接收不同的数据类型,这里列出了 常用的数据类型的设置默认值的写法,其中包含: Number, String, Boolean, Array,  Function, Object   ...

  4. ggplot2在一幅图上画两条曲线

    ggplot2在一幅图上画两条曲线 print(data)后的结果是 C BROWN.P MI.P 0 0.9216 0.9282 30 0.9240 0.9282 100 0.9255 0.9282 ...

  5. 在WinDBG中查看内存的命令

    当我们在调试器中分析问题时, 经常需要查看不同内存块的内容以分析产生的原因, 并且在随后验证所做出的假设是否正确. 由于各个对象的状态都是保存在内存中的, 因此内存的内容也就相当于对象的状态. d命令 ...

  6. ionic-CSS:ionic 单选框

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

  7. class12_pack_grid_place 放置位置

    其中的部分运行效果图(程序见序号1): #!/usr/bin/env python# -*- coding:utf-8 -*-# ----------------------------------- ...

  8. 20140312 Excel表格画折现图次坐标轴

    1.struct 是8字节对齐 2.char * a, sizeof(a)=4; char a[]="abcdefg";sizeof(a)=8; 3.内存对其齐

  9. ThreadPoolTaskExecutor的配置使用

    版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/ft305977550/article/de ...

  10. 网页开发人员收藏的16款HTML5工具

    本文收集的20款优秀的 HTML5 Web 应用程序,值得添加到您的 HTML5 的工具箱中,他们能够帮助你开发前端项目更快.更容易. Initializr Initializr 是一个可以让你创建 ...