Skip to content
Permalink
Browse files

Update to .NET Core 3.0

  • Loading branch information...
RickStrahl committed Sep 23, 2019
1 parent 97bb68f commit 8a48e63ee60d04df05fe48a247cbd7ae29b6e31d
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!--<ProjectReference Include="..\Westwind.Utilities\Westwind.Utilities.csproj" />-->
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Westwind.Utilities" Version="3.0.26" />
<PackageReference Include="Westwind.Utilities" Version="3.0.30" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
@@ -12,6 +12,9 @@ public AlbumViewerContext(DbContextOptions options) : base(options)
{
}

public AlbumViewerContext()
{ }

public DbSet<Album> Albums { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Track> Tracks { get; set; }
@@ -7,6 +7,7 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Westwind.BusinessObjects
@@ -92,9 +93,17 @@ public static class DbSetExtensions
var state = context.Entry(entity);
var metadata = state.Metadata;
var key = metadata.FindPrimaryKey();
var props = key.Properties.ToArray();
//var props = key.Properties.ToArray();

return props.Select(x => x.GetGetter().GetClrValue(entity)).ToArray();
var list = new List<object>();
foreach (var prop in key.Properties)
{
var getter = prop.GetGetter();
var val = getter.GetClrValue(entity);
list.Add(val);
}

return list.ToArray();
}
}
}
@@ -282,24 +282,31 @@ public async Task<bool> SaveAsync(TEntity entity = null)
var entry = Context.Entry(entity);
if (entry.State == EntityState.Detached)
{
Context.Attach(entity);

// see if it exists
TEntity match = null;
try
{
object id = Context.GetEntityKey(entity).FirstOrDefault();
if (id != null)
match = DbSet.Find(id);
}
catch
{
}
bool exists = entry.IsKeySet;

if (match != null)

//false;
//try
//{
// object id = Context.GetEntityKey(entity).FirstOrDefault();
// if (id != null)
// exists =
//}
//catch
//{
//}

if (exists)
{
entry = Context.Update(entity);
entry.State = EntityState.Modified;
}
else
{
Context.Add(entity);
entry.State = EntityState.Added;
}
}
}

@@ -1,31 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>d900d6cb-0e21-403b-94a3-17412045e7b4</UserSecretsId>
<Version>1.0.10</Version>
<AssemblyVersion>1.0.10.0</AssemblyVersion>
<FileVersion>1.0.10.0</FileVersion>
<Version>1.1</Version>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.4" />

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="Westwind.AspNetCore" Version="3.2.9" />
<ProjectReference Include="..\AlbumViewerBusiness\AlbumViewerBusiness.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="wwwroot\**\*;Areas\**\Views;appsettings.json;album.js;web.config">
<Content Update="wwwroot\**\*;Areas\**\Views;appsettings.json;albums.js;web.config">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<Compile Remove="logs\**" />
<Content Remove="logs\**" />
<EmbeddedResource Remove="logs\**" />
<None Remove="logs\**" />
</ItemGroup>

</Project>
@@ -30,7 +30,7 @@ public class AlbumViewerApiController : Controller
IConfiguration Configuration;
private ILogger<AlbumViewerApiController> Logger;

private IHostingEnvironment HostingEnv;
private IWebHostEnvironment HostingEnv;

public AlbumViewerApiController(
AlbumViewerContext ctx,
@@ -39,7 +39,7 @@ public class AlbumViewerApiController : Controller
AlbumRepository albumRepo,
IConfiguration config,
ILogger<AlbumViewerApiController> logger,
IHostingEnvironment env)
IWebHostEnvironment env)
{
context = ctx;
serviceProvider = svcProvider;
@@ -121,7 +121,9 @@ public async Task<string> DeleteAlbumByName(string name)
throw new ApiException("You have to be logged in to modify data", 401);

var pks =
await context.Albums.Where(alb => alb.Title == name).Select(alb => alb.Id).ToAsyncEnumerable().ToList();
await context.Albums
.Where(alb => alb.Title == name)
.Select(alb => alb.Id).ToListAsync();

StringBuilder sb = new StringBuilder();
foreach (int pk in pks)
@@ -171,8 +173,13 @@ public async Task<ArtistResponse> SaveArtist([FromBody] Artist artist)
if (!ArtistRepo.Validate(artist))
throw new ApiException(ArtistRepo.ValidationErrors.ToString(), 500, ArtistRepo.ValidationErrors);

//if (artist.Id < 1)
// ArtistRepo.Context.Artists.Add(artist);
//else
// ArtistRepo.Context.Artists.Update(artist);

if (!await ArtistRepo.SaveAsync(artist))
throw new ApiException("Unable to save artist.");
throw new ApiException($"Unable to save artist. {ArtistRepo.ErrorMessage}");

return new ArtistResponse()
{
@@ -10,7 +10,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Reflection;
using Microsoft.AspNetCore.Hosting.Internal;
using System.Runtime.Versioning;
using Microsoft.Extensions.Configuration;

@@ -24,11 +23,11 @@ public class ConfigurationController : Controller
{
IOptions<ApplicationConfiguration> AppConfiguration;
IConfigurationRoot RawConfiguration;
IHostingEnvironment Host;
IWebHostEnvironment Host;

public ConfigurationController(IOptions<ApplicationConfiguration> configuration,
IConfigurationRoot config,
IHostingEnvironment host)
IWebHostEnvironment host)
{
AppConfiguration = configuration;
Host = host;
@@ -7,6 +7,7 @@
using AlbumViewerBusiness;
using Microsoft.EntityFrameworkCore;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using AlbumViewerAspNetCore;
@@ -17,6 +18,7 @@
using Serilog;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Builder;

namespace AlbumViewerNetCore
{
@@ -130,25 +132,23 @@ public void ConfigureServices(IServiceCollection services)
// Per request injections
services.AddScoped<ApiExceptionFilter>();

// Add framework services
services
.AddMvc(options =>
{
// options.Filters.Add(new ApiExceptionFilter());
})
.SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2)
.AddJsonOptions(opt =>
{
var resolver = opt.SerializerSettings.ContractResolver;
if (resolver != null)
{
var res = resolver as DefaultContractResolver;
res.NamingStrategy = null;
}


services.AddControllers()
// Use classic JSON
.AddNewtonsoftJson(opt =>
{
var resolver = opt.SerializerSettings.ContractResolver;
if (resolver != null)
{
var res = resolver as DefaultContractResolver;
res.NamingStrategy = null;
}

if (HostingEnvironment.IsDevelopment())
opt.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
});
});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -207,17 +207,21 @@ public void ConfigureServices(IServiceCollection services)
string useSqLite = Configuration["Data:useSqLite"];
Console.WriteLine(useSqLite == "true" ? "SqLite" : "Sql Server");

app.UseRouting();

app.UseCors("CorsPolicy");


app.UseAuthentication();
app.UseAuthorization();

app.UseDatabaseErrorPage();
//app.UseDatabaseErrorPage();
app.UseStatusCodePages();

app.UseDefaultFiles(); // so index.html is not required
app.UseStaticFiles();


// Handle Lets Encrypt Route (before MVC processing!)
// alternately use an MVC Route (in ConfigurationController)
//app.UseRouter(r =>
@@ -231,8 +235,10 @@ public void ConfigureServices(IServiceCollection services)
//});

//// put last so header configs like CORS or Cookies etc can fire
app.UseMvcWithDefaultRoute();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});

// catch-all handler for HTML5 client routes - serve index.html
app.Run(async context =>

0 comments on commit 8a48e63

Please sign in to comment.
You can’t perform that action at this time.