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 StartupIConfiguration 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 ConfigureServicesIServiceCollection services) { services.AddSignalR); services.AddDistributedMemoryCache); services.AddSession); // FineUI 和 MVC 服务 services.AddFineUIConfiguration); services.AddMvcoptions => { // 自定义模型绑定(Newtonsoft.Json) options.ModelBinderProviders.Insert0, new JsonModelBinderProvider)); }); services.AddMvc).AddJsonOptionsoptions => { 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.UseSqlServerconnstr2); }); //添加认证Cookie信息 services.AddAuthenticationCookieAuthenticationDefaults.AuthenticationScheme) .AddCookieoptions => { options.LoginPath = new PathString"/login"); options.Cookie.HttpOnly = true; }); services.AddAntiforgeryoptions => { 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 ConfigureIApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { //跨域支持 app.UseCorsbuilder => builder.AllowAnyOrigin).AllowAnyMethod).AllowAnyHeader).AllowCredentials)); app.UseSignalRu => u.MapHub<Chat>"/chathub")); // 静态资源中间件 app.UseStaticFilesnew 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.UseMvcroutes => { 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 EFManageStrudentSomDbContext context)
{
//这里实现了依赖注入
this._efDbContext = context;
}
public void AddStudentStudent s)
{
_efDbContext.Adds);
_efDbContext.save);
}
}