Implement NET8 server but without packaging capability.
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<Application
|
||||
x:Class="Wino.Server.NET8.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:Wino.Server.NET8">
|
||||
xmlns:local="using:Wino.Server.NET8"
|
||||
xmlns:styles="using:Wino.Server.Styles">
|
||||
<Application.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
|
||||
<!-- Other merged dictionaries here -->
|
||||
<styles:TrayIconResources />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<!-- Other app resources here -->
|
||||
</ResourceDictionary>
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
|
||||
@@ -1,32 +1,87 @@
|
||||
using H.NotifyIcon;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using H.NotifyIcon;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Windows.Storage;
|
||||
using Wino.Core;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Core.Services;
|
||||
using Wino.Core.UWP.Services;
|
||||
using Wino.Services;
|
||||
|
||||
namespace Wino.Server.NET8
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
public TaskbarIcon? TrayIcon { get; private set; }
|
||||
public Window? Window { get; set; }
|
||||
public new static App Current => (App)Application.Current;
|
||||
|
||||
private const string WinoServerAppName = "Wino.Server";
|
||||
|
||||
public TaskbarIcon TrayIcon { get; private set; }
|
||||
public bool HandleClosedEvents { get; set; } = true;
|
||||
public IServiceProvider Services { get; private set; }
|
||||
public App()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override void OnLaunched(LaunchActivatedEventArgs args)
|
||||
private IServiceProvider ConfigureServices()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
services.AddTransient<ServerContext>();
|
||||
services.AddTransient<ServerViewModel>();
|
||||
|
||||
services.RegisterCoreServices();
|
||||
|
||||
// Below services belongs to UWP.Core package and some APIs are not available for WPF.
|
||||
// We register them here to avoid compilation errors.
|
||||
|
||||
services.AddSingleton<IConfigurationService, ConfigurationService>();
|
||||
services.AddSingleton<INativeAppService, NativeAppService>();
|
||||
services.AddSingleton<IPreferencesService, PreferencesService>();
|
||||
|
||||
return services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
protected override async void OnLaunched(LaunchActivatedEventArgs args)
|
||||
{
|
||||
Services = ConfigureServices();
|
||||
|
||||
await InitializeNewServerAsync();
|
||||
InitializeTrayIcon();
|
||||
}
|
||||
|
||||
private async Task<ServerViewModel> InitializeNewServerAsync()
|
||||
{
|
||||
// TODO: Error handling.
|
||||
|
||||
var databaseService = Services.GetService<IDatabaseService>();
|
||||
var applicationFolderConfiguration = Services.GetService<IApplicationConfiguration>();
|
||||
|
||||
applicationFolderConfiguration.ApplicationDataFolderPath = ApplicationData.Current.LocalFolder.Path;
|
||||
applicationFolderConfiguration.PublisherSharedFolderPath = ApplicationData.Current.GetPublisherCacheFolder(ApplicationConfiguration.SharedFolderName).Path;
|
||||
|
||||
await databaseService.InitializeAsync();
|
||||
|
||||
var serverViewModel = Services.GetRequiredService<ServerViewModel>();
|
||||
|
||||
await serverViewModel.InitializeAsync();
|
||||
|
||||
return serverViewModel;
|
||||
}
|
||||
|
||||
private void InitializeTrayIcon()
|
||||
{
|
||||
var showHideWindowCommand = (XamlUICommand)Resources["ShowHideWindowCommand"];
|
||||
// showHideWindowCommand.ExecuteRequested ;
|
||||
var viewModel = Services.GetService<ServerViewModel>();
|
||||
|
||||
var exitApplicationCommand = (XamlUICommand)Resources["ExitApplicationCommand"];
|
||||
//exitApplicationCommand.ExecuteRequested += ExitApplicationCommand_ExecuteRequested;
|
||||
var launchCommand = (XamlUICommand)Resources["LaunchCommand"];
|
||||
launchCommand.Command = viewModel.LaunchWinoCommand;
|
||||
|
||||
var exitApplicationCommand = (XamlUICommand)Resources["TerminateCommand"];
|
||||
exitApplicationCommand.Command = viewModel.ExitApplicationCommand;
|
||||
|
||||
TrayIcon = (TaskbarIcon)Resources["TrayIcon"];
|
||||
TrayIcon.ForceCreate();
|
||||
|
||||
217
Wino.Server.NET8/ServerContext.cs
Normal file
217
Wino.Server.NET8/ServerContext.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Windows.ApplicationModel;
|
||||
using Windows.ApplicationModel.AppService;
|
||||
using Windows.Foundation.Collections;
|
||||
using Wino.Core.Authenticators;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Core.Domain.Models.Synchronization;
|
||||
using Wino.Core.Integration.Processors;
|
||||
using Wino.Core.Services;
|
||||
using Wino.Core.Synchronizers;
|
||||
using Wino.Messaging;
|
||||
using Wino.Messaging.Enums;
|
||||
using Wino.Messaging.Server;
|
||||
using Wino.Server.NET8;
|
||||
|
||||
namespace Wino.Server
|
||||
{
|
||||
public class ServerContext :
|
||||
IRecipient<AccountCreatedMessage>,
|
||||
IRecipient<AccountUpdatedMessage>,
|
||||
IRecipient<AccountRemovedMessage>,
|
||||
IRecipient<DraftCreated>,
|
||||
IRecipient<DraftFailed>,
|
||||
IRecipient<DraftMapped>,
|
||||
IRecipient<FolderRenamed>,
|
||||
IRecipient<FolderSynchronizationEnabled>,
|
||||
IRecipient<MailAddedMessage>,
|
||||
IRecipient<MailDownloadedMessage>,
|
||||
IRecipient<MailRemovedMessage>,
|
||||
IRecipient<MailUpdatedMessage>,
|
||||
IRecipient<MergedInboxRenamed>
|
||||
{
|
||||
private static object connectionLock = new object();
|
||||
|
||||
private AppServiceConnection connection = null;
|
||||
|
||||
private readonly IDatabaseService _databaseService;
|
||||
private readonly IApplicationConfiguration _applicationFolderConfiguration;
|
||||
|
||||
public ServerContext(IDatabaseService databaseService, IApplicationConfiguration applicationFolderConfiguration)
|
||||
{
|
||||
_databaseService = databaseService;
|
||||
_applicationFolderConfiguration = applicationFolderConfiguration;
|
||||
|
||||
WeakReferenceMessenger.Default.RegisterAll(this);
|
||||
}
|
||||
|
||||
#region Message Handlers
|
||||
|
||||
public async void Receive(MailAddedMessage message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(AccountCreatedMessage message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(AccountUpdatedMessage message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(AccountRemovedMessage message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(DraftCreated message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(DraftFailed message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(DraftMapped message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(FolderRenamed message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(FolderSynchronizationEnabled message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(MailDownloadedMessage message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(MailRemovedMessage message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(MailUpdatedMessage message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
public async void Receive(MergedInboxRenamed message) => await SendMessageAsync(MessageType.UIMessage, message);
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// Open connection to UWP app service
|
||||
/// </summary>
|
||||
public async Task InitializeAppServiceConnectionAsync()
|
||||
{
|
||||
if (connection != null) DisposeConnection();
|
||||
|
||||
connection = new AppServiceConnection
|
||||
{
|
||||
AppServiceName = "WinoInteropService",
|
||||
PackageFamilyName = GetAppPackagFamilyName()
|
||||
};
|
||||
|
||||
connection.RequestReceived += OnWinRTMessageReceived;
|
||||
connection.ServiceClosed += OnConnectionClosed;
|
||||
|
||||
AppServiceConnectionStatus status = await connection.OpenAsync();
|
||||
|
||||
if (status != AppServiceConnectionStatus.Success)
|
||||
{
|
||||
// TODO: Handle connection error
|
||||
|
||||
DisposeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task TestOutlookSynchronizer()
|
||||
{
|
||||
var accountService = App.Current.Services.GetService<IAccountService>();
|
||||
|
||||
var accs = await accountService.GetAccountsAsync();
|
||||
var acc = accs.ElementAt(0);
|
||||
|
||||
var authenticator = App.Current.Services.GetService<OutlookAuthenticator>();
|
||||
var processor = App.Current.Services.GetService<IOutlookChangeProcessor>();
|
||||
|
||||
var sync = new OutlookSynchronizer(acc, authenticator, processor);
|
||||
|
||||
var options = new SynchronizationOptions()
|
||||
{
|
||||
AccountId = acc.Id,
|
||||
Type = Core.Domain.Enums.SynchronizationType.Full
|
||||
};
|
||||
|
||||
var result = await sync.SynchronizeAsync(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes current connection to UWP app service.
|
||||
/// </summary>
|
||||
private void DisposeConnection()
|
||||
{
|
||||
lock (connectionLock)
|
||||
{
|
||||
if (connection == null) return;
|
||||
|
||||
connection.RequestReceived -= OnWinRTMessageReceived;
|
||||
connection.ServiceClosed -= OnConnectionClosed;
|
||||
|
||||
connection.Dispose();
|
||||
connection = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a serialized object to UWP application if connection exists with given type.
|
||||
/// </summary>
|
||||
/// <param name="messageType">Type of the message.</param>
|
||||
/// <param name="message">IServerMessage object that will be serialized.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException">When the message is not IServerMessage.</exception>
|
||||
private async Task SendMessageAsync(MessageType messageType, object message)
|
||||
{
|
||||
if (connection == null) return;
|
||||
|
||||
if (message is not IServerMessage serverMessage)
|
||||
throw new ArgumentException("Server message must be a type of IServerMessage");
|
||||
|
||||
string json = JsonSerializer.Serialize(message);
|
||||
|
||||
var set = new ValueSet
|
||||
{
|
||||
{ MessageConstants.MessageTypeKey, (int)messageType },
|
||||
{ MessageConstants.MessageDataKey, json },
|
||||
{ MessageConstants.MessageDataTypeKey, message.GetType().Name }
|
||||
};
|
||||
|
||||
Debug.WriteLine($"S: {messageType} ({message.GetType().Name})");
|
||||
await connection.SendMessageAsync(set);
|
||||
}
|
||||
|
||||
private void OnConnectionClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
|
||||
{
|
||||
// TODO: Handle connection closed.
|
||||
|
||||
// UWP app might've been terminated or suspended.
|
||||
// At this point, we must keep active synchronizations going, but connection is lost.
|
||||
// As long as this process is alive, database will be kept updated, but no messages will be sent.
|
||||
|
||||
DisposeConnection();
|
||||
}
|
||||
|
||||
private void OnWinRTMessageReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
|
||||
{
|
||||
// TODO: Handle incoming messages from UWP/WINUI Application.
|
||||
|
||||
}
|
||||
|
||||
#region Init
|
||||
|
||||
private string GetAppPackagFamilyName()
|
||||
{
|
||||
// If running as a standalone app, Package will throw exception.
|
||||
// Return hardcoded value for debugging purposes.
|
||||
// Connection will not be available in this case.
|
||||
|
||||
try
|
||||
{
|
||||
return Package.Current.Id.FamilyName;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "Debug.Wino.Server.FamilyName";
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
|
||||
await InitializeAppServiceConnectionAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
37
Wino.Server.NET8/ServerViewModel.cs
Normal file
37
Wino.Server.NET8/ServerViewModel.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Threading.Tasks;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
|
||||
namespace Wino.Server
|
||||
{
|
||||
public partial class ServerViewModel : ObservableObject, IInitializeAsync
|
||||
{
|
||||
public ServerContext Context { get; }
|
||||
|
||||
public ServerViewModel(ServerContext serverContext)
|
||||
{
|
||||
Context = serverContext;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task LaunchWinoAsync()
|
||||
{
|
||||
await Context.TestOutlookSynchronizer();
|
||||
// ServerContext.SendTestMessageAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuts down the application.
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
public void ExitApplication()
|
||||
{
|
||||
// TODO: App service send message to UWP app to terminate itself.
|
||||
}
|
||||
|
||||
public async Task ReconnectAsync() => await Context.InitializeAppServiceConnectionAsync();
|
||||
|
||||
public Task InitializeAsync() => Context.InitializeAppServiceConnectionAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
<ResourceDictionary
|
||||
x:Class="Wino.Server.TrayIconResources"
|
||||
x:Class="Wino.Server.Styles.TrayIconResources"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:domain="using:Wino.Core.Domain"
|
||||
xmlns:tb="using:H.NotifyIcon"
|
||||
xmlns:local="using:Wino.Server">
|
||||
xmlns:local="using:Wino.Server"
|
||||
xmlns:tb="using:H.NotifyIcon">
|
||||
|
||||
<!-- TODO: Localize by Translator. -->
|
||||
|
||||
<XamlUICommand
|
||||
x:Key="LaunchCommand"
|
||||
Label="Launch"
|
||||
@@ -30,10 +29,10 @@
|
||||
x:Key="TrayIcon"
|
||||
Visibility="Visible"
|
||||
ToolTipText="Wino Mail"
|
||||
ContextMenuMode="SecondWindow"
|
||||
ContextMenuMode="PopupMenu"
|
||||
LeftClickCommand="{StaticResource LaunchCommand}"
|
||||
NoLeftClickDelay="True"
|
||||
IconSource="\Images\Wino_Icon.ico">
|
||||
IconSource="\Assets\Wino_Icon.ico">
|
||||
<tb:TaskbarIcon.ContextFlyout>
|
||||
<MenuFlyout>
|
||||
<MenuFlyoutItem Command="{StaticResource LaunchCommand}" />
|
||||
12
Wino.Server.NET8/Styles/TrayIconResources.xaml.cs
Normal file
12
Wino.Server.NET8/Styles/TrayIconResources.xaml.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Wino.Server.Styles
|
||||
{
|
||||
partial class TrayIconResources : ResourceDictionary
|
||||
{
|
||||
public TrayIconResources()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Wino.Server
|
||||
{
|
||||
partial class TrayIconResources : ResourceDictionary
|
||||
{
|
||||
public TrayIconResources()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,13 @@
|
||||
<RootNamespace>Wino.Server</RootNamespace>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
<Platforms>x86;x64;ARM64</Platforms>
|
||||
<SelfContained>true</SelfContained>
|
||||
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) >= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers>
|
||||
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) < 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
|
||||
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
|
||||
<UseWinUI>true</UseWinUI>
|
||||
<EnableMsixTooling>true</EnableMsixTooling>
|
||||
<DisableEmbeddedXbf>false</DisableEmbeddedXbf>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Disable XAML generated main to enable single activation. -->
|
||||
@@ -34,12 +36,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Remove="Assets\Wino_Icon.ico" />
|
||||
<None Remove="app.manifest" />
|
||||
<None Remove="Assets\Wino_Icon.ico" />
|
||||
<None Remove="TrayIconResources.xaml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="app.manifest" />
|
||||
<None Remove="TrayIconResources.xaml" />
|
||||
<Page Remove="App.xaml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -86,6 +89,10 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Styles\" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--
|
||||
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
|
||||
|
||||
Reference in New Issue
Block a user