예제 #1
0
        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CoreWebSite>(Configuration.GetSection("CoreWebSite"));
            services.Configure <CoreUpload>(Configuration.GetSection("CoreUpload"));
            services.AddCommonService()
            .AddRepositoryService()
            .AddDomainService();
            var assmebies = new System.IO.DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles("Core.Application*.dll")
                            .Select(x => System.Reflection.Assembly.LoadFrom(x.FullName)).ToArray();

            services.AddMvc(option =>
            {
            }).AddApplicationPart(assmebies.FirstOrDefault())
            .AddRazorOptions(options =>
            {
                options.AreaViewLocationFormats.Add("/Core/Admin/{1}/{0}.cshtml");
                options.AreaViewLocationFormats.Add("/Core/Admin/{0}.cshtml");
                options.AreaViewLocationFormats.Add("/Core/Admin/Shared/{0}.cshtml");

                options.ViewLocationFormats.Add("/Core/Mobile/{1}/{0}.cshtml");
                options.ViewLocationFormats.Add("/Core/Mobile/{0}.cshtml");
                options.ViewLocationFormats.Add("/Core/Mobile/Shared/{0}.cshtml");
            }).AddJsonOptions(option =>
            {
                option.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
            });
            services.AddDbContext <BaseDbContext>(option =>
            {
                var connectionString = Configuration.GetConnectionString("Core");
                option.UseSqlServer(connectionString, b => b.UseRowNumberForPaging());//分页增加对sqlserver2008的支持
                var logFactory = new LoggerFactory();
                logFactory.AddLog4Net();
                option.UseLoggerFactory(logFactory);                         //添加sql监控日志
            });                                                              //初始化数据库连接
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //添加对象映射组件
            services.AddHttpContextAccessor();
            services.AddAuthentication(option =>
            {
                option.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                option.RequireAuthenticatedSignIn = false;
            }).AddCookie(option =>
            {
                option.LoginPath          = "/User/Login";
                option.LogoutPath         = "/User/LoginOut";
                option.Events.OnSigningIn = (context) =>
                {
                    var cookieName = context.Options.Cookie.Name;
                    return(Task.CompletedTask);
                };
            });
            services.AddSession();
            if (WebHostEnvironment.IsDevelopment())
            {
                services.AddMemoryCache();
            }
            else
            {
                services.AddDistributedRedisCache(option =>
                {
                    option.Configuration = "127.0.0.1";
                    option.InstanceName  = "db0";
                });
            }
            services.AddUEditorService();
            services.AddDapper("sqlserver", option =>
            {
                option.ConnectionString = Configuration.GetConnectionString("Core");
                option.DBType           = CoreEnum.DBType.SqlServer;
            });
            ServiceCollection = services;
        }