public void TestInjectorConstructorNotNull() { Injector.Clear(); // define static user in injector var definedUser = new User("John", "Smith"); Injector.Register <IUser>(definedUser); // compare user from injector to original var viewModel = InjectionResolver.InjectConstructor <CustomViewModel>(Injector); Assert.IsNotNull(viewModel); }
public void TestInjectorConstructorDependenciesEqualsDefined() { Injector.Clear(); // define static user in injector var definedUser = new User("John", "Smith"); Injector.Register <IUser>(definedUser); // compare user from injector to original var viewModel = InjectionResolver.InjectConstructor <CustomViewModel>(Injector); Assert.AreEqual(viewModel.User, definedUser); }
// 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 ConfigureServices(IServiceCollection services) { string projectPath = Directory.GetCurrentDirectory() + "\\Properties"; var configuration = new ConfigurationBuilder() .SetBasePath(projectPath) .AddJsonFile("appSettings.json") .Build(); string connectionString = configuration.GetConnectionString("DefaultConnection"); InjectionResolver.ConfigurateInjections(services, connectionString); services.AddCors(); services.AddMvc().AddJsonOptions(option => option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1", Title = "GameStore", Description = "ASP.NET Core Web API" }); }); services.AddSwaggerGen(); services.AddMvc( config => { config.Filters.Add(new ActionFilters()); }); services.ConfigureSwaggerGen(options => { var basePath = PlatformServices.Default.Application.ApplicationBasePath; var xmlPath = Path.Combine(basePath, "GameStore.WebApi.xml"); options.IncludeXmlComments(xmlPath); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { InjectionResolver.Inject(services, Configuration.GetConnectionString("DefaultConnection")); string _secretKey = Configuration.GetSection("Secret").Value; SymmetricSecurityKey _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_secretKey)); services.AddAuthorization(); //? services.AddControllers(); services.AddRazorPages(); var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions)); services.Configure <JwtIssuerOptions>(options => { options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)]; options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)]; options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256); }); var tokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)], ValidateAudience = true, ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)], ValidateIssuerSigningKey = true, IssuerSigningKey = _signingKey, RequireExpirationTime = false, ValidateLifetime = true, ClockSkew = TimeSpan.Zero }; services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(configureOptions => { configureOptions.ClaimsIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)]; configureOptions.TokenValidationParameters = tokenValidationParameters; configureOptions.Events = new JwtBearerEvents { OnTokenValidated = context => { var accessToken = context.SecurityToken as JwtSecurityToken; if (accessToken != null) { ClaimsIdentity identity = context.Principal.Identity as ClaimsIdentity; if (identity != null) { identity.AddClaim(new Claim("access_token", accessToken.RawData)); } } return(Task.CompletedTask); } }; configureOptions.SaveToken = true; }); }
/// <summary> /// Inject this object's dependencies /// </summary> /// <typeparam name="T">The type of the object</typeparam> /// <param name="reference">A reference of the object, or null if static</param> /// <param name="injector">The injector to use for resolving types</param> /// <exception cref="InjectorException">Thrown if some dependencies could not be resolved</exception> public static void Inject <T>(this T reference, IInjector injector) { InjectionResolver.InjectProperties(reference, injector); InjectionResolver.InjectFields(reference, injector); }