예제 #1
0
        protected void Application_Start()
        {
            #region Autofac在MVC中注册
            ContainerBuilder builder = new ContainerBuilder();
            var        service       = Assembly.Load("IService");
            var        service1      = Assembly.Load("Service");
            var        service2      = Assembly.Load("Model");
            Assembly[] assemblyArr   = new Assembly[] { service, service1, service2 };
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyTypes(assemblyArr).AsImplementedInterfaces();
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            #endregion

            DbContext db = new MyContext();
            if (db.Database.CreateIfNotExists())
            {
                //产品表
                IProductAdminService productSerivce = new ProductAdminService();
                List <Product>       productList    = new List <Product>()
                {
                    new Product()
                    {
                        Category = 1, ImagePath = "../image", MarketTime = DateTime.Now, Number = 30, Name = "可口可乐", Price = 2.5, ProduceTime = DateTime.Now, ProtectTime = 12, Status = 1
                    },
                    new Product()
                    {
                        Category = 1, ImagePath = "../image", MarketTime = DateTime.Now, Number = 30, Name = "雪碧", Price = 2.5, ProduceTime = DateTime.Now, ProtectTime = 12, Status = 1
                    }
                };
                productSerivce.AddRange(productList);

                //机器表
                IMachineAdminService machineService = new MachineAdminService();
                List <Machine>       machineList    = new List <Machine>()
                {
                    new Machine()
                    {
                        Address = "雨花台软件大道", Code = "JQ0001", MaintainTime = DateTime.Now, MarketTime = DateTime.Now
                    },
                    new Machine()
                    {
                        Address = "玄武区孝陵卫", Code = "JQ0002", MaintainTime = DateTime.Now, MarketTime = DateTime.Now
                    }
                };
                machineService.AddRange(machineList);

                //权限
                IAuthorityService authorityService = new AuthorityAdminService();
                List <Authority>  authorityList    = new List <Authority>()
                {
                    new Authority()
                    {
                        Id = 1, BuildTime = DateTime.Now, Description = "测试", Name = "R&W", Status = 0, Type = 0, UpdateTime = DateTime.Now, Roles = new List <Role>()
                    },
                    new Authority()
                    {
                        Id = 2, BuildTime = DateTime.Now, Description = "测试1", Name = "W", Status = 0, Type = 0, UpdateTime = DateTime.Now, Roles = new List <Role>()
                    }
                };
                authorityService.AddRange(authorityList);

                Authority authority1 = new Authority();
                authority1 = authorityService.GetList(s => s.Id == 1).FirstOrDefault();
                Authority authority2 = new Authority();
                authority2 = authorityService.GetList(s => s.Id == 2).FirstOrDefault();

                //角色
                IRoleAdminService roleService = new RoleAdminService();
                List <Role>       roleList    = new List <Role>()
                {
                    new Role()
                    {
                        Id = 1, BuildTime = DateTime.Now, Description = "测试1", RoleName = "测试1", Status = 0, UpateTime = DateTime.Now, Authoritys = new List <Authority>()
                    },
                    new Role()
                    {
                        Id = 2, BuildTime = DateTime.Now, Description = "测试2", RoleName = "测试2", Status = 0, UpateTime = DateTime.Now, Authoritys = new List <Authority>()
                    }
                };
                roleService.AddRange(roleList);

                Role role1 = new Role();
                role1 = roleService.GetList(s => s.Id == 1).FirstOrDefault();
                Role role2 = new Role();
                role2 = roleService.GetList(s => s.Id == 2).FirstOrDefault();

                role1.Authoritys.Add(authority1);
                role2.Authoritys.Add(authority2);
                //用户表
                IUserAdminService userService = new UserAdminService();
                List <User>       userList    = new List <User>()
                {
                    new User()
                    {
                        ID = 1, NickName = "薄荷", Password = "******", EMail = "*****@*****.**", Role = role1, RoleID = role1.Id, Status = 0, LoginTime = DateTime.Now, Count = 0, BuildTime = DateTime.Now, UpdateTime = DateTime.Now
                    },
                    new User()
                    {
                        ID = 2, NickName = "少年", Password = "******", EMail = "*****@*****.**", Role = role2, RoleID = role2.Id, Status = 0, LoginTime = DateTime.Now, Count = 0, BuildTime = DateTime.Now, UpdateTime = DateTime.Now
                    }
                };
                userService.AddRange(userList);
            }

            //log4net.Config.XmlConfigurator.Configure();//读取Log4Net配置信息

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);



            //MiniProfilerEF6.Initialize();//注册MiniProfiler,网页性能插件
            log4net.Config.XmlConfigurator.Configure();
            //WaitCallback
            ThreadPool.QueueUserWorkItem((a) =>
            {
                while (true)
                {
                    if (MyExceptionAttribute.ExceptionQueue.Count > 0)
                    {
                        Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue();//出队
                        //string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                        //File.AppendAllText(Path.Combine("App_Data", fileName), ex.ToString(), System.Text.Encoding.Default);
                        //ILog logger = LogManager.GetLogger("errorMsg");
                        ILog logger = log4net.LogManager.GetLogger("logger");
                        logger.Error(ex.ToString());

                        #region 发送邮件
                        //MailHelper mail = new MailHelper();
                        //mail.MailServer = "smtp.qq.com";
                        //mail.MailboxName = "*****@*****.**";
                        //mail.MailboxPassword = "******";//开启QQ邮箱POP3/SMTP服务时给的授权码
                        ////操作打开QQ邮箱->在账号下方点击"设置"->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
                        ////obxxsfowztbideee为2872845261@qq的授权码
                        //mail.MailName = "Error";
                        //try
                        //{
                        //    mail.Send("*****@*****.**", "Error", ex.ToString());
                        //}
                        //catch
                        //{ }
                        #endregion
                    }
                    else
                    {
                        Thread.Sleep(3000);//如果队列中没有数据,则休息为了避免占用CPU的资源.
                    }
                }
            });
        }
예제 #2
0
        protected void Application_Start()
        {
            DbContext db = new MyContext();

            if (db.Database.CreateIfNotExists())
            {
                IProductAdminService productservice = new ProductAdminService();
                List <ProductModel>  productlist    = new List <ProductModel>()
                {
                    new ProductModel()
                    {
                        Id = 1, itemid = "1", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 2, itemid = "2", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 3, itemid = "3", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 4, itemid = "4", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 5, itemid = "5", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 6, itemid = "6", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 7, itemid = "7", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 8, itemid = "8", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 9, itemid = "9", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 10, itemid = "10", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 11, itemid = "11", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 12, itemid = "12", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 13, itemid = "13", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 14, itemid = "14", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 15, itemid = "15", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 16, itemid = "16", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 17, itemid = "17", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 18, itemid = "18", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                    new ProductModel()
                    {
                        Id = 19, itemid = "19", productid = "1", listprice = "1", Status = 1, attr1 = "1", unitcost = "1"
                    },
                };
                productservice.AddRange(productlist);
            }
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            #region Autofac在MVC中注册
            ContainerBuilder builder = new ContainerBuilder();
            var        service       = Assembly.Load("IService");
            var        service1      = Assembly.Load("Service");
            var        service2      = Assembly.Load("Model");
            Assembly[] assemblyArr   = new Assembly[] { service, service1, service2 };
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyTypes(assemblyArr).AsImplementedInterfaces();
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            #endregion

            //log4net.Config.XmlConfigurator.Configure();//读取Log4Net配置信息

            //MiniProfilerEF6.Initialize();//注册MiniProfiler,网页性能插件

            log4net.Config.XmlConfigurator.Configure();

            //WaitCallback
            ThreadPool.QueueUserWorkItem((a) =>
            {
                while (true)
                {
                    if (MyExceptionAttribute.ExceptionQueue.Count > 0)
                    {
                        Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue();//出队
                        //string fileName = DateTime.Now.ToString("yyyy-MM-dd")+".txt";
                        //File.AppendAllText(fileLogPath + fileName, ex.ToString(), System.Text.Encoding.Default);
                        //ILog logger = LogManager.GetLogger("errorMsg");
                        ILog logger = log4net.LogManager.GetLogger("logger");
                        logger.Error(ex.ToString());

                        #region 发送邮件
                        //MailHelper mail = new MailHelper();
                        //mail.MailServer = "smtp.qq.com";
                        //mail.MailboxName = "*****@*****.**";
                        //mail.MailboxPassword = "******";//开启QQ邮箱POP3/SMTP服务时给的授权码
                        ////操作打开QQ邮箱->在账号下方点击"设置"->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
                        ////obxxsfowztbideee为2872845261@qq的授权码
                        //mail.MailName = "Error";
                        //try
                        //{
                        //    mail.Send("*****@*****.**", "Error", ex.ToString());
                        //}
                        //catch
                        //{ }
                        #endregion
                    }
                    else
                    {
                        Thread.Sleep(3000);//如果队列中没有数据,则休息为了避免占用CPU的资源.
                    }
                }
            });
        }