Exemplo n.º 1
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     IocRegister.AddRegistration(services, Configuration);
     SwaggerConfig.AddRegistration(services);
     CorsConfig.AddCorsOptions(services);
     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
 }
Exemplo n.º 2
0
        public void ServiceTest()
        {
            DapperContext.DatabaseConfigurationEventHandle += config =>
            {
                config.SqlProvider      = SqlProvider.MySQL;
                config.ConnectionString = "server=127.0.0.1;user id=root;password=1234;database=test;allowuservariables=True;SslMode=none;";
                config.RegisterUserId   = false;
            };

            IocRegister.RegisterEventHandler += builder =>
            {
                builder.RegisterGeneric(typeof(Repository <>)).As(typeof(IRepository <>));
            };
            IocRegister.Register("FrameworkDemo");

            var dbSession = IocManager.Resolve <IDbSession>();

            dbSession.Transaction(dt =>
            {
                dbSession.UserRepository.Add(new User()
                {
                    UserId = IdHelper.Instance.LongId,
                    Name   = "测试",
                    Age    = 18
                });
                dbSession.ScoreRepository.Add(new Score()
                {
                    Id        = IdHelper.Instance.LongId,
                    ScoreData = 22,
                    UserId    = IdHelper.Instance.LongId
                });
            });
        }
 /// <summary>
 /// Build and initialize
 /// </summary>
 public IServiceProvider Build(IServiceCollection services)
 {
     IocRegister.Register(UowOptions);
     IocRegister.Register(this);
     IocRegister.RegisterInterceptor <UnitOfWorkInterceptor>(
         implementationType =>
     {
         if (implementationType.IsDefined(typeof(UnitOfWorkAttribute), true))
         {
             return(true);
         }
         var methods = implementationType.GetMethods(
             BindingFlags.Instance |
             BindingFlags.Public |
             BindingFlags.NonPublic);
         if (methods.Any(m => m.IsDefined(typeof(UnitOfWorkAttribute), true)))
         {
             return(true);
         }
         if (UowOptions.ConventionalUowSelectors.Any(selector => selector(implementationType)))
         {
             return(true);
         }
         return(false);
     });
     IocRegister.RegisterAssemblyByBasicInterface(GetType().Assembly);
     return(IocRegister.GetServiceProvider(services));
 }
        /// <summary>
        /// The register.
        /// </summary>
        /// <param name="config">
        /// The config.
        /// </param>
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Enable cors
            var cors = new EnableCorsAttribute("*", "*", "*");

            config.EnableCors(cors);

            // Web API routes
            config.MapHttpAttributeRoutes();

            var container = new UnityContainer();

            IocRegister.RegisterAll(container);
            config.DependencyResolver = new UnityResolver(container);

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });

            config.Formatters.JsonFormatter
            .SerializerSettings
            .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        }
Exemplo n.º 5
0
        public void Setup()
        {
            DapperContext.DatabaseConfigurationEventHandle += config =>
            {
                config.SqlProvider      = SqlProvider.MySQL;
                config.ConnectionString = "server=127.0.0.1;user id=root;password=1234;database=test;allowuservariables=True;SslMode=none;";
                config.RegisterUserId   = false;
            };

            IocRegister.RegisterEventHandler += builder =>
            {
                builder.RegisterGeneric(typeof(Repository <>)).As(typeof(IRepository <>));
            };
            IocRegister.Register("IBaseFramework.NUnitTest");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Build and initialize
        /// </summary>
        public IServiceProvider Build(IServiceCollection services)
        {
            IocRegister.Register(this);
            IocRegister.Register(UowOptions);
            IocRegister.RegisterAssemblyByBasicInterface(this, GetType().Assembly);
            IocRegister.Register(typeof(IRepository <,>), typeof(EFRepositoryBase <,>), DependencyLifeStyle.Transient);
            IocRegister.RegisterInterceptor <UnitOfWorkInterceptor>(implementationType =>
            {
                var isUowNeeded = false;
                if (implementationType.IsDefined(typeof(UnitOfWorkAttribute), true))
                {
                    isUowNeeded = true;
                }
                else
                {
                    var methodInfos = implementationType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    isUowNeeded     = methodInfos.Any(m => m.IsDefined(typeof(UnitOfWorkAttribute), true)) ||
                                      UowOptions.ConventionalUowSelectors.Any(selector => selector(implementationType));
                }
                return(isUowNeeded);
            });

            services.Configure <LocalEventBusOptions>(x =>
            {
                LocalEventBusOptions.Handlers.ToList().ForEach(y =>
                {
                    if (!x.Handlers.Contains(y))
                    {
                        x.Handlers.Add(y);
                    }
                });
            });

            services.Configure <BackgroundJobOptions>(x =>
            {
                BackgroundJobOptions.GetJobs().ToList()
                .ForEach(y => x.TryAddJob(y));
            });

            return(IocRegister.GetServiceProvider(services));
        }
Exemplo n.º 7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add service and create Policy with options
            services.AddCors(options => {
                options.AddPolicy("AllowAll",
                                  builder => {
                    builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
            });
            IocRegister.RegisterServices(services);

            // Add framework services.
            services.AddMvc().AddJsonOptions(a => a.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());
            services.AddMvc().AddJsonOptions(a => a.SerializerSettings.Converters       = new List <JsonConverter> {
                new Utilities.DecimalConverter()
            });
            services.AddAuthorization(options =>
            {
                options.AddPolicy("UserRole", policy => policy.RequireClaim(CoreServiceConstants.IdentityClaims.LoginUserId));
                options.AddPolicy("AdminAdminRole", policy => policy.RequireClaim(CoreServiceConstants.IdentityClaims.IsAdmin));
            });

            services.Configure <WebCoreSettings>(Configuration.GetSection("CoreSettings"));

            // configure identity server with in-memory stores, keys, clients and resources
            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
            .AddInMemoryClients(IdentityServerConfig.GetClients());

            MongoDBContext.ConnectionString = Configuration.GetSection("CoreSettings:ConnectionString").Value;
            MongoDBContext.DatabaseName     = Configuration.GetSection("CoreSettings:DatabaseName").Value;
            MongoDBContext.IsSSL            = Convert.ToBoolean(Configuration.GetSection("CoreSettings:IsSSL").Value);



            services.AddAutoMapper();
        }