Example #1
0
        public void ConfigureServices
            (Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            services.AddControllers();

            // Note:
            services.AddTransient
            <Microsoft.AspNetCore.Http.IHttpContextAccessor,
             Microsoft.AspNetCore.Http.HttpContextAccessor>();

            // Note:
            //services.AddTransient<Dtx.Logging.ILogger, Dtx.Logging.NLogAdapter>(); // Compile Error!

            //services.AddTransient<Dtx.Logging.ILogger<>, Dtx.Logging.NLogAdapter<>>(); // Compile Error!

            //services.AddSingleton
            //	(serviceType: typeof(Dtx.Logging.ILogger<>),
            //	implementationType: typeof(Dtx.Logging.NLogAdapter<>)); // Runtime Error!

            services.AddTransient
                (serviceType: typeof(Dtx.Logging.ILogger <>),
                implementationType: typeof(Dtx.Logging.NLogAdapter <>));

            //services.AddTransient
            //	(serviceType: typeof(Dtx.Logging.ILogger<>),
            //	implementationType: typeof(Dtx.Logging.Log4NetAdapter<>));
        }
Example #2
0
        public void ConfigureServices
            (Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            services.AddControllers();

            services.AddDbContext <Banking.Data.Context.DatabaseContext>(options =>
            {
                // UseSqlServer -> using Microsoft.EntityFrameworkCore;
                // GetConnectionString -> using Microsoft.Extensions.Configuration;
                options.UseSqlServer
                    (Configuration.GetConnectionString("BankingConnectionString"));
            });

            services.AddSwaggerGen(current =>
            {
                current.SwaggerDoc(name: "v1",
                                   info: new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Version = "v1",
                    Title   = "Banking Microservice",
                });
            });

            services.AddMediatR(handlerAssemblyMarkerTypes: typeof(Startup));

            Infrastructure.IoC.DependencyContainer.RegisterServices(services: services);
        }
        public void ConfigureServices
            (Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            services.AddControllers();

            // AddFluentValidation->Extension Method-> using FluentValidation.AspNetCore;
            //services.AddControllers()
            //	.AddFluentValidation(current =>
            //	{
            //		current.RegisterValidatorsFromAssemblyContaining
            //			<Application.LogsFeature.Commands.CreateLogCommandValidator>();

            //		current.LocalizationEnabled = true; // Default: [true]
            //		current.AutomaticValidationEnabled = true; // Default: [true]
            //		current.ImplicitlyValidateChildProperties = false; // Default: [false]
            //		current.ImplicitlyValidateRootCollectionElements = false; // Default: [false]
            //		current.RunDefaultMvcValidationAfterFluentValidationExecutes = false; // Default: [true]
            //	});

            Core.DependencyContainer.ConfigureServices
                (configuration: Configuration, services: services);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            services.AddControllers();


            // 添加MVC服务
            services.AddControllersWithViews();

            // 注册服务

            #region 依赖注入

            #region 注册方法注解
            // Singleton:单例服务,从当前服务容器中获取这个类型的实例永远是同一个实例;
            // Scoped:每个作用域生成周期内创建一个实例;
            // Transient:每一次请求服务都创建一个新实例;
            #endregion
            services.AddSingleton <ILogService, SysLogService>();

            //services.AddScoped<ILogService, SysLogService>();

            //services.AddTransient<ILogService, SysLogService>();

            services.AddTransient <IOperationTransient, Operation>();
            services.AddScoped <IOperationScoped, Operation>();
            services.AddSingleton <IOperationSingleton, Operation>();
            services.AddSingleton <IOperationSingletonInstance>(new Operation(Guid.Empty));
            services.AddTransient <OperationService, OperationService>();


            var serviceCollection = new ServiceCollection()
                                    .AddTransient <ILogService, SysLogService>()
                                    .AddSingleton <ILogService, SysLogService>()
                                    .AddScoped <ILogService, SysLogService>();
            #endregion
        }
Example #5
0
        public void ConfigureServices
            (Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            services.AddCors();
            services.AddControllers();

            // Configure strongly typed settings object
            services.Configure <Infrastructure.ApplicationSettings.Main>
                (Configuration.GetSection("AppSettings"));

            // Configure Swagger for application services
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Version        = "v1",
                    Title          = "Swagger Demo",
                    Description    = "Swagger Demo",
                    TermsOfService = new System.Uri("https://example.com/terms"),
                    Contact        = new Microsoft.OpenApi.Models.OpenApiContact()
                    {
                        Name  = "Mohsen Farokhi",
                        Email = "*****@*****.**",
                        Url   = new System.Uri("https://www.linkedin.com/in/mohsen-farokhi"),
                    },
                    License = new Microsoft.OpenApi.Models.OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url  = new System.Uri("https://example.com/license"),
                    }
                });

                c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme.",
                    Name        = "Authorization",
                    In          = Microsoft.OpenApi.Models.ParameterLocation.Header,
                    Type        = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });

                c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement()
                {
                    {
                        new Microsoft.OpenApi.Models.OpenApiSecurityScheme
                        {
                            Reference = new Microsoft.OpenApi.Models.OpenApiReference
                            {
                                Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = Microsoft.OpenApi.Models.ParameterLocation.Header,
                        },
                        new System.Collections.Generic.List <string>()
                    }
                });

                var xmlPath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "Application.xml");
                c.IncludeXmlComments(xmlPath);
            });

            // Configure DI for application services
            services.AddScoped <Services.IUserService, Services.UserService>();
        }
Example #6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            services.Configure <FormOptions>(o => {
                o.ValueLengthLimit         = int.MaxValue;
                o.MultipartBodyLengthLimit = int.MaxValue;
                o.MemoryBufferThreshold    = int.MaxValue;
            });


            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(
                                                             Configuration.GetConnectionString("MSSQL")));

            services.AddIdentity <ApplicationUser, IdentityRole>(cfg =>
            {
                cfg.User.RequireUniqueEmail = true;
            })
            .AddEntityFrameworkStores <ApplicationDbContext>();



            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddJwtBearer(cfg =>
            {
                cfg.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidIssuer      = this.Configuration["Tokens:Issuer"],
                    ValidAudience    = this.Configuration["Tokens:Audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(this.Configuration["Tokens:Key"]))
                };
            });


            services.Configure <IdentityOptions>(options =>
            {
                options.Password.RequiredLength         = 3;
                options.Password.RequireDigit           = false;
                options.Password.RequireLowercase       = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequiredUniqueChars    = 0;
            });

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder => builder.WithOrigins("http://www.taximiapi.com.aspbg.net/", "http://localhost:8100/")
                                  .AllowAnyHeader()
                                  .AllowAnyMethod()
                                  .AllowCredentials()
                                  .SetIsOriginAllowed((host) => true));
            });

            services.AddMvc();

            services.AddSignalR();

            services.AddControllers()
            .AddNewtonsoftJson(options =>
                               options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

            services.RegisterRepositoryServices();

            services.RegisterCloudinary(Configuration);

            services.Configure <MailSettings>(Configuration.GetSection("MailSettings"));

            services.RegisterCustomServices();
        }
Example #7
0
        public void ConfigureServices
            (Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            // Cross-Origin Resource Sharing (CORS)
            services.AddCors(options =>
            {
                options.AddPolicy(AdminCorsPolicy,
                                  builder =>
                {
                    builder
                    .WithOrigins("http://localhost:1220")
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    //.AllowCredentials()
                    ;
                });

                options.AddPolicy(OthersCorsPolicy,
                                  builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    //.AllowCredentials()
                    ;
                });
            });

            //services.AddControllers();

            services.AddControllers().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.MaxDepth             = 5;
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
            });

            //services.AddDbContext<Data.DatabaseContext>(options =>
            //{
            //	options.UseSqlServer
            //		(connectionString: "Password=1234512345;Persist Security Info=True;User ID=SA;Initial Catalog=DtxSecurity;Data Source=.");
            //});

            //services.AddDbContext<Data.DatabaseContext>(options =>
            //{
            //	options.UseSqlServer
            //		(connectionString: Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString");
            //});

            //services.AddTransient<Data.IUnitOfWork, Data.UnitOfWork>();

            services.AddTransient <Data.IUnitOfWork, Data.UnitOfWork>(sp =>
            {
                Data.Tools.Options options =
                    new Data.Tools.Options
                {
                    InMemoryDatabase = false,
                    ConnectionString =
                        Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value,
                };

                return(new Data.UnitOfWork(options: options));
            });
        }
Example #8
0
 public void ConfigureServices
     (Microsoft.Extensions.DependencyInjection.IServiceCollection services)
 {
     services.AddControllers();
 }
Example #9
0
        public void ConfigureServices
            (Microsoft.Extensions.DependencyInjection.IServiceCollection services)
        {
            // Cross-Origin Resource Sharing (CORS)
            services.AddCors(options =>
            {
                options.AddPolicy(ADMIN_CORS_POLICY,
                                  builder =>
                {
                    builder
                    .WithOrigins("http://localhost:5001")
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    //.AllowCredentials()
                    ;
                });

                options.AddPolicy(OTHERS_CORS_POLICY,
                                  builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    //.AllowCredentials()
                    ;
                });
            });

            //services.AddControllers();

            services.AddControllers().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.MaxDepth             = 5;
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
            });

            //services.AddDbContext<Data.DatabaseContext>(options =>
            //{
            //	options.UseSqlServer
            //		(connectionString: "Password=1234512345;Persist Security Info=True;User ID=SA;Initial Catalog=DtxSecurity;Data Source=.");
            //});

            //services.AddDbContext<Data.DatabaseContext>(options =>
            //{
            //	options.UseSqlServer
            //		(connectionString: Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString");
            //});

            //services.AddTransient<Data.IUnitOfWork, Data.UnitOfWork>();

            services.AddTransient <Data.IUnitOfWork, Data.UnitOfWork>(sp =>
            {
                Data.Tools.Options options =
                    new Data.Tools.Options
                {
                    Provider =
                        (Data.Tools.Enums.Provider)
                        System.Convert.ToInt32(Configuration.GetSection(key: "databaseProvider").Value),

                    //using Microsoft.EntityFrameworkCore;
                    //ConnectionString =
                    //	Configuration.GetConnectionString().GetSection(key: "MyConnectionString").Value,

                    ConnectionString =
                        Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value,
                };

                return(new Data.UnitOfWork(options: options));
            });

            //services.AddTransient<Dtx.ILogger, Dtx.Logger>();
        }