Skip to content

Commit 866a781

Browse files
authored
Merge pull request #60 from alastairtree/feat/netcore2
Upgrade to version 2 (netstandard 2/dotnet core)
2 parents 9862ffa + 2c3b2ba commit 866a781

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1439
-1145
lines changed

.gitattributes

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.App" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.2" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\LazyCache.AspNetCore\LazyCache.AspNetCore.csproj" />
18+
<ProjectReference Include="..\LazyCache\LazyCache.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

Samples/ApiAsyncCachingSample/Controllers/DbQueriesController.cs renamed to CacheDatabaseQueriesApiSample/Controllers/DbQueriesController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.Web.Http;
2-
using ApiAsyncCachingSample.Models;
1+
using CacheDatabaseQueriesApiSample;
2+
using Microsoft.AspNetCore.Mvc;
33

44
namespace ApiAsyncCachingSample.Controllers
55
{
6-
public class DbQueriesController : ApiController
6+
public class DbQueriesController : Controller
77
{
88
[HttpGet]
99
[Route("api/dbQueries")]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using LazyCache;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace CacheDatabaseQueriesApiSample.Controllers
6+
{
7+
public class DbTimeController : Controller
8+
{
9+
private readonly IAppCache cache;
10+
private readonly string cacheKey = "DbTimeController.Get";
11+
private readonly DbTimeContext dbContext;
12+
13+
14+
public DbTimeController(DbTimeContext context, IAppCache cache)
15+
{
16+
dbContext = context;
17+
this.cache = cache;
18+
}
19+
20+
[HttpGet]
21+
[Route("api/dbtime")]
22+
public DbTimeEntity Get()
23+
{
24+
Func<DbTimeEntity> actionThatWeWantToCache = () => dbContext.GeDbTime();
25+
26+
var cachedDatabaseTime = cache.GetOrAdd(cacheKey, actionThatWeWantToCache);
27+
28+
return cachedDatabaseTime;
29+
}
30+
31+
[HttpDelete]
32+
[Route("api/dbtime")]
33+
public IActionResult DeleteFromCache()
34+
{
35+
cache.Remove(cacheKey);
36+
var friendlyMessage = new {Message = $"Item with key '{cacheKey}' removed from server in-memory cache"};
37+
return Ok(friendlyMessage);
38+
}
39+
}
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Linq;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace CacheDatabaseQueriesApiSample
5+
{
6+
public class DbTimeContext : DbContext
7+
{
8+
private static int databaseRequestCounter; //just for demo - don't use static fields for statistics!
9+
10+
public DbTimeContext(DbContextOptions<DbTimeContext> options)
11+
: base(options)
12+
{
13+
}
14+
15+
// simulate a table in the database so we can get just one row with the current time
16+
private DbSet<DbTimeEntity> Times { get; set; }
17+
18+
public static int DatabaseRequestCounter()
19+
{
20+
return databaseRequestCounter;
21+
}
22+
23+
public DbTimeEntity GeDbTime()
24+
{
25+
// get the current time from SQL server right now asynchronously (simulating a slow query)
26+
var result = Times
27+
.FromSql("WAITFOR DELAY '00:00:00:500'; SELECT 1 as [ID], GETDATE() as [TimeNowInTheDatabase]")
28+
.Single();
29+
30+
databaseRequestCounter++;
31+
32+
return result;
33+
}
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace CacheDatabaseQueriesApiSample
4+
{
5+
/// <summary>
6+
/// Simulates loading a record from a table, but really just gets the current datatime from the database
7+
/// </summary>
8+
public class DbTimeEntity
9+
{
10+
public DbTimeEntity(DateTime now)
11+
{
12+
TimeNowInTheDatabase = now;
13+
}
14+
15+
public DbTimeEntity()
16+
{
17+
}
18+
19+
public virtual int id { get; set; }
20+
21+
public virtual DateTime TimeNowInTheDatabase { get; set; }
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
namespace CacheDatabaseQueriesApiSample
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
BuildWebHost(args).Run();
11+
}
12+
13+
public static IWebHost BuildWebHost(string[] args)
14+
{
15+
return WebHost.CreateDefaultBuilder(args)
16+
.UseStartup<Startup>()
17+
.Build();
18+
}
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:52671/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"launchUrl": "/",
15+
"environmentVariables": {
16+
"ASPNETCORE_ENVIRONMENT": "Development"
17+
}
18+
},
19+
"CacheDatabaseQueriesApiSample": {
20+
"commandName": "Project",
21+
"launchBrowser": true,
22+
"launchUrl": "/",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
},
26+
"applicationUrl": "http://localhost:52672/"
27+
}
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace CacheDatabaseQueriesApiSample
8+
{
9+
public class Startup
10+
{
11+
public Startup(IConfiguration configuration)
12+
{
13+
Configuration = configuration;
14+
}
15+
16+
public IConfiguration Configuration { get; }
17+
18+
// This method gets called by the runtime. Use this method to add services to the container.
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
services.AddMvc();
22+
23+
// just for demo - use app settings for db config
24+
var connection =
25+
@"Server=(localdb)\projectsv13;Database=Master;Trusted_Connection=True;ConnectRetryCount=0";
26+
27+
// register the database
28+
services.AddDbContext<DbTimeContext>(options => options.UseSqlServer(connection));
29+
30+
// Register IAppCache as a singleton CachingService
31+
services.AddLazyCache();
32+
}
33+
34+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
35+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
36+
{
37+
if (env.IsDevelopment())
38+
app.UseDeveloperExceptionPage();
39+
40+
app.UseDefaultFiles();
41+
app.UseStaticFiles();
42+
app.UseMvc();
43+
}
44+
}
45+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"LogLevel": {
5+
"Default": "Debug",
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)