Skip to content

Commit

Permalink
Merge pull request #60 from alastairtree/feat/netcore2
Browse files Browse the repository at this point in the history
Upgrade to version 2 (netstandard 2/dotnet core)
  • Loading branch information
alastairtree committed Mar 20, 2019
2 parents 9862ffa + 2c3b2ba commit 866a781
Show file tree
Hide file tree
Showing 59 changed files with 1,439 additions and 1,145 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
21 changes: 21 additions & 0 deletions CacheDatabaseQueriesApiSample/CacheDatabaseQueriesApiSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.2" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LazyCache.AspNetCore\LazyCache.AspNetCore.csproj" />
<ProjectReference Include="..\LazyCache\LazyCache.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Web.Http;
using ApiAsyncCachingSample.Models;
using CacheDatabaseQueriesApiSample;
using Microsoft.AspNetCore.Mvc;

namespace ApiAsyncCachingSample.Controllers
{
public class DbQueriesController : ApiController
public class DbQueriesController : Controller
{
[HttpGet]
[Route("api/dbQueries")]
Expand Down
40 changes: 40 additions & 0 deletions CacheDatabaseQueriesApiSample/Controllers/DbTimeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using LazyCache;
using Microsoft.AspNetCore.Mvc;

namespace CacheDatabaseQueriesApiSample.Controllers
{
public class DbTimeController : Controller
{
private readonly IAppCache cache;
private readonly string cacheKey = "DbTimeController.Get";
private readonly DbTimeContext dbContext;


public DbTimeController(DbTimeContext context, IAppCache cache)
{
dbContext = context;
this.cache = cache;
}

[HttpGet]
[Route("api/dbtime")]
public DbTimeEntity Get()
{
Func<DbTimeEntity> actionThatWeWantToCache = () => dbContext.GeDbTime();

var cachedDatabaseTime = cache.GetOrAdd(cacheKey, actionThatWeWantToCache);

return cachedDatabaseTime;
}

[HttpDelete]
[Route("api/dbtime")]
public IActionResult DeleteFromCache()
{
cache.Remove(cacheKey);
var friendlyMessage = new {Message = $"Item with key '{cacheKey}' removed from server in-memory cache"};
return Ok(friendlyMessage);
}
}
}
35 changes: 35 additions & 0 deletions CacheDatabaseQueriesApiSample/DbTimeContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace CacheDatabaseQueriesApiSample
{
public class DbTimeContext : DbContext
{
private static int databaseRequestCounter; //just for demo - don't use static fields for statistics!

public DbTimeContext(DbContextOptions<DbTimeContext> options)
: base(options)
{
}

// simulate a table in the database so we can get just one row with the current time
private DbSet<DbTimeEntity> Times { get; set; }

public static int DatabaseRequestCounter()
{
return databaseRequestCounter;
}

public DbTimeEntity GeDbTime()
{
// get the current time from SQL server right now asynchronously (simulating a slow query)
var result = Times
.FromSql("WAITFOR DELAY '00:00:00:500'; SELECT 1 as [ID], GETDATE() as [TimeNowInTheDatabase]")
.Single();

databaseRequestCounter++;

return result;
}
}
}
23 changes: 23 additions & 0 deletions CacheDatabaseQueriesApiSample/DbTimeEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace CacheDatabaseQueriesApiSample
{
/// <summary>
/// Simulates loading a record from a table, but really just gets the current datatime from the database
/// </summary>
public class DbTimeEntity
{
public DbTimeEntity(DateTime now)
{
TimeNowInTheDatabase = now;
}

public DbTimeEntity()
{
}

public virtual int id { get; set; }

public virtual DateTime TimeNowInTheDatabase { get; set; }
}
}
20 changes: 20 additions & 0 deletions CacheDatabaseQueriesApiSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace CacheDatabaseQueriesApiSample
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
}
29 changes: 29 additions & 0 deletions CacheDatabaseQueriesApiSample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52671/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CacheDatabaseQueriesApiSample": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:52672/"
}
}
}
45 changes: 45 additions & 0 deletions CacheDatabaseQueriesApiSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace CacheDatabaseQueriesApiSample
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

// just for demo - use app settings for db config
var connection =
@"Server=(localdb)\projectsv13;Database=Master;Trusted_Connection=True;ConnectRetryCount=0";

// register the database
services.AddDbContext<DbTimeContext>(options => options.UseSqlServer(connection));

// Register IAppCache as a singleton CachingService
services.AddLazyCache();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
}
}
}
10 changes: 10 additions & 0 deletions CacheDatabaseQueriesApiSample/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
15 changes: 15 additions & 0 deletions CacheDatabaseQueriesApiSample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="centre-box">
<div class="counter pull-right"><span class="counter-val">0</span> Database query(s)</div>

<h1>Sample app to demonstrate using an async cache in your API to save database SQL queries and speed up API calls</h1>
<h1>Sample app to demonstrate using an cache in your API to save database SQL queries and speed up API calls</h1>

<p>
Every 3 seconds we fetch the current time from the database, however because the sql query
Expand Down Expand Up @@ -70,7 +70,7 @@ <h1>Sample app to demonstrate using an async cache in your API to save database
},

updateLogWithTime: function (dbTime, duration) {
var message = "API reports that the time now in the database is " + dbTime.TimeNowInTheDatabase + " (" + duration + "ms)";
var message = "API reports that the time now in the database is " + dbTime.timeNowInTheDatabase + " (" + duration + "ms)";
app.log(message);
},

Expand All @@ -80,7 +80,7 @@ <h1>Sample app to demonstrate using an async cache in your API to save database
url: "/api/dbtime"
})
.done(function (data) {
app.log(data.Message);
app.log(data.message);
});
},

Expand Down
22 changes: 22 additions & 0 deletions Console.Net461/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Loading

0 comments on commit 866a781

Please sign in to comment.