Add server projects. Packaging with WinUI server doesn't work. Payload contains two or more files etc.

This commit is contained in:
Burak Kaan Köse
2024-07-21 02:17:11 +02:00
parent 1f472d78e0
commit f112f369a7
29 changed files with 186 additions and 153 deletions

10
Wino.Server.WPF/App.xaml Normal file
View File

@@ -0,0 +1,10 @@
<Application
x:Class="Wino.Server.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown"
xmlns:local="clr-namespace:Wino.Server">
<Application.Resources>
<ResourceDictionary Source="TrayIconResources.xaml" />
</Application.Resources>
</Application>

131
Wino.Server.WPF/App.xaml.cs Normal file
View File

@@ -0,0 +1,131 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using H.NotifyIcon;
using Microsoft.Extensions.DependencyInjection;
using Windows.Storage;
using Wino.Core;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Services;
using Wino.Core.UWP;
//using Wino.Core.UWP;
namespace Wino.Server
{
/// <summary>
/// Single instance Wino Server.
/// Instancing is done using Mutex.
/// App will not start if another instance is already running.
/// App will let running server know that server execution is triggered, which will
/// led server to start new connection to requesting UWP app.
/// </summary>
public partial class App : Application
{
private const string NotifyIconResourceKey = "NotifyIcon";
private const string WinoServerAppName = "Wino.Server";
private const string WinoServerActiatedName = "Wino.Server.Activated";
public new static App Current => (App)Application.Current;
private TaskbarIcon? notifyIcon;
private static Mutex _mutex = null;
private EventWaitHandle _eventWaitHandle;
public IServiceProvider Services { get; private set; }
private IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddTransient<ServerContext>();
services.AddTransient<ServerViewModel>();
services.RegisterCoreServices();
services.RegisterCoreUWPServices();
// 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();
}
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;
}
protected override async void OnStartup(StartupEventArgs e)
{
_mutex = new Mutex(true, WinoServerAppName, out bool isCreatedNew);
_eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, WinoServerActiatedName);
if (isCreatedNew)
{
// Spawn a thread which will be waiting for our event
var thread = new Thread(() =>
{
while (_eventWaitHandle.WaitOne())
{
if (notifyIcon == null) return;
Current.Dispatcher.BeginInvoke(async () =>
{
if (notifyIcon.DataContext is ServerViewModel trayIconViewModel)
{
await trayIconViewModel.ReconnectAsync();
}
});
}
});
// It is important mark it as background otherwise it will prevent app from exiting.
thread.IsBackground = true;
thread.Start();
Services = ConfigureServices();
base.OnStartup(e);
var serverViewModel = await InitializeNewServerAsync();
// Create taskbar icon for the new server.
notifyIcon = (TaskbarIcon)FindResource(NotifyIconResourceKey);
notifyIcon.DataContext = serverViewModel;
notifyIcon.ForceCreate(enablesEfficiencyMode: true);
}
else
{
// Notify other instance so it could reconnect to UWP app if needed.
_eventWaitHandle.Set();
// Terminate this instance.
Shutdown();
}
}
protected override void OnExit(ExitEventArgs e)
{
notifyIcon?.Dispose();
base.OnExit(e);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

View File

@@ -0,0 +1,221 @@
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;
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
}
}

View File

@@ -0,0 +1,40 @@
using System.Threading.Tasks;
using System.Windows;
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.
Application.Current.Shutdown();
}
public async Task ReconnectAsync() => await Context.InitializeAppServiceConnectionAsync();
public Task InitializeAsync() => Context.InitializeAppServiceConnectionAsync();
}
}

View File

@@ -0,0 +1,21 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:server="clr-namespace:Wino.Server">
<ContextMenu x:Shared="false" x:Key="SysTrayMenu">
<MenuItem Header="Launch Wino Mail" Command="{Binding LaunchWinoCommand}" />
<Separator />
<MenuItem Header="Exit" Command="{Binding ExitApplicationCommand}" />
</ContextMenu>
<tb:TaskbarIcon
x:Key="NotifyIcon"
IconSource="Images/Wino_Icon.ico"
ToolTipText="Wino Mail"
LeftClickCommand="{Binding LaunchWinoCommand}"
NoLeftClickDelay="True"
ContextMenu="{StaticResource SysTrayMenu}" />
</ResourceDictionary>

View File

@@ -0,0 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>Wino.Server.WinUI</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &gt;= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Wino.Server.App</StartupObject>
</PropertyGroup>
<ItemGroup>
<None Remove="Images\Wino_Icon.ico" />
<None Remove="TrayIconResources.xaml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Wino.Core.UWP\Services\ConfigurationService.cs" Link="Services\ConfigurationService.cs" />
<Compile Include="..\Wino.Core.UWP\Services\NativeAppService.cs" Link="Services\NativeAppService.cs" />
<Compile Include="..\Wino.Core.UWP\Services\NotificationBuilder.cs" Link="Services\NotificationBuilder.cs" />
<Compile Include="..\Wino.Core.UWP\Services\PreferencesService.cs" Link="Services\PreferencesService.cs" />
</ItemGroup>
<ItemGroup>
<Resource Include="Images\Wino_Icon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Wino.Core.Domain\Wino.Core.Domain.NET8.csproj" />
<ProjectReference Include="..\Wino.Core\Wino.Core.NET8.csproj" />
<ProjectReference Include="..\Wino.Messages\Wino.Messaging.NET8.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>
<ItemGroup>
<Page Update="TrayIconResources.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<OutputType>WinExe</OutputType>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &gt;= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<CsWinRTComponent>true</CsWinRTComponent>
<CsWinRTWindowsMetadata>10.0.22621.0</CsWinRTWindowsMetadata>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Wino.Server.App</StartupObject>
</PropertyGroup>
<ItemGroup>
<None Remove="Images\Wino_Icon.ico" />
</ItemGroup>
<ItemGroup>
<Resource Include="Images\Wino_Icon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Wino.Core.Domain\Wino.Core.Domain.NET8.csproj" />
<ProjectReference Include="..\Wino.Core.UWP\Wino.Core.WinUI.csproj" />
<ProjectReference Include="..\Wino.Core\Wino.Core.NET8.csproj" />
<ProjectReference Include="..\Wino.Messages\Wino.Messaging.NET8.csproj" />
</ItemGroup>
</Project>