本文整理汇总了C#中IServiceCollection.AddMemoryCache方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.AddMemoryCache方法的具体用法?C# IServiceCollection.AddMemoryCache怎么用?C# IServiceCollection.AddMemoryCache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceCollection
的用法示例。
在下文中一共展示了IServiceCollection.AddMemoryCache方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
//Add Cqrs services
services.AddSingleton<InProcessBus>(new InProcessBus());
services.AddSingleton<ICommandSender>(y => y.GetService<InProcessBus>());
services.AddSingleton<IEventPublisher>(y => y.GetService<InProcessBus>());
services.AddSingleton<IHandlerRegistrar>(y => y.GetService<InProcessBus>());
services.AddScoped<ISession, Session>();
services.AddSingleton<IEventStore, InMemoryEventStore>();
services.AddScoped<ICache, CQRSlite.Cache.MemoryCache>();
services.AddScoped<IRepository>(y => new CacheRepository(new Repository(y.GetService<IEventStore>()), y.GetService<IEventStore>(), y.GetService<ICache>()));
services.AddTransient<IReadModelFacade, ReadModelFacade>();
//Scan for commandhandlers and eventhandlers
services.Scan(scan => scan
.FromAssemblies(typeof(InventoryCommandHandlers).GetTypeInfo().Assembly)
.AddClasses(classes => classes.Where(x => {
var allInterfaces = x.GetInterfaces();
return
allInterfaces.Any(y => y.GetTypeInfo().IsGenericType && y.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICommandHandler<>)) ||
allInterfaces.Any(y => y.GetTypeInfo().IsGenericType && y.GetTypeInfo().GetGenericTypeDefinition() == typeof(IEventHandler<>));
}))
.AsSelf()
.WithTransientLifetime()
);
//Register bus
var serviceProvider = services.BuildServiceProvider();
var registrar = new BusRegistrar(new DependencyResolver(serviceProvider));
registrar.Register(typeof(InventoryCommandHandlers));
// Add framework services.
services.AddMvc();
}
示例2: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
var useInMemoryStore = !_platform.IsRunningOnWindows
|| _platform.IsRunningOnMono
|| _platform.IsRunningOnNanoServer;
// Add EF services to the services container
if (useInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase());
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__",":")]));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.AccessDeniedPath = "/Home/AccessDenied";
})
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
// Add memory cache services
services.AddMemoryCache();
services.AddDistributedMemoryCache();
// Add session related services.
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.AddAuthorization(options =>
{
options.AddPolicy(
"ManageStore",
authBuilder => {
authBuilder.RequireClaim("ManageStore", "Allowed");
});
});
}
示例3: ConfigureCachingServices
/// <summary>
/// Configures caching for the application. Registers the <see cref="IDistrbutedCache"/> and
/// <see cref="IMemoryCache"/> types with the services collection or IoC container. The
/// <see cref="IDistrbutedCache"/> is intended to be used in cloud hosted scenarios where there is a shared
/// cache, which is shared between multiple instances of the application. Use the <see cref="IMemoryCache"/>
/// otherwise.
/// </summary>
/// <param name="services">The services collection or IoC container.</param>
private static void ConfigureCachingServices(IServiceCollection services)
{
// Adds IMemoryCache which is a simple in-memory cache.
services.AddMemoryCache();
// Adds IDistributedCache which is a distributed cache shared between multiple servers. This adds a default
// implementation of IDistributedCache which is not distributed. See below.
services.AddDistributedMemoryCache();
// Uncomment the following line to use the Redis implementation of IDistributedCache. This will override
// any previously registered IDistributedCache service.
// Redis is a very fast cache provider and the recommended distributed cache provider.
// services.AddTransient<IDistributedCache, RedisCache>();
// Uncomment the following line to use the Microsoft SQL Server implementation of IDistributedCache.
// Note that this would require setting up the session state database.
// Redis is the preferred cache implementation but you can use SQL Server if you don't have an alternative.
// services.AddSqlServerCache(
// x =>
// {
// x.ConnectionString = "Server=.;Database=ASPNET5SessionState;Trusted_Connection=True;";
// x.SchemaName = "dbo";
// x.TableName = "Sessions";
// });
}
示例4: ConfigureServices
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddTransient<ITimeService, TimeService>();
services.AddTransient<IGreetingService, GreetingService>();
}
示例5: ConfigureServices
// 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.AddMemoryCache();
services.AddMvc();
services.AddObjectValidator();
}
示例6: ConfigureServices
// 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.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMemoryCache();
services.AddMvc()
.AddJsonOptions(options =>
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());
services.Configure<AppSettings>(settings => settings = Configuration.GetSection("AppSettings") as AppSettings);
// Add application services.
services.AddSingleton<RawDataRepository>()
.AddSingleton<CacheDataRepository>()
.AddSingleton<GlobalCacheRepository>()
//.AddSingleton<LocalCacheRepository>()
//.AddSingleton<MasterDataService>()
//.AddSingleton<CardService>()
//.AddSingleton<TreasureService>()
//.AddSingleton<CombatService>()
//.AddSingleton<FamilyService>()
//.AddSingleton<AccountService>()
//.AddSingleton<SecurityService>()
.AddSingleton<SummaryService>()
.AddSingleton<MasterDataService>()
.AddSingleton<MasterDataRepository>();
}
示例7: ConfigureServices
// This method gets called by the runtime.
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
services.AddMemoryCache();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// Add ReactJS.NET services.
services.AddReact();
}
示例8: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
// Add EF services to the services container
if (_platform.UseInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase());
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
// Add memory cache services
services.AddMemoryCache();
services.AddDistributedMemoryCache();
// Add session related services.
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.AddAuthorization(options =>
{
options.AddPolicy(
"ManageStore",
authBuilder =>
{
authBuilder.RequireClaim("ManageStore", "Allowed");
});
});
}
示例9: ConfigureServices
// 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
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")))
.AddDbContext<CronicaDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = new TimeSpan(0, 15, 0);
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMemoryCache();
//services.AddSession(o =>
//{
// o.IdleTimeout = TimeSpan.FromSeconds(10);
//});
services.AddMvc();
services.AddAuthorization(options =>
{
options.AddPolicy("Jugador", policy => policy.AddRequirements(new TipoCuentaRequirement(TipoCuenta.Jugador)));
options.AddPolicy("Narrador", policy => policy.Requirements.Add(new TipoCuentaRequirement(TipoCuenta.Narrador)));
options.AddPolicy("Administrador", policy => policy.Requirements.Add(new TipoCuentaRequirement(TipoCuenta.Administrador)));
});
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddScoped<IServicioUsuarios, ServicioUsuarios>();
services.AddScoped<IServicioPersonajes, ServicioPersonajes>();
services.AddScoped<IServicioJugadores, ServicioJugadores>();
services.AddScoped<IServicioAtributos, ServicioAtributos>();
services.AddScoped<IServicioPlantillasTrama, ServicioPlantillasTrama>();
services.AddScoped<IServicioTramas, ServicioTramas>();
services.AddScoped<IServicioEntrePartidas, ServicioEntrePartidas>();
services.AddScoped<IServicioInterludios, ServicioInterludios>();
services.AddScoped<IServicioSeguidores, ServicioSeguidores>();
services.AddScoped<IServicioAsignaciones, ServicioAsignaciones>();
services.AddScoped<IServicioMensajeria, ServicioMensajeria>();
services.AddScoped<IServicioEmail, ServicioEmail>();
services.AddScoped<IServicioConfiguracion, ServicioConfiguracion>();
services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper());
services.AddSingleton<IAuthorizationHandler, TipoCuentaHandler>();
}
示例10: ConfigureServices
public IServiceProvider ConfigureServices(IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddIdentity<User, Role>();
services.AddMemoryCache();
services.AddAuthentication(options => { options.SignInScheme = "ServerCookie"; });
services.AddMvc();
services.AddCors(ConfigureCors);
return CreateContainerServiceProvider(services);
}
示例11: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
//Sql client not available on mono
var useInMemoryStore = !_runtimeEnvironment.OperatingSystem.Equals("Windows", StringComparison.OrdinalIgnoreCase);
// Add EF services to the services container
if (useInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase());
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
//Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddMemoryCache();
services.AddDistributedMemoryCache();
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.Configure<AuthorizationOptions>(options =>
{
options.AddPolicy("ManageStore", new AuthorizationPolicyBuilder().RequireClaim("ManageStore", "Allowed").Build());
});
}
示例12: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMemoryCache();
// Add framework services.
services.AddMvc();
services.Configure<SwiftServiceOptions>(Configuration.GetSection("SwiftCluster"));
services.AddSingleton<ISwiftLogger, SwiftServiceLogger>();
services.AddSingleton<ISwiftAuthManager, SwiftAuthManagerMemoryCache>();
services.AddTransient<ISwiftClient, SwiftService>();
}
示例13: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddTransient<IBlogRepository, JsonBlogRepository>();
services.AddScoped<IApplicationContext, ApplicationContext>();
services.AddMvc();
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
});
}
示例14: ConfigureServices
// 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.AddMemoryCache();
services.AddSession();
services.AddMvc();
// Register application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddScoped<IWiFiAuthService, WiFiAuthService>(_ =>
new WiFiAuthService(Configuration.GetConnectionString("DefaultConnection"))
);
}
示例15: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMemoryCache();
services.AddSession(s =>
{
s.IdleTimeout = TimeSpan.FromMinutes(1);
});
//add a Assembly Loader to services
services.AddScoped<AssemblyLoadContext, AssemblyLoadContext>(x => {
return AssemblyLoadContext.Default;
});
}