public static void RegisterStorageServices(this IAbstractDependencyInjector di, IHostingEnvironment env) { // Configure service types. di.AddSingletone <IOuth2CodeSynchronizer, InProcessOuth2CodeSynchronizer>(); di.AddPerThread <ILocalStorage>(provider => { var config = provider.Resolve <IConfiguration>(); var mlpath = config["MediaStorage:Local:MediaLibraryPath"]; var tempMlPath = config["MediaStorage:Local:TempLibraryPath"]; if (mlpath.StartsWith("~")) { mlpath = env.ContentRootPath + mlpath.Substring(1); } if (tempMlPath.StartsWith("~")) { tempMlPath = env.ContentRootPath + tempMlPath.Substring(1); } return(new LocalStorage(mlpath)); }); di.AddPerThread <IStorage>(provider => { var config = provider.Resolve <IConfiguration>(); var accessToken = config["MediaStorage:GoogleDrive:AccessToken"]; var refreshoken = config["MediaStorage:GoogleDrive:RefreshToken"]; var appName = config["MediaStorage:GoogleDrive:AppName"]; var clientId = config["MediaStorage:GoogleDrive:ClientId"]; var clientSecret = config["MediaStorage:GoogleDrive:ClientSecret"]; string folderPath = config.GetValue <string>("MediaStorage:GoogleDrive:DataStoreFolder", string.Empty); bool isFullPath = config.GetValue <bool>("DataStoreFolderIsFullPath", true); if (string.IsNullOrEmpty(accessToken)) { var gdriveStorage = GoogleDriveStorageFactory.Create(appName, clientId, clientSecret); if (!string.IsNullOrEmpty(folderPath)) { gdriveStorage.DataStoreFolder = folderPath; gdriveStorage.IsFullPath = isFullPath; } //gdriveStorage.AuthorizeRedirectUrl = config.GetValue<string>("MediaStorage:GoogleDrive:CodeReceiverUrl", string.Empty); return(gdriveStorage); } else { var gdriveStorage = GoogleDriveStorageFactory.CreateUsingAccessToken(appName, clientId, clientSecret, accessToken, refreshoken); if (!string.IsNullOrEmpty(folderPath)) { gdriveStorage.DataStoreFolder = folderPath; gdriveStorage.IsFullPath = isFullPath; } return(gdriveStorage); } }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddScoped <WebAppDataContext>(provider => new WebAppDataContext(provider.GetService <IConfiguration>())); services.AddIdentity <ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <WebAppDataContext>() .AddDefaultTokenProviders(); services.ConfigureApplicationCookie(options => { options.LoginPath = "/Account/LogIn"; // options.Cookie.Name = ".AuthCookie"; options.Cookie.SameSite = SameSiteMode.Strict; options.Cookie.HttpOnly = true; }); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme); services.AddMvc() .AddJsonOptions(options => { // Without this contract property names will change useing camelCase. options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); // Add application services. services.AddTransient <IEmailSender, AuthMessageSender>(); services.AddTransient <ISmsSender, AuthMessageSender>(); // Configure dependency abstructor. _di = services.UseMicrosoftDI(_serviceProvider); _di.AddSingletone(_configuration); //services.AddSingleton(typeof(IConfiguration), _configuration); _di.AddSingletone(_di); // Start registering services. _di.RegisterStorageServices(_hostingEnvironment); _di.RegisterMediaDataContextServices(); _di.RegisterCoreServices(); // End registering services. }
static void RegisterServices(IAbstractDependencyInjector di) { // Microsoft.Extensions.Configuration.IConfigurationRoot // Microsoft.Extensions.Configuration.IConfiguration //var ss = typeof(Configuration).Name; // Add services here. di.AddSingletone(Configuration); //di.AddPerThread<IMediaDataContext, MediaDataContext>(); di.AddPerThread <itest, test>(); di.Build(); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddAuthentication("MyAuth") .AddScheme <MyAuthSchemeOptions, MyAuthenticationHandler>("MyAuth", options => { var authSettings = _di.Resolve <IOptions <AuthSettings> >().Value; options.Scheme = "MyAuth"; options.AccessTokenKeyHeaderName = authSettings.AccessTokenHeaderName; options.SessionInfoContextKeyName = authSettings.SessionInfoContextKeyName; options.Expiration = new TimeSpan((authSettings.ExpirationInMin - authSettings.ExpirationInMin % 60) / 60, authSettings.ExpirationInMin % 60, 0); }); services.AddMvc(); services.AddMemoryCache(); // Configure settings. services.AddOptions(); services.Configure <GoogleDriveSettings>(_configuration.GetSection("MediaStorage:GoogleDrive")); services.Configure <LocalDriveSettings>(_configuration.GetSection("MediaStorage:Local")); services.Configure <AuthSettings>(_configuration.GetSection("Auth")); // Configure dependency abstructor. _di = services.UseMicrosoftDI(_serviceProvider); _di.AddSingletone(_configuration); _di.AddSingletone(_di); _di.AddPerThread <AuthenticationHandler <MyAuthSchemeOptions>, MyAuthenticationHandler>(); // Start registering services. _di.RegisterStorageServices(_hostingEnvironment); _di.RegisterMediaDataContextServices(); _di.RegisterStreamingDataContextServices(); _di.RegisterCoreServices(); // End registering services. return(_di.Build()); }