Skip to content

Commit 84e7311

Browse files
authored
(#271) WPF Sample Update to .NET 9 (#279)
1 parent 580e139 commit 84e7311

File tree

4 files changed

+92
-41
lines changed

4 files changed

+92
-41
lines changed

samples/todoapp/Samples.TodoApp.sln

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoApp.Avalonia.iOS", "Tod
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoApp.Avalonia.Browser", "TodoApp.Avalonia\TodoApp.Avalonia.Browser\TodoApp.Avalonia.Browser.csproj", "{E8BB1310-477D-44B0-B13E-77F09433D0A3}"
2525
EndProject
26+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Files", "Solution Files", "{E0CB420D-EEFC-415C-B10A-A01233115A3C}"
27+
ProjectSection(SolutionItems) = preProject
28+
..\Directory.Packages.props = ..\Directory.Packages.props
29+
EndProjectSection
30+
EndProject
31+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server", "Server", "{AF5C479C-1BDE-4783-8707-A3440FC0B89D}"
32+
EndProject
33+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Clients", "Clients", "{32025F6A-2D8A-4BA2-93BE-B989DFD216FC}"
34+
EndProject
2635
Global
2736
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2837
Debug|Any CPU = Debug|Any CPU
@@ -59,22 +68,6 @@ Global
5968
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555}.Release|x86.ActiveCfg = Release|x86
6069
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555}.Release|x86.Build.0 = Release|x86
6170
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555}.Release|x86.Deploy.0 = Release|x86
62-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|Any CPU.Build.0 = Debug|Any CPU
64-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|ARM64.ActiveCfg = Debug|Any CPU
65-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|ARM64.Build.0 = Debug|Any CPU
66-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x64.ActiveCfg = Debug|Any CPU
67-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x64.Build.0 = Debug|Any CPU
68-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x86.ActiveCfg = Debug|Any CPU
69-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x86.Build.0 = Debug|Any CPU
70-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|Any CPU.ActiveCfg = Release|Any CPU
71-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|Any CPU.Build.0 = Release|Any CPU
72-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|ARM64.ActiveCfg = Release|Any CPU
73-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|ARM64.Build.0 = Release|Any CPU
74-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x64.ActiveCfg = Release|Any CPU
75-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x64.Build.0 = Release|Any CPU
76-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.ActiveCfg = Release|Any CPU
77-
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.Build.0 = Release|Any CPU
7871
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7972
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
8073
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|ARM64.ActiveCfg = Debug|Any CPU
@@ -216,7 +209,11 @@ Global
216209
HideSolutionNode = FALSE
217210
EndGlobalSection
218211
GlobalSection(NestedProjects) = preSolution
219-
{2AC73FBE-9E76-4702-B551-B5884383CC68} = {7183ECEC-9F44-48CE-BB97-AA2445170D5E}
212+
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
213+
{E67734DD-B397-4A65-AA50-D62F37EF05DD} = {AF5C479C-1BDE-4783-8707-A3440FC0B89D}
214+
{00430043-04C5-4F8F-87A9-98ECC0051808} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
215+
{A0996FB8-890D-4E90-A881-01F9EF709711} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
216+
{9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
220217
{539C6E0F-8F23-4AE0-B8E6-7E72C53B890A} = {9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E}
221218
{9C2BA2A4-4AD6-4B67-BB6B-29A9024C33C4} = {9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E}
222219
{3D741850-6FAA-4F36-BD58-F6ECE0CE55D7} = {9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E}

samples/todoapp/TodoApp.WPF/Database/AppDbContext.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,50 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#undef OFFLINE_SYNC_ENABLED
6+
57
using CommunityToolkit.Datasync.Client.Http;
68
using CommunityToolkit.Datasync.Client.Offline;
79
using Microsoft.EntityFrameworkCore;
10+
using TodoApp.WPF.Services;
811

912
namespace TodoApp.WPF.Database;
1013

14+
#if OFFLINE_SYNC_ENABLED
15+
public class AppDbContext(DbContextOptions<AppDbContext> options) : OfflineDbContext(options)
16+
#else
1117
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
18+
#endif
1219
{
1320
public DbSet<TodoItem> TodoItems => Set<TodoItem>();
1421

15-
//protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
16-
//{
17-
// HttpClientOptions clientOptions = new()
18-
// {
19-
// Endpoint = new Uri("https://YOURSITEHERE.azurewebsites.net/"),
20-
// HttpPipeline = [new LoggingHandler()]
21-
// };
22-
// _ = optionsBuilder.UseHttpClientOptions(clientOptions);
23-
//}
22+
#if OFFLINE_SYNC_ENABLED
23+
protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
24+
{
25+
HttpClientOptions clientOptions = new()
26+
{
27+
Endpoint = new Uri("https://YOUR_SITE_HERE.azurewebsites.net/"),
28+
HttpPipeline = [new LoggingHandler()]
29+
};
30+
_ = optionsBuilder.UseHttpClientOptions(clientOptions);
31+
}
32+
#endif
2433

2534
public async Task SynchronizeAsync(CancellationToken cancellationToken = default)
2635
{
27-
//PushResult pushResult = await this.PushAsync(cancellationToken);
28-
//if (!pushResult.IsSuccessful)
29-
//{
30-
// throw new ApplicationException($"Push failed: {pushResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
31-
//}
32-
33-
//PullResult pullResult = await this.PullAsync(cancellationToken);
34-
//if (!pullResult.IsSuccessful)
35-
//{
36-
// throw new ApplicationException($"Pull failed: {pullResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
37-
//}
36+
#if OFFLINE_SYNC_ENABLED
37+
PushResult pushResult = await this.PushAsync(cancellationToken);
38+
if (!pushResult.IsSuccessful)
39+
{
40+
throw new ApplicationException($"Push failed: {pushResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
41+
}
42+
43+
PullResult pullResult = await this.PullAsync(cancellationToken);
44+
if (!pullResult.IsSuccessful)
45+
{
46+
throw new ApplicationException($"Pull failed: {pullResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
47+
}
48+
#endif
3849
}
3950
}
4051

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics;
6+
using System.Net.Http;
7+
8+
namespace TodoApp.WPF.Services;
9+
10+
/// <summary>
11+
/// A delegating handler that logs the request/response to stdout.
12+
/// </summary>
13+
public class LoggingHandler : DelegatingHandler
14+
{
15+
public LoggingHandler() : base()
16+
{
17+
}
18+
19+
public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
20+
{
21+
}
22+
23+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
24+
{
25+
Debug.WriteLine($"[HTTP] >>> {request.Method} {request.RequestUri}");
26+
await WriteContentAsync(request.Content, cancellationToken);
27+
28+
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
29+
30+
Debug.WriteLine($"[HTTP] <<< {response.StatusCode} {response.ReasonPhrase}");
31+
await WriteContentAsync(response.Content, cancellationToken);
32+
33+
return response;
34+
}
35+
36+
private static async Task WriteContentAsync(HttpContent? content, CancellationToken cancellationToken = default)
37+
{
38+
if (content is not null)
39+
{
40+
Debug.WriteLine($"[HTTP] >>> {await content.ReadAsStringAsync(cancellationToken)}");
41+
}
42+
}
43+
}

samples/todoapp/TodoApp.WPF/TodoApp.WPF.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net8.0-windows</TargetFramework>
5+
<TargetFramework>net9.0-windows</TargetFramework>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<UseWPF>true</UseWPF>
@@ -19,7 +19,7 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.0" />
23-
<PackageReference Include="CommunityToolkit.Datasync.Client" Version="8.0.4" />
22+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
23+
<PackageReference Include="CommunityToolkit.Datasync.Client" Version="9.0.0" />
2424
</ItemGroup>
2525
</Project>

0 commit comments

Comments
 (0)